add encoding speed test and gofmt

pull/820/head
Xiang Li 2013-07-10 10:31:56 -07:00
parent 7cfb6f554e
commit 62ddd377a0
4 changed files with 44 additions and 3 deletions

View File

@ -19,6 +19,7 @@ type AppendEntriesRequest struct {
// The response returned from a server appending entries to the log.
type AppendEntriesResponse struct {
Term uint64 `json:"term"`
Index uint64 `json:"index"`
Success bool `json:"success"`
CommitIndex uint64 `json:"commitIndex"`
}

View File

@ -0,0 +1,40 @@
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))
}

View File

@ -91,8 +91,8 @@ func (p *Peer) startHeartbeat() {
// Stops the peer heartbeat.
func (p *Peer) stopHeartbeat() {
// here is a problem
// the previous stop is no buffer leader may get blocked
// when heartbeat returns at line 132
// the previous stop is no buffer leader may get blocked
// when heartbeat returns at line 132
// I make the channel with 1 buffer
// and try to panic here
select {

View File

@ -721,7 +721,7 @@ func (s *Server) processAppendEntriesResponse(resp *AppendEntriesResponse) {
s.debugln("commit index ", commitIndex)
for i := committedIndex; i < commitIndex; i++ {
if entry := s.log.getEntry(i + 1); entry != nil {
// if the leader is a new one and the entry came from the
// if the leader is a new one and the entry came from the
// old leader, the commit channel will be nil and no go routine
// is waiting from this channel
// if we try to send to it, the new leader will get stuck