File: bench_test.go

package info (click to toggle)
golang-github-komkom-toml 0.0~git20211215.3c8ee9d-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,072 kB
  • sloc: makefile: 2
file content (102 lines) | stat: -rw-r--r-- 1,495 bytes parent folder | download
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
package toml

import (
	"bytes"
	"io"
	"strconv"
	"strings"
	"testing"

	"github.com/pkg/errors"
	"github.com/stretchr/testify/require"
)

type TmplBuffer struct {
	buf     bytes.Buffer
	tmpl    string
	counter int
	stop    bool
	n       int64
}

func (t *TmplBuffer) fillBuffer() {

	for i := 0; i < 100; i++ {
		tmp := strings.Replace(t.tmpl, `$1`, strconv.Itoa(t.counter), -1)
		t.buf.WriteString(tmp)
		t.counter++
	}

}

func (t *TmplBuffer) Read(p []byte) (int, error) {

	for {
		n, err := t.buf.Read(p)
		if err != nil && !errors.Is(err, io.EOF) {
			return n, err
		}
		if t.stop && (errors.Is(err, io.EOF) || n == 0) {
			return n, io.EOF
		}

		if n == 0 {
			t.fillBuffer()
			continue
		}

		t.n += int64(n)

		return n, nil
	}
}

var (
	tmpl = `
	key1_$1 = "tt"
	key2_$1 = "tt"
	[table_$1]
	1=1.389740932847e101917
	2="some longer string"
	[[array_$1]]
	a.b.c.d.e = "test value"
	[[array_$1]]
	a.b.c.d.e = "test value"
	[[array_$1]]
	a.b = "test value"`
)

func BenchmarkParserThroughput(b *testing.B) {

	buf := &TmplBuffer{tmpl: tmpl}
	p := make([]byte, 128)
	rd := New(buf)

	var n int64
	for i := 0; i < b.N; i++ {

		rn, err := rd.Read(p)
		require.NoError(b, err)

		n += int64(rn)
	}

	b.SetBytes(buf.n / int64(b.N))
}

func BenchmarkMemoryThroughput(b *testing.B) {

	buf := &TmplBuffer{tmpl: tmpl}
	p := make([]byte, 128)

	var n int64
	for i := 0; i < b.N; i++ {

		rn, err := buf.Read(p)
		require.NoError(b, err)

		n += int64(rn)
	}

	b.SetBytes(buf.n / int64(b.N))
}