File: bench_test.go

package info (click to toggle)
golang-sourcehut-sircmpwn-go-bare 0.0~git20210406.ab86bc2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 268 kB
  • sloc: makefile: 5
file content (49 lines) | stat: -rw-r--r-- 769 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
package example

import (
	"io/ioutil"
	"testing"

	"git.sr.ht/~sircmpwn/go-bare"
	"github.com/stretchr/testify/assert"
)

func BenchmarkMarshal(b *testing.B) {
	person, _ := makeCustomer(b)
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		_, err := bare.Marshal(person)
		if err != nil {
			panic(err)
		}
	}

}

func BenchmarkUnmarshal(b *testing.B) {
	_, buf := makeCustomer(b)
	var person Person
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		err := bare.Unmarshal(buf, &person)
		if err != nil {
			panic(err)
		}
	}

}

func makeCustomer(b *testing.B) (Person, []byte) {
	buf, err := ioutil.ReadFile("customer.bin")
	assert.Nil(b, err)

	b.SetBytes(int64(len(buf)))

	var person Person
	err = bare.Unmarshal(buf, &person)
	assert.Nil(b, err)

	return person, buf
}