-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlarge_file_test.go
75 lines (69 loc) · 1.49 KB
/
large_file_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
package go_benchmark
import (
"testing"
"github.com/json-iterator/go"
"os"
"encoding/json"
"github.com/buger/jsonparser"
"io/ioutil"
)
func Test_jsonparser_skip(t *testing.T) {
file, _ := os.Open("/tmp/large-file.json")
bytes, _ := ioutil.ReadAll(file)
file.Close()
total := 0
jsonparser.ArrayEach(bytes, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
total++
})
if total != 11351 {
t.Fatal(total)
}
}
func Test_jsoniter_skip(t *testing.T) {
for i := 0; i < 100; i++ {
file, _ := os.Open("/tmp/large-file.json")
iter := jsoniter.Parse(file, 4096)
total := 0
for iter.ReadArray() {
iter.Skip()
total++
}
file.Close()
if total != 11351 {
t.Fatal(total)
}
}
}
func Benchmark_jsonparser(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
file, _ := os.Open("/tmp/large-file.json")
bytes, _ := ioutil.ReadAll(file)
file.Close()
total := 0
jsonparser.ArrayEach(bytes, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
total++
})
}
}
func Benchmark_stardard_lib(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
file, _ := os.Open("/tmp/large-file.json")
result := []struct{}{}
decoder := json.NewDecoder(file)
decoder.Decode(&result)
file.Close()
}
}
func Benchmark_jsoniter(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
file, _ := os.Open("/tmp/large-file.json")
iter := jsoniter.Parse(file, 4096)
for iter.ReadArray() {
iter.Skip()
}
file.Close()
}
}