-
Notifications
You must be signed in to change notification settings - Fork 87
/
marshal_test.go
97 lines (79 loc) · 1.9 KB
/
marshal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package zoekt
import (
"bytes"
"encoding/gob"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func BenchmarkRepoList_Encode(b *testing.B) {
set := genRepoList(1000)
// do one write to amortize away the cost of gob registration
w := &countWriter{}
enc := gob.NewEncoder(w)
if err := enc.Encode(set); err != nil {
b.Fatal(err)
}
b.ResetTimer()
b.ReportAllocs()
b.ReportMetric(float64(w.n), "bytes")
for n := 0; n < b.N; n++ {
if err := enc.Encode(set); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkRepoList_Decode(b *testing.B) {
set := genRepoList(1000)
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(set); err != nil {
b.Fatal(err)
}
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
// We need to include gob.NewDecoder cost to avoid measuring encoding.
var repoBranches RepoList
if err := gob.NewDecoder(bytes.NewReader(buf.Bytes())).Decode(&repoBranches); err != nil {
b.Fatal(err)
}
}
}
func TestRepoList_Marshal(t *testing.T) {
for i := range []int{0, 1, 10, 100} {
want := genRepoList(i)
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(want); err != nil {
t.Fatal(err)
}
var got RepoList
if err := gob.NewDecoder(bytes.NewReader(buf.Bytes())).Decode(&got); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(want, &got); diff != "" {
t.Fatalf("mismatch for reposmap size %d (-want +got):\n%s", i, diff)
}
}
}
func genRepoList(size int) *RepoList {
m := make(ReposMap, size)
indexTime := time.Now().Unix()
for i := 0; i < size; i++ {
m[uint32(i)] = MinimalRepoListEntry{
HasSymbols: true,
IndexTimeUnix: indexTime,
Branches: []RepositoryBranch{{
Name: "HEAD",
Version: "c301e5c82b6e1632dce5c39902691c359559852e",
}},
}
}
return &RepoList{ReposMap: m}
}
type countWriter struct {
n int
}
func (w *countWriter) Write(b []byte) (int, error) {
w.n += len(b)
return len(b), nil
}