File: logmap_test.go

package info (click to toggle)
golang-github-cactus-mlog 1.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 272 kB
  • sloc: makefile: 2
file content (51 lines) | stat: -rw-r--r-- 1,274 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
package mlog

import (
	"testing"

	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

type discardSliceWriter struct{}

func (d *discardSliceWriter) WriteString(s string) (int, error) { return len(s), nil }
func (d *discardSliceWriter) Write(b []byte) (int, error)       { return len(b), nil }
func (d *discardSliceWriter) WriteByte(c byte) error            { return nil }
func (d *discardSliceWriter) Truncate(i int)                    {}

func BenchmarkLogMapUnsortedWriteBuf(b *testing.B) {
	buf := &discardSliceWriter{}
	m := Map{}
	for i := 1; i <= 100; i++ {
		m[randString(10, false)] = randString(25, true)
	}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		m.unsortedWriteBuf(buf)
		buf.Truncate(0)
	}
}

func BenchmarkLogMapSortedWriteBuf(b *testing.B) {
	buf := &discardSliceWriter{}
	m := Map{}
	for i := 1; i <= 100; i++ {
		m[randString(10, false)] = randString(25, true)
	}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		m.sortedWriteBuf(buf)
		buf.Truncate(0)
	}
}

func TestLogMapWriteTo(t *testing.T) {
	m := Map{"test": "this is \"a test\" of \t some \n a"}
	buf := &sliceBuffer{make([]byte, 0, 1024)}
	m.sortedWriteBuf(buf)
	n := `test="this is \"a test\" of \t some \n a"`
	l := buf.String()
	assert.Check(t, is.Equal(n, l), "did not match")

}