File tree Expand file tree Collapse file tree 1 file changed +33
-1
lines changed
Expand file tree Collapse file tree 1 file changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -51,7 +51,6 @@ func initDB(b *testing.B, queries ...string) *sql.DB {
5151 return db
5252}
5353
54- // by Brad Fitzpatrick
5554const concurrencyLevel = 10
5655
5756func BenchmarkQuery (b * testing.B ) {
@@ -93,6 +92,39 @@ func BenchmarkQuery(b *testing.B) {
9392 }
9493}
9594
95+ func BenchmarkExec (b * testing.B ) {
96+ tb := (* TB )(b )
97+ b .StopTimer ()
98+ b .ReportAllocs ()
99+ db := tb .checkDB (sql .Open ("mysql" , dsn ))
100+ db .SetMaxIdleConns (concurrencyLevel )
101+ defer db .Close ()
102+
103+ stmt := tb .checkStmt (db .Prepare ("DO 1" ))
104+ defer stmt .Close ()
105+
106+ remain := int64 (b .N )
107+ var wg sync.WaitGroup
108+ wg .Add (concurrencyLevel )
109+ defer wg .Wait ()
110+ b .StartTimer ()
111+
112+ for i := 0 ; i < concurrencyLevel ; i ++ {
113+ go func () {
114+ for {
115+ if atomic .AddInt64 (& remain , - 1 ) < 0 {
116+ wg .Done ()
117+ return
118+ }
119+
120+ if _ , err := stmt .Exec (); err != nil {
121+ b .Fatal (err .Error ())
122+ }
123+ }
124+ }()
125+ }
126+ }
127+
96128// data, but no db writes
97129var roundtripSample []byte
98130
You can’t perform that action at this time.
0 commit comments