|
| 1 | +// Copyright 2022 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package tpcc |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "net/url" |
| 11 | + "os/exec" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/cockroachdb/cockroach/pkg/base" |
| 15 | + "github.com/cockroachdb/cockroach/pkg/storage" |
| 16 | + "github.com/cockroachdb/cockroach/pkg/testutils/pgurlutils" |
| 17 | + "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" |
| 18 | + "github.com/cockroachdb/cockroach/pkg/testutils/testfixtures" |
| 19 | + "github.com/cockroachdb/cockroach/pkg/util/leaktest" |
| 20 | + "github.com/cockroachdb/cockroach/pkg/util/log" |
| 21 | + "github.com/cockroachdb/cockroach/pkg/util/stop" |
| 22 | + _ "github.com/cockroachdb/cockroach/pkg/workload/tpcc" |
| 23 | +) |
| 24 | + |
| 25 | +// BenchmarkTPCC runs TPC-C transactions against a single warehouse. It runs the |
| 26 | +// client side of the workload in a subprocess so that the client overhead is |
| 27 | +// not included in CPU and heap profiles. |
| 28 | +// |
| 29 | +// The benchmark will generate the schema and table data for a single warehouse, |
| 30 | +// using a reusable store directory. In future runs the cockroach server will |
| 31 | +// clone and use the store directory, rather than regenerating the schema and |
| 32 | +// data. This enables faster iteration when re-running the benchmark. |
| 33 | +func BenchmarkTPCC(b *testing.B) { |
| 34 | + defer leaktest.AfterTest(b)() |
| 35 | + defer log.Scope(b).Close(b) |
| 36 | + |
| 37 | + // Reuse or generate TPCC data. |
| 38 | + storeName := "bench_tpcc_store_" + storage.MinimumSupportedFormatVersion.String() |
| 39 | + storeDir := testfixtures.ReuseOrGenerate(b, storeName, func(dir string) { |
| 40 | + c, output := generateStoreDir.withEnv(storeDirEnvVar, dir).exec() |
| 41 | + if err := c.Run(); err != nil { |
| 42 | + b.Fatalf("failed to generate store dir: %s\n%s", err, output.String()) |
| 43 | + } |
| 44 | + }) |
| 45 | + |
| 46 | + for _, impl := range []struct{ name, flag string }{ |
| 47 | + {"literal", "--literal-implementation=true"}, |
| 48 | + {"optimized", "--literal-implementation=false"}, |
| 49 | + } { |
| 50 | + b.Run(impl.name, func(b *testing.B) { |
| 51 | + for _, mix := range []struct{ name, flag string }{ |
| 52 | + {"new_order", "--mix=newOrder=1"}, |
| 53 | + {"payment", "--mix=payment=1"}, |
| 54 | + {"order_status", "--mix=orderStatus=1"}, |
| 55 | + {"delivery", "--mix=delivery=1"}, |
| 56 | + {"stock_level", "--mix=stockLevel=1"}, |
| 57 | + {"default", "--mix=newOrder=10,payment=10,orderStatus=1,delivery=1,stockLevel=1"}, |
| 58 | + } { |
| 59 | + b.Run(mix.name, func(b *testing.B) { |
| 60 | + run(b, storeDir, []string{impl.flag, mix.flag}) |
| 61 | + }) |
| 62 | + } |
| 63 | + }) |
| 64 | + |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func run(b *testing.B, storeDir string, workloadFlags []string) { |
| 69 | + server, pgURL := startCockroach(b, storeDir) |
| 70 | + defer server.Stopper().Stop(context.Background()) |
| 71 | + c, output := startClient(b, pgURL, workloadFlags) |
| 72 | + |
| 73 | + var s synchronizer |
| 74 | + s.init(c.Process.Pid) |
| 75 | + |
| 76 | + // Reset the timer when the client starts running queries. |
| 77 | + if timedOut := s.waitWithTimeout(); timedOut { |
| 78 | + b.Fatalf("waiting on client timed-out:\n%s", output.String()) |
| 79 | + } |
| 80 | + b.ResetTimer() |
| 81 | + s.notify(b) |
| 82 | + |
| 83 | + // Stop the timer when the client stops running queries. |
| 84 | + s.wait() |
| 85 | + b.StopTimer() |
| 86 | + |
| 87 | + if err := c.Wait(); err != nil { |
| 88 | + b.Fatalf("client failed: %s\n%s\n%s", err, output.String(), output.String()) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func startCockroach( |
| 93 | + b testing.TB, storeDir string, |
| 94 | +) (server serverutils.TestServerInterface, pgURL string) { |
| 95 | + // Clone the store dir. |
| 96 | + td := b.TempDir() |
| 97 | + c, output := cloneEngine. |
| 98 | + withEnv(srcEngineEnvVar, storeDir). |
| 99 | + withEnv(dstEngineEnvVar, td). |
| 100 | + exec() |
| 101 | + if err := c.Run(); err != nil { |
| 102 | + b.Fatalf("failed to clone engine: %s\n%s", err, output.String()) |
| 103 | + } |
| 104 | + |
| 105 | + // Start the server. |
| 106 | + s := serverutils.StartServerOnly(b, base.TestServerArgs{ |
| 107 | + StoreSpecs: []base.StoreSpec{{Path: td}}, |
| 108 | + }) |
| 109 | + |
| 110 | + // Generate a PG URL. |
| 111 | + u, urlCleanup, err := pgurlutils.PGUrlE( |
| 112 | + s.AdvSQLAddr(), b.TempDir(), url.User("root"), |
| 113 | + ) |
| 114 | + if err != nil { |
| 115 | + b.Fatalf("failed to create pgurl: %s", err) |
| 116 | + } |
| 117 | + u.Path = databaseName |
| 118 | + s.Stopper().AddCloser(stop.CloserFn(urlCleanup)) |
| 119 | + |
| 120 | + return s, u.String() |
| 121 | +} |
| 122 | + |
| 123 | +func startClient( |
| 124 | + b *testing.B, pgURL string, workloadFlags []string, |
| 125 | +) (c *exec.Cmd, output *synchronizedBuffer) { |
| 126 | + c, output = runClient. |
| 127 | + withEnv(nEnvVar, b.N). |
| 128 | + withEnv(pgurlEnvVar, pgURL). |
| 129 | + exec(workloadFlags...) |
| 130 | + if err := c.Start(); err != nil { |
| 131 | + b.Fatalf("failed to start client: %s\n%s", err, output.String()) |
| 132 | + } |
| 133 | + return c, output |
| 134 | +} |
0 commit comments