File: bench_test.go

package info (click to toggle)
golang-toml 1.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,904 kB
  • sloc: makefile: 4
file content (218 lines) | stat: -rw-r--r-- 4,476 bytes parent folder | download | duplicates (2)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//go:build go1.16
// +build go1.16

package toml_test

import (
	"bytes"
	"io/fs"
	"io/ioutil"
	"os"
	"path/filepath"
	"sort"
	"strings"
	"testing"
	"time"

	"github.com/BurntSushi/toml"
	tomltest "github.com/BurntSushi/toml/internal/toml-test"
)

func BenchmarkDecode(b *testing.B) {
	files := make(map[string][]string)
	fs.WalkDir(tomltest.EmbeddedTests(), ".", func(path string, d fs.DirEntry, err error) error {
		if strings.HasPrefix(path, "valid/") && strings.HasSuffix(path, ".toml") {
			d, _ := fs.ReadFile(tomltest.EmbeddedTests(), path)
			g := filepath.Dir(path[6:])
			if g == "." {
				g = "top"
			}
			files[g] = append(files[g], string(d))
		}
		return nil
	})

	type test struct {
		group string
		toml  []string
	}
	tests := make([]test, 0, len(files))
	for k, v := range files {
		tests = append(tests, test{group: k, toml: v})
	}
	sort.Slice(tests, func(i, j int) bool { return tests[i].group < tests[j].group })

	b.ResetTimer()
	for _, tt := range tests {
		b.Run(tt.group, func(b *testing.B) {
			b.ResetTimer()
			for n := 0; n < b.N; n++ {
				for _, f := range tt.toml {
					var val map[string]interface{}
					toml.Decode(f, &val)
				}
			}
		})
	}

	b.Run("large-doc", func(b *testing.B) {
		d, err := os.ReadFile("testdata/ja-JP.toml")
		if err != nil {
			b.Fatal(err)
		}
		doc := string(d)

		b.ResetTimer()
		for n := 0; n < b.N; n++ {
			var val map[string]interface{}
			toml.Decode(doc, &val)
		}
	})
}

func BenchmarkEncode(b *testing.B) {
	files := make(map[string][]map[string]interface{})
	fs.WalkDir(tomltest.EmbeddedTests(), ".", func(path string, d fs.DirEntry, err error) error {
		if strings.HasPrefix(path, "valid/") && strings.HasSuffix(path, ".toml") {
			d, _ := fs.ReadFile(tomltest.EmbeddedTests(), path)
			g := filepath.Dir(path[6:])
			if g == "." {
				g = "top"
			}

			// "next" version of TOML.
			switch path {
			case "valid/string/escape-esc.toml", "valid/datetime/no-seconds.toml",
				"valid/string/hex-escape.toml", "valid/inline-table/newline.toml",
				"valid/key/unicode.toml":
				return nil
			}

			var dec map[string]interface{}
			_, err := toml.Decode(string(d), &dec)
			if err != nil {
				b.Fatalf("decode %q: %s", path, err)
			}

			buf := new(bytes.Buffer)
			err = toml.NewEncoder(buf).Encode(dec)
			if err != nil {
				b.Logf("encode failed for %q (skipping): %s", path, err)
				return nil
			}

			files[g] = append(files[g], dec)
		}
		return nil
	})

	type test struct {
		group string
		data  []map[string]interface{}
	}
	tests := make([]test, 0, len(files))
	for k, v := range files {
		tests = append(tests, test{group: k, data: v})
	}
	sort.Slice(tests, func(i, j int) bool { return tests[i].group < tests[j].group })

	b.ResetTimer()
	for _, tt := range tests {
		b.Run(tt.group, func(b *testing.B) {
			buf := new(bytes.Buffer)
			buf.Grow(1024 * 64)
			b.ResetTimer()
			for n := 0; n < b.N; n++ {
				for _, f := range tt.data {
					toml.NewEncoder(buf).Encode(f)
				}
			}
		})
	}
}

func BenchmarkExample(b *testing.B) {
	d, err := ioutil.ReadFile("_example/example.toml")
	if err != nil {
		b.Fatal(err)
	}
	t := string(d)

	var decoded example
	_, err = toml.Decode(t, &decoded)
	if err != nil {
		b.Fatal(err)
	}

	buf := new(bytes.Buffer)
	err = toml.NewEncoder(buf).Encode(decoded)
	if err != nil {
		b.Fatal(err)
	}

	b.ResetTimer()
	b.Run("decode", func(b *testing.B) {
		for n := 0; n < b.N; n++ {
			var c example
			toml.Decode(t, &c)
		}
	})

	b.Run("encode", func(b *testing.B) {
		for n := 0; n < b.N; n++ {
			buf.Reset()
			toml.NewEncoder(buf).Encode(decoded)
		}
	})
}

// Copy from _example/example.go
type (
	example struct {
		Title      string
		Integers   []int
		Times      []fmtTime
		Duration   []duration
		Distros    []distro
		Servers    map[string]server
		Characters map[string][]struct {
			Name string
			Rank string
		}
	}

	server struct {
		IP       string
		Hostname string
		Enabled  bool
	}

	distro struct {
		Name     string
		Packages string
	}

	duration struct{ time.Duration }
	fmtTime  struct{ time.Time }
)

func (d *duration) UnmarshalText(text []byte) (err error) {
	d.Duration, err = time.ParseDuration(string(text))
	return err
}

func (t fmtTime) String() string {
	f := "2006-01-02 15:04:05.999999999"
	if t.Time.Hour() == 0 {
		f = "2006-01-02"
	}
	if t.Time.Year() == 0 {
		f = "15:04:05.999999999"
	}
	if t.Time.Location() == time.UTC {
		f += " UTC"
	} else {
		f += " -0700"
	}
	return t.Time.Format(`"` + f + `"`)
}