OSDN Git Service

log into file (#357)
[bytom/vapor.git] / vendor / github.com / lestrrat-go / strftime / strftime_bench_test.go
1 // +build bench
2
3 package strftime_test
4
5 import (
6         "bytes"
7         "log"
8         "net/http"
9         _ "net/http/pprof"
10         "testing"
11         "time"
12
13         jehiah "github.com/jehiah/go-strftime"
14         fastly "github.com/fastly/go-utils/strftime"
15         lestrrat "github.com/lestrrat-go/strftime"
16         tebeka "github.com/tebeka/strftime"
17 )
18
19 func init() {
20         go func() {
21                 log.Println(http.ListenAndServe("localhost:8080", nil))
22         }()
23 }
24
25 const benchfmt = `%A %a %B %b %d %H %I %M %m %p %S %Y %y %Z`
26
27 func BenchmarkTebeka(b *testing.B) {
28         var t time.Time
29         for i := 0; i < b.N; i++ {
30                 tebeka.Format(benchfmt, t)
31         }
32 }
33
34 func BenchmarkJehiah(b *testing.B) {
35         // Grr, uses byte slices, and does it faster, but with more allocs
36         var t time.Time
37         for i := 0; i < b.N; i++ {
38                 jehiah.Format(benchfmt, t)
39         }
40 }
41
42 func BenchmarkFastly(b *testing.B) {
43         var t time.Time
44         for i := 0; i < b.N; i++ {
45                 fastly.Strftime(benchfmt, t)
46         }
47 }
48
49 func BenchmarkLestrrat(b *testing.B) {
50         var t time.Time
51         for i := 0; i < b.N; i++ {
52                 lestrrat.Format(benchfmt, t)
53         }
54 }
55
56 func BenchmarkLestrratCachedString(b *testing.B) {
57         var t time.Time
58         f, _ := lestrrat.New(benchfmt)
59         // This benchmark does not take into effect the compilation time
60         for i := 0; i < b.N; i++ {
61                 f.FormatString(t)
62         }
63 }
64
65 func BenchmarkLestrratCachedWriter(b *testing.B) {
66         var t time.Time
67         f, _ := lestrrat.New(benchfmt)
68         var buf bytes.Buffer
69         b.ResetTimer()
70
71         // This benchmark does not take into effect the compilation time
72         // nor the buffer reset time
73         for i := 0; i < b.N; i++ {
74                 b.StopTimer()
75                 buf.Reset()
76                 b.StartTimer()
77                 f.Format(&buf, t)
78                 f.FormatString(t)
79         }
80 }