forked from wI2L/jsondiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_test.go
64 lines (58 loc) · 1.16 KB
/
hash_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
package jsondiff
import (
"encoding/json"
"hash/maphash"
"io/ioutil"
"testing"
)
func readJSON(filename string) (interface{}, error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var i interface{}
if err := json.Unmarshal(b, &i); err != nil {
return nil, err
}
return i, nil
}
func Test_digestValue(t *testing.T) {
data, err := readJSON("testdata/examples/twitter.json")
if err != nil {
t.Error(err)
}
h := hasher{}
n1 := h.digest(data)
n2 := h.digest(data)
if n1 != n2 {
t.Errorf("expected hash sums to be equal: %d != %d", n1, n2)
}
}
func BenchmarkHashing(b *testing.B) {
if testing.Short() {
b.Skip("skipping benchmark in short mode")
}
data, err := readJSON("testdata/examples/twitter.json")
if err != nil {
b.Error(err)
}
b.Run("hasher-digestValue", func(b *testing.B) {
h := hasher{}
for i := 0; i < b.N; i++ {
_ = h.digest(data)
}
})
b.Run("json.Marshal+hash", func(b *testing.B) {
for i := 0; i < b.N; i++ {
bts, err := json.Marshal(data)
if err != nil {
b.Error(err)
}
h := maphash.Hash{}
if _, err := h.Write(bts); err != nil {
b.Error(err)
}
_ = h.Sum64()
}
})
}