|
| 1 | +// Copyright 2018 Timon Wong. All rights reserved. |
| 2 | + |
| 3 | +// +build bench |
| 4 | + |
| 5 | +package strftime_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + fastly "github.com/fastly/go-utils/strftime" |
| 12 | + imperfectgo "github.com/imperfectgo/go-strftime" |
| 13 | + jehiah "github.com/jehiah/go-strftime" |
| 14 | + lestrrat "github.com/lestrrat-go/strftime" |
| 15 | + tebeka "github.com/tebeka/strftime" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + benchfmt = `%A %a %B %b %d %H %I %M %m %p %S %Y %y %Z` |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + now = time.Now().UTC() |
| 24 | +) |
| 25 | + |
| 26 | +func BenchmarkImperfectGo(b *testing.B) { |
| 27 | + for i := 0; i < b.N; i++ { |
| 28 | + imperfectgo.Format(now, benchfmt) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func BenchmarkTebeka(b *testing.B) { |
| 33 | + for i := 0; i < b.N; i++ { |
| 34 | + tebeka.Format(benchfmt, now) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func BenchmarkJehiah(b *testing.B) { |
| 39 | + // Grr, uses byte slices, and does it faster, but with more allocs |
| 40 | + for i := 0; i < b.N; i++ { |
| 41 | + jehiah.Format(benchfmt, now) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func BenchmarkFastly(b *testing.B) { |
| 46 | + for i := 0; i < b.N; i++ { |
| 47 | + fastly.Strftime(benchfmt, now) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func BenchmarkLestrrat(b *testing.B) { |
| 52 | + for i := 0; i < b.N; i++ { |
| 53 | + lestrrat.Format(benchfmt, now) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func BenchmarkLestrratCachedString(b *testing.B) { |
| 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(now) |
| 62 | + } |
| 63 | +} |
0 commit comments