Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add QueryerContext interface #23

Merged
merged 4 commits into from
Feb 28, 2019
Merged

Add QueryerContext interface #23

merged 4 commits into from
Feb 28, 2019

Conversation

surki
Copy link
Collaborator

@surki surki commented Jan 3, 2019

If we don't support QueryerContext, the db.Query() call will always do
"prepare" statement

Solves issue #22

If we don't support QueryerContext, the db.Query() call will always do
"prepare" statement
@coveralls
Copy link

Coverage Status

Coverage decreased (-1.7%) to 65.174% when pulling 98e9bd8 on surki:master into 7408a7f on gchaincl:master.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage decreased (-1.7%) to 65.174% when pulling 98e9bd8 on surki:master into 7408a7f on gchaincl:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.7%) to 65.174% when pulling 98e9bd8 on surki:master into 7408a7f on gchaincl:master.

@coveralls
Copy link

coveralls commented Jan 3, 2019

Coverage Status

Coverage increased (+7.7%) to 74.545% when pulling 5b518ef on surki:master into 7408a7f on gchaincl:master.

@qustavo
Copy link
Owner

qustavo commented Jan 7, 2019

Thanks for doing this @surki . Is there a way to integrate benchmark this?

@surki
Copy link
Collaborator Author

surki commented Jan 21, 2019

@gchaincl Do you want me to benchmark with / without this change?

Here is what I see

sqlhook master (without this changes):

go test -bench=. -benchmem
goos: linux
goarch: amd64
BenchmarkSQLite3/Without_Hooks-8                  200000              8856 ns/op             752 B/op         14 allocs/op
BenchmarkSQLite3/With_Hooks-8                     200000             10370 ns/op             864 B/op         16 allocs/op
BenchmarkMySQL/Without_Hooks-8                     20000             68749 ns/op             629 B/op         11 allocs/op
BenchmarkMySQL/With_Hooks-8                        10000            116692 ns/op             774 B/op         14 allocs/op
BenchmarkPostgres/Without_Hooks-8                  20000             68941 ns/op             800 B/op         17 allocs/op
BenchmarkPostgres/With_Hooks-8                     10000            156187 ns/op            1273 B/op         27 allocs/op
PASS
ok      _/home/suresh/src/sqlhooks      11.130s

With this change:

$ go test -bench=. -benchmem
goos: linux
goarch: amd64
BenchmarkSQLite3/Without_Hooks-8                  200000              9237 ns/op             752 B/op         14 allocs/op
BenchmarkSQLite3/With_Hooks-8                     200000              9466 ns/op             752 B/op         14 allocs/op
BenchmarkMySQL/Without_Hooks-8                     20000             68764 ns/op             629 B/op         11 allocs/op
BenchmarkMySQL/With_Hooks-8                        30000             59447 ns/op             629 B/op         11 allocs/op
BenchmarkPostgres/Without_Hooks-8                  20000             69172 ns/op             800 B/op         17 allocs/op
BenchmarkPostgres/With_Hooks-8                     20000             66238 ns/op             800 B/op         17 allocs/op
PASS
ok      _/home/suresh/src/sqlhooks      12.652s

Weirdly WithHooks is faster now, need to check how it got faster

@qustavo
Copy link
Owner

qustavo commented Jan 21, 2019

Interesting! I'd check whether mysql or psql implement *ExecerContext and *QueryerContext

@surki
Copy link
Collaborator Author

surki commented Jan 23, 2019

They do implement, verified it.
Did a quick test with dlv and pprof, both WithHooks and WithoutHooks take same code path.
See below callstack for db.Query() call, all the way to mysqlConn.query.

Without hook                                                                   With hook

github.com/go-sql-driver/mysql.(*mysqlConn).query                              github.com/go-sql-driver/mysql.(*mysqlConn).query        
github.com/go-sql-driver/mysql.(*mysqlConn).QueryContext                       github.com/go-sql-driver/mysql.(*mysqlConn).QueryContext
                                                                               sqlhooks.(*QueryerContext).queryContext
                                                                               sqlhooks.(*QueryerContext).QueryContext
database/sql.ctxDriverQuery                                                    database/sql.ctxDriverQuery            
database/sql.(*DB).queryDC.func1                                               database/sql.(*DB).queryDC.func1       
database/sql.withLock                                                          database/sql.withLock                  
database/sql.(*DB).queryDC                                                     database/sql.(*DB).queryDC             
database/sql.(*DB).query                                                       database/sql.(*DB).query               
database/sql.(*DB).QueryContext                                                database/sql.(*DB).QueryContext        
database/sql.(*DB).Query                                                       database/sql.(*DB).Query               
main.benchmark                                                                 main.benchmark                         
main.main                                                                      main.main                              
runtime.main                                                                   runtime.main                           
runtime.goexit                                                                 runtime.goexit                         

I am in middle of something, I will get to this next week for further check

@qustavo
Copy link
Owner

qustavo commented Jan 23, 2019

Wow, that's impressive! Thank you so much for doing it.
I am looking forward to seeing what you find out.

@surki
Copy link
Collaborator Author

surki commented Feb 19, 2019

SessionResetter interface was the difference (MySQL driver implements it, so it was doing extra work that SQLHook wasn't doing, so SQLHooks was faster ...).

I have implemented that, and as well as added unit tests for interface implementation.

Numbers now (overhead is negligible):

$ go test -bench=.
goos: linux
goarch: amd64
BenchmarkSQLite3/Without_Hooks-8                  200000              9137 ns/op
BenchmarkSQLite3/With_Hooks-8                     200000              9361 ns/op
BenchmarkMySQL/Without_Hooks-8                     20000             69912 ns/op
BenchmarkMySQL/With_Hooks-8                        20000             70635 ns/op
BenchmarkPostgres/Without_Hooks-8                  20000             67732 ns/op
BenchmarkPostgres/With_Hooks-8                     20000             68989 ns/op
PASS
ok      _/home/suresh/src/sqlhooks      12.343s

@qustavo
Copy link
Owner

qustavo commented Feb 19, 2019

That makes much more sense, this is a great piece of work!

@qustavo
Copy link
Owner

qustavo commented Feb 19, 2019

@keegancsmith I'd love to have your input on this one

Copy link
Collaborator

@keegancsmith keegancsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

sqlhooks.go Outdated Show resolved Hide resolved
sqlhooks.go Outdated Show resolved Hide resolved
@surki
Copy link
Collaborator Author

surki commented Feb 26, 2019

hmm, so looks like driver.SessionResetter is go 1.10+. That makes SQLHook require go 1.10+.
Is it okay? If not, we can disable SessionResetter changes for now (but that will change behavior of underlying driver when going through SQLHooks).

Or we could conditional compile it only for go1.10+: surki@bcd1551

@qustavo
Copy link
Owner

qustavo commented Feb 27, 2019

surki@bcd1551 looks like the right approach to me

Copy link
Owner

@qustavo qustavo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update with the build tag changes and merge

@qustavo qustavo merged commit 1932c8d into qustavo:master Feb 28, 2019
@qustavo
Copy link
Owner

qustavo commented Feb 28, 2019

@surki
Copy link
Collaborator Author

surki commented Feb 28, 2019

Okay, thanks, we probably should update the benchmark numbers in README

@qustavo
Copy link
Owner

qustavo commented Feb 28, 2019 via email

@surki surki mentioned this pull request Mar 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants