...
1 package benchmark_test
2
3 import (
4 "compress/gzip"
5 "encoding/json"
6 "io/ioutil"
7 "os"
8 "path/filepath"
9 "testing"
10
11 "github.com/pelletier/go-toml/v2"
12 "github.com/stretchr/testify/require"
13 )
14
15 var bench_inputs = []struct {
16 name string
17 jsonLen int
18 }{
19
20 {"config", 806507},
21
22
23 {"canada", 2090234},
24 {"citm_catalog", 479897},
25 {"twitter", 428778},
26 {"code", 1940472},
27
28
29 {"example", 7779},
30 }
31
32 func TestUnmarshalDatasetCode(t *testing.T) {
33 for _, tc := range bench_inputs {
34 t.Run(tc.name, func(t *testing.T) {
35 buf := fixture(t, tc.name)
36
37 var v interface{}
38 require.NoError(t, toml.Unmarshal(buf, &v))
39
40 b, err := json.Marshal(v)
41 require.NoError(t, err)
42 require.Equal(t, len(b), tc.jsonLen)
43 })
44 }
45 }
46
47 func BenchmarkUnmarshalDataset(b *testing.B) {
48 for _, tc := range bench_inputs {
49 b.Run(tc.name, func(b *testing.B) {
50 buf := fixture(b, tc.name)
51 b.SetBytes(int64(len(buf)))
52 b.ReportAllocs()
53 b.ResetTimer()
54 for i := 0; i < b.N; i++ {
55 var v interface{}
56 require.NoError(b, toml.Unmarshal(buf, &v))
57 }
58 })
59 }
60 }
61
62
63 func fixture(tb testing.TB, path string) []byte {
64 tb.Helper()
65
66 file := path + ".toml.gz"
67 f, err := os.Open(filepath.Join("testdata", file))
68 if os.IsNotExist(err) {
69 tb.Skip("benchmark fixture not found:", file)
70 }
71 require.NoError(tb, err)
72 defer f.Close()
73
74 gz, err := gzip.NewReader(f)
75 require.NoError(tb, err)
76
77 buf, err := ioutil.ReadAll(gz)
78 require.NoError(tb, err)
79 return buf
80 }
81
View as plain text