From 7dd474cb05b446380001123d778cc6790b133547 Mon Sep 17 00:00:00 2001 From: Tobias Schottdorf Date: Tue, 8 Dec 2015 22:07:12 -0500 Subject: [PATCH] "benchmark" 1px txn with simulated latency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit name old time/op new time/op delta SingleRoundtripTxnWithLatency_0-8 2.42ms ± 0% 1.65ms ± 0% ~ (p=1.000 n=1+1) SingleRoundtripTxnWithLatency_10-8 25.3ms ± 0% 13.2ms ± 0% ~ (p=1.000 n=1+1) Both changes are not statistically relevant due to the high cost of a single operation, but the second number is actually consistent (the first will fluctuate somewhat). Their value is mostly in showing the benefit of 1pc transactions end-to-end from the KV client. before: PASS BenchmarkSingleRoundtripTxnWithLatency_0-8 20000 2418298 ns/op BenchmarkSingleRoundtripTxnWithLatency_10-8 1000 25316220 ns/op ok github.com/cockroachdb/cockroach/kv 89.027s PASS after: BenchmarkSingleRoundtripTxnWithLatency_0-8 10000 1647978 ns/op BenchmarkSingleRoundtripTxnWithLatency_10-8 2000 13190856 ns/op ok github.com/cockroachdb/cockroach/kv 44.214s --- kv/local_test_cluster.go | 5 +++++ kv/txn_test.go | 21 ++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/kv/local_test_cluster.go b/kv/local_test_cluster.go index 653c70aad108..437e047cf283 100644 --- a/kv/local_test_cluster.go +++ b/kv/local_test_cluster.go @@ -19,6 +19,7 @@ package kv import ( "net" + "time" "golang.org/x/net/context" @@ -57,6 +58,7 @@ type LocalTestCluster struct { Sender *TxnCoordSender distSender *DistSender Stopper *stop.Stopper + Latency time.Duration // sleep for each RPC sent tester util.Tester } @@ -82,6 +84,9 @@ func (ltc *LocalTestCluster) Start(t util.Tester) { getArgs func(addr net.Addr) proto.Message, getReply func() proto.Message, _ *rpc.Context) ([]proto.Message, error) { // TODO(tschottdorf): remove getReply(). + if ltc.Latency > 0 { + time.Sleep(ltc.Latency) + } br, pErr := ltc.stores.Send(context.Background(), *getArgs(nil).(*roachpb.BatchRequest)) if br == nil { br = &roachpb.BatchResponse{} diff --git a/kv/txn_test.go b/kv/txn_test.go index 59b6bd49c59f..b11c0352e623 100644 --- a/kv/txn_test.go +++ b/kv/txn_test.go @@ -102,15 +102,18 @@ func TestTxnDBBasics(t *testing.T) { } } -// BenchmarkTxnWrites benchmarks a number of transactions writing to the -// same key back to back, without using Prepare/Flush. -func BenchmarkTxnWrites(b *testing.B) { - s := createTestDB(b) +// benchmarkSingleRoundtripWithLatency runs a number of transactions writing to +// the same key back to back in a single round-trip. Latency is simulated +// by pausing before each RPC sent. +func benchmarkSingleRoundtripWithLatency(b *testing.B, latency time.Duration) { + s := &LocalTestCluster{} + s.Latency = latency + s.Start(b) defer s.Stop() + defer b.StopTimer() key := roachpb.Key("key") b.ResetTimer() for i := 0; i < b.N; i++ { - s.Manual.Increment(1) if tErr := s.DB.Txn(func(txn *client.Txn) error { b := txn.NewBatch() b.Put(key, fmt.Sprintf("value-%d", i)) @@ -121,6 +124,14 @@ func BenchmarkTxnWrites(b *testing.B) { } } +func BenchmarkSingleRoundtripTxnWithLatency_0(b *testing.B) { + benchmarkSingleRoundtripWithLatency(b, 0) +} + +func BenchmarkSingleRoundtripTxnWithLatency_10(b *testing.B) { + benchmarkSingleRoundtripWithLatency(b, 10*time.Millisecond) +} + // disableOwnNodeCertain is used in tests which want to verify uncertainty // related logic on single-node installations. In regular operation, trans- // actional requests automatically have the "own" NodeID marked as free from