From df0c7ef8230cab31870e096de195f37abce4dd0f Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Wed, 10 Jul 2013 12:08:08 -0600 Subject: [PATCH 1/2] Convert encoding/decoding tests to benchmarks. --- append_entries_test.go | 34 +++++++++++++++++++++++++++++++ encodingAndDecoding_test.go | 40 ------------------------------------- 2 files changed, 34 insertions(+), 40 deletions(-) create mode 100644 append_entries_test.go delete mode 100644 encodingAndDecoding_test.go diff --git a/append_entries_test.go b/append_entries_test.go new file mode 100644 index 0000000000..40366647a5 --- /dev/null +++ b/append_entries_test.go @@ -0,0 +1,34 @@ +package raft + +import ( + "bytes" + "encoding/json" + "testing" +) + +func BenchmarkAppendEntriesEncoding(b *testing.B) { + req, _ := createTestAppendEntriesRequest(2000) + for i := 0; i < b.N; i++ { + var buf bytes.Buffer + json.NewEncoder(&buf).Encode(req) + } +} + +func BenchmarkAppendEntriesDecoding(b *testing.B) { + req, buf := createTestAppendEntriesRequest(2000) + for i := 0; i < b.N; i++ { + json.NewDecoder(bytes.NewReader(buf)).Decode(req) + } +} + +func createTestAppendEntriesRequest(entryCount int) (*AppendEntriesRequest, []byte) { + entries := make([]*LogEntry, 0) + for i := 0; i < entryCount; i++ { + entries = append(entries, newLogEntry(nil, 1, 2, &joinCommand{Name: "localhost:1000"})) + } + req := newAppendEntriesRequest(1, "leader", 1, 1, entries, 1) + buf, _ := json.Marshal(req) + + return req, buf +} + diff --git a/encodingAndDecoding_test.go b/encodingAndDecoding_test.go deleted file mode 100644 index 50d23bf904..0000000000 --- a/encodingAndDecoding_test.go +++ /dev/null @@ -1,40 +0,0 @@ -package raft - -import ( - "bytes" - "encoding/json" - "fmt" - "testing" - "time" -) - -func TestEncodingAndDecodingSpeed(t *testing.T) { - entries := make([]*LogEntry, 2000) - - e := newLogEntry(nil, 1, 2, &joinCommand{Name: "localhost:1000"}) - - for i := 0; i < 2000; i++ { - entries[i] = e - } - - req := newAppendEntriesRequest(1, "leader", 1, 1, entries, 1) - - var b bytes.Buffer - - startTime := time.Now() - - json.NewEncoder(&b).Encode(req) - - fmt.Println("Encoding ", b.Len()/1000, " kb took ", time.Now().Sub(startTime)) - - startTime = time.Now() - - resp := &AppendEntriesResponse{} - - length := b.Len() - - json.NewDecoder(&b).Decode(&resp) - - fmt.Println("Decoding ", length/1000, " kb took ", time.Now().Sub(startTime)) - -} From 628b86fe1df445f9b0544a980123365ff96db68a Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Wed, 10 Jul 2013 12:10:52 -0600 Subject: [PATCH 2/2] Add throughput metrics to benchmark. --- append_entries_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/append_entries_test.go b/append_entries_test.go index 40366647a5..6cecad0da4 100644 --- a/append_entries_test.go +++ b/append_entries_test.go @@ -7,11 +7,12 @@ import ( ) func BenchmarkAppendEntriesEncoding(b *testing.B) { - req, _ := createTestAppendEntriesRequest(2000) + req, tmp := createTestAppendEntriesRequest(2000) for i := 0; i < b.N; i++ { var buf bytes.Buffer json.NewEncoder(&buf).Encode(req) } + b.SetBytes(int64(len(tmp))) } func BenchmarkAppendEntriesDecoding(b *testing.B) { @@ -19,6 +20,7 @@ func BenchmarkAppendEntriesDecoding(b *testing.B) { for i := 0; i < b.N; i++ { json.NewDecoder(bytes.NewReader(buf)).Decode(req) } + b.SetBytes(int64(len(buf))) } func createTestAppendEntriesRequest(entryCount int) (*AppendEntriesRequest, []byte) {