-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbenchmark_comp_test.go
70 lines (57 loc) · 1.36 KB
/
benchmark_comp_test.go
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
// Copyright 2018 Timon Wong. All rights reserved.
// +build benchcomp
package strftime_test
import (
"testing"
"time"
fastly "github.com/fastly/go-utils/strftime"
imperfectgo "github.com/imperfectgo/go-strftime"
jehiah "github.com/jehiah/go-strftime"
lestrrat "github.com/lestrrat-go/strftime"
tebeka "github.com/tebeka/strftime"
)
const (
benchfmt = `%A %a %B %b %d %H %I %M %m %p %S %Y %y %Z`
)
var (
now = time.Now().UTC()
)
func BenchmarkImperfectGo(b *testing.B) {
for i := 0; i < b.N; i++ {
imperfectgo.Format(now, benchfmt)
}
}
func BenchmarkImperfectGoNoAlloc(b *testing.B) {
var buf [128]byte
for i := 0; i < b.N; i++ {
imperfectgo.AppendFormat(buf[:0], now, benchfmt)
}
}
func BenchmarkTebeka(b *testing.B) {
for i := 0; i < b.N; i++ {
tebeka.Format(benchfmt, now)
}
}
func BenchmarkJehiah(b *testing.B) {
// Grr, uses byte slices, and does it faster, but with more allocs
for i := 0; i < b.N; i++ {
jehiah.Format(benchfmt, now)
}
}
func BenchmarkFastly(b *testing.B) {
for i := 0; i < b.N; i++ {
fastly.Strftime(benchfmt, now)
}
}
func BenchmarkLestrrat(b *testing.B) {
for i := 0; i < b.N; i++ {
lestrrat.Format(benchfmt, now)
}
}
func BenchmarkLestrratCachedString(b *testing.B) {
f, _ := lestrrat.New(benchfmt)
// This benchmark does not take into effect the compilation time
for i := 0; i < b.N; i++ {
f.FormatString(now)
}
}