File: bench_test.go

package info (click to toggle)
golang-github-lestrrat-go-strftime 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 180 kB
  • sloc: makefile: 24
file content (103 lines) | stat: -rw-r--r-- 2,082 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
103
package bench_test

import (
	"bytes"
	"log"
	"net/http"
	_ "net/http/pprof"
	"testing"
	"time"

	fastly "github.com/fastly/go-utils/strftime"
	jehiah "github.com/jehiah/go-strftime"
	lestrrat "github.com/lestrrat-go/strftime"
	ncruces "github.com/ncruces/go-strftime"
	tebeka "github.com/tebeka/strftime"
)

func init() {
	go func() {
		log.Println(http.ListenAndServe("localhost:8080", nil))
	}()
}

const benchfmt = `%A %a %B %b %d %H %I %M %m %p %S %Y %y %Z`

func BenchmarkTebeka(b *testing.B) {
	var t time.Time
	for i := 0; i < b.N; i++ {
		tebeka.Format(benchfmt, t)
	}
}

func BenchmarkJehiah(b *testing.B) {
	// Grr, uses byte slices, and does it faster, but with more allocs
	var t time.Time
	for i := 0; i < b.N; i++ {
		jehiah.Format(benchfmt, t)
	}
}

func BenchmarkFastly(b *testing.B) {
	var t time.Time
	for i := 0; i < b.N; i++ {
		fastly.Strftime(benchfmt, t)
	}
}

func BenchmarkNcruces(b *testing.B) {
	var t time.Time
	for i := 0; i < b.N; i++ {
		ncruces.Format(benchfmt, t)
	}
}

func BenchmarkNcrucesAppend(b *testing.B) {
	var d []byte
	var t time.Time
	for i := 0; i < b.N; i++ {
		d = ncruces.AppendFormat(d[:0], benchfmt, t)
	}
}

func BenchmarkLestrrat(b *testing.B) {
	// This is expected to be rather slow, as it compiles the format string
	// every time it's called.
	var t time.Time
	for i := 0; i < b.N; i++ {
		lestrrat.Format(benchfmt, t)
	}
}

func BenchmarkLestrratCachedString(b *testing.B) {
	var t time.Time
	f, _ := lestrrat.New(benchfmt)
	// This benchmark does not take into effect the compilation time
	for i := 0; i < b.N; i++ {
		f.FormatString(t)
	}
}

func BenchmarkLestrratCachedWriter(b *testing.B) {
	var t time.Time
	f, _ := lestrrat.New(benchfmt)
	var buf bytes.Buffer
	b.ResetTimer()

	// This benchmark does not take into effect the compilation time
	for i := 0; i < b.N; i++ {
		buf.Reset()
		f.Format(&buf, t)
	}
}

func BenchmarkLestrratCachedFormatBuffer(b *testing.B) {
	var t time.Time
	f, _ := lestrrat.New(benchfmt)
	b.ResetTimer()

	var buf []byte
	for i := 0; i < b.N; i++ {
		buf = f.FormatBuffer(buf[:0], t)
	}
}