-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql: benchmark the pgbench tcp-b batch
standalone pgbench still provides more detailed instrumentation and more configurable options, but a pure-go benchmark is useful too, since its easier to run and integrate with out existing benchmarks. ``` name time/op PgbenchQuery_Cockroach-8 3.16ms ± 1% PgbenchQuery_Postgres-8 407µs ± 8% ParallelPgbenchQuery_Cockroach-8 2.45ms ± 9% ParallelPgbenchQuery_Postgres-8 188µs ±17% name alloc/op PgbenchQuery_Cockroach-8 230kB ± 0% ParallelPgbenchQuery_Cockroach-8 246kB ±11% name allocs/op PgbenchQuery_Cockroach-8 3.48k ± 0% ParallelPgbenchQuery_Cockroach-8 3.90k ± 7% ```
- Loading branch information
Showing
5 changed files
with
174 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2016 The Cockroach Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
// | ||
// Author: David Taylor (david@cockroachlabs.com) | ||
|
||
package pgbench | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"math/rand" | ||
) | ||
|
||
// This is the TPC-B(ish) query that pgbench runs. | ||
// We don't use placeholders because pgwire protocol does not | ||
// allow multiple statements in prepared queries. | ||
const tpcbQuery = `BEGIN; | ||
UPDATE pgbench_accounts SET abalance = abalance + %[1]d WHERE aid = %[2]d; | ||
SELECT abalance FROM pgbench_accounts WHERE aid = %[2]d; | ||
UPDATE pgbench_tellers SET tbalance = tbalance + %[1]d WHERE tid = %[3]d; | ||
UPDATE pgbench_branches SET bbalance = bbalance + %[1]d WHERE bid = %[4]d; | ||
INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (%[3]d, %[4]d, %[2]d, %[1]d, CURRENT_TIMESTAMP); | ||
END;` // vars: 1 delta, 2 aid, 3 tid, 4 bid | ||
|
||
// RunOne executes one iteration of the query batch that `pgbench` executes. | ||
func RunOne(db *sql.DB, r *rand.Rand, accounts int) error { | ||
account := r.Intn(accounts) | ||
delta := r.Intn(5000) | ||
teller := r.Intn(tellers) | ||
branch := 1 | ||
|
||
q := fmt.Sprintf(tpcbQuery, delta, account, teller, branch) | ||
_, err := db.Exec(q) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2016 The Cockroach Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
// | ||
// Author: David Taylor (david@cockroachlabs.com) | ||
|
||
package sql_test | ||
|
||
import ( | ||
"database/sql" | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/sql/pgbench" | ||
) | ||
|
||
// Tests a batch of queries very similar to those that that PGBench runs | ||
// in its TPC-B(ish) mode. | ||
func runPgbenchQuery(b *testing.B, db *sql.DB) { | ||
if err := pgbench.SetupBenchDB(db, 20000, true /*quiet*/); err != nil { | ||
b.Fatal(err) | ||
} | ||
src := rand.New(rand.NewSource(5432)) | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
if err := pgbench.RunOne(db, src, 20000); err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
b.StopTimer() | ||
} | ||
|
||
// Tests a batch of queries very similar to those that that PGBench runs | ||
// in its TPC-B(ish) mode. | ||
func runPgbenchQueryParallel(b *testing.B, db *sql.DB) { | ||
if err := pgbench.SetupBenchDB(db, 20000, true /*quiet*/); err != nil { | ||
b.Fatal(err) | ||
} | ||
|
||
b.ResetTimer() | ||
b.RunParallel(func(pb *testing.PB) { | ||
src := rand.New(rand.NewSource(5432)) | ||
for pb.Next() { | ||
if err := pgbench.RunOne(db, src, 20000); err != nil { | ||
// TODO(dt): handle retry/aborted correctly | ||
// b.Fatal(err) | ||
} | ||
} | ||
}) | ||
b.StopTimer() | ||
} | ||
|
||
func BenchmarkPgbenchQuery_Cockroach(b *testing.B) { | ||
benchmarkCockroach(b, runPgbenchQuery) | ||
} | ||
|
||
func BenchmarkPgbenchQuery_Postgres(b *testing.B) { | ||
benchmarkPostgres(b, runPgbenchQuery) | ||
} | ||
|
||
func BenchmarkParallelPgbenchQuery_Cockroach(b *testing.B) { | ||
benchmarkCockroach(b, runPgbenchQueryParallel) | ||
} | ||
|
||
func BenchmarkParallelPgbenchQuery_Postgres(b *testing.B) { | ||
benchmarkPostgres(b, runPgbenchQueryParallel) | ||
} |