append_entries_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package raft
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. func BenchmarkAppendEntriesRequestEncoding(b *testing.B) {
  7. req, tmp := createTestAppendEntriesRequest(2000)
  8. b.ResetTimer()
  9. for i := 0; i < b.N; i++ {
  10. var buf bytes.Buffer
  11. req.Encode(&buf)
  12. }
  13. b.SetBytes(int64(len(tmp)))
  14. }
  15. func BenchmarkAppendEntriesRequestDecoding(b *testing.B) {
  16. req, buf := createTestAppendEntriesRequest(2000)
  17. b.ResetTimer()
  18. for i := 0; i < b.N; i++ {
  19. req.Decode(bytes.NewReader(buf))
  20. }
  21. b.SetBytes(int64(len(buf)))
  22. }
  23. func BenchmarkAppendEntriesResponseEncoding(b *testing.B) {
  24. req, tmp := createTestAppendEntriesResponse(2000)
  25. b.ResetTimer()
  26. for i := 0; i < b.N; i++ {
  27. var buf bytes.Buffer
  28. req.Encode(&buf)
  29. }
  30. b.SetBytes(int64(len(tmp)))
  31. }
  32. func BenchmarkAppendEntriesResponseDecoding(b *testing.B) {
  33. req, buf := createTestAppendEntriesResponse(2000)
  34. b.ResetTimer()
  35. for i := 0; i < b.N; i++ {
  36. req.Decode(bytes.NewReader(buf))
  37. }
  38. b.SetBytes(int64(len(buf)))
  39. }
  40. func createTestAppendEntriesRequest(entryCount int) (*AppendEntriesRequest, []byte) {
  41. entries := make([]*LogEntry, 0)
  42. for i := 0; i < entryCount; i++ {
  43. command := &DefaultJoinCommand{Name: "localhost:1000"}
  44. entry, _ := newLogEntry(nil, nil, 1, 2, command)
  45. entries = append(entries, entry)
  46. }
  47. req := newAppendEntriesRequest(1, 1, 1, 1, "leader", entries)
  48. var buf bytes.Buffer
  49. req.Encode(&buf)
  50. return req, buf.Bytes()
  51. }
  52. func createTestAppendEntriesResponse(entryCount int) (*AppendEntriesResponse, []byte) {
  53. resp := newAppendEntriesResponse(1, true, 1, 1)
  54. var buf bytes.Buffer
  55. resp.Encode(&buf)
  56. return resp, buf.Bytes()
  57. }