Merge pull request #66 from benbjohnson/append-entries-benchmarking

Append entries benchmarking
pull/820/head
Ben Johnson 2013-07-10 11:31:13 -07:00
commit 459c84b32d
2 changed files with 36 additions and 40 deletions

36
append_entries_test.go Normal file
View File

@ -0,0 +1,36 @@
package raft
import (
"bytes"
"encoding/json"
"testing"
)
func BenchmarkAppendEntriesEncoding(b *testing.B) {
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) {
req, buf := createTestAppendEntriesRequest(2000)
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) {
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
}

View File

@ -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))
}