Skip to content

Commit b802ab8

Browse files
authored
Merge pull request #33 from gofiber/add_timestamp_calculator
Add timestamp calculation functionality
2 parents 8898e89 + 2de34c7 commit b802ab8

File tree

3 files changed

+168
-65
lines changed

3 files changed

+168
-65
lines changed

README.md

+89-65
Original file line numberDiff line numberDiff line change
@@ -10,72 +10,96 @@ A collection of common functions but with better performance, less allocations a
1010

1111
## Benchmarks
1212

13+
Environment:
14+
goos: darwin
15+
goarch: amd64
16+
pkg: github.com/gofiber/utils
17+
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
18+
1319
```go
1420
// go test -benchmem -run=^$ -bench=Benchmark_ -count=2
1521

16-
Benchmark_ToLowerBytes/fiber-16 51138252 22.61 ns/op 0 B/op 0 allocs/op
17-
Benchmark_ToLowerBytes/fiber-16 52126545 22.63 ns/op 0 B/op 0 allocs/op
18-
Benchmark_ToLowerBytes/default-16 16114736 72.76 ns/op 80 B/op 1 allocs/op
19-
Benchmark_ToLowerBytes/default-16 16651540 73.85 ns/op 80 B/op 1 allocs/op
20-
21-
Benchmark_ToUpperBytes/fiber-16 52127224 22.62 ns/op 0 B/op 0 allocs/op
22-
Benchmark_ToUpperBytes/fiber-16 54283167 22.86 ns/op 0 B/op 0 allocs/op
23-
Benchmark_ToUpperBytes/default-16 14060098 84.12 ns/op 80 B/op 1 allocs/op
24-
Benchmark_ToUpperBytes/default-16 14183122 84.51 ns/op 80 B/op 1 allocs/op
25-
26-
Benchmark_EqualFoldBytes/fiber-16 29240264 41.22 ns/op 0 B/op 0 allocs/op
27-
Benchmark_EqualFoldBytes/fiber-16 28535826 40.84 ns/op 0 B/op 0 allocs/op
28-
Benchmark_EqualFoldBytes/default-16 7929867 150.2 ns/op 0 B/op 0 allocs/op
29-
Benchmark_EqualFoldBytes/default-16 7935478 149.7 ns/op 0 B/op 0 allocs/op
30-
31-
Benchmark_EqualFold/fiber-16 35442768 34.25 ns/op 0 B/op 0 allocs/op
32-
Benchmark_EqualFold/fiber-16 35946870 34.96 ns/op 0 B/op 0 allocs/op
33-
Benchmark_EqualFold/default-16 8942130 133.5 ns/op 0 B/op 0 allocs/op
34-
Benchmark_EqualFold/default-16 8977231 134.3 ns/op 0 B/op 0 allocs/op
35-
36-
Benchmark_UUID/fiber-16 30726213 40.57 ns/op 48 B/op 1 allocs/op
37-
Benchmark_UUID/fiber-16 26539394 40.25 ns/op 48 B/op 1 allocs/op
38-
Benchmark_UUID/default-16 4737199 247.5 ns/op 168 B/op 6 allocs/op
39-
Benchmark_UUID/default-16 4603738 250.8 ns/op 168 B/op 6 allocs/op
40-
41-
Benchmark_ConvertToBytes/fiber-16 62450884 19.41 ns/op 0 B/op 0 allocs/op
42-
Benchmark_ConvertToBytes/fiber-16 52123602 19.53 ns/op 0 B/op 0 allocs/op
43-
44-
Benchmark_UnsafeString/unsafe-16 1000000000 0.4496 ns/op 0 B/op 0 allocs/op
45-
Benchmark_UnsafeString/unsafe-16 1000000000 0.4488 ns/op 0 B/op 0 allocs/op
46-
Benchmark_UnsafeString/default-16 79925935 13.99 ns/op 16 B/op 1 allocs/op
47-
Benchmark_UnsafeString/default-16 85637211 14.35 ns/op 16 B/op 1 allocs/op
48-
49-
Benchmark_UnsafeBytes/unsafe-16 540970148 2.214 ns/op 0 B/op 0 allocs/op
50-
Benchmark_UnsafeBytes/unsafe-16 543356940 2.212 ns/op 0 B/op 0 allocs/op
51-
Benchmark_UnsafeBytes/default-16 68896224 17.19 ns/op 16 B/op 1 allocs/op
52-
Benchmark_UnsafeBytes/default-16 70560426 17.05 ns/op 16 B/op 1 allocs/op
53-
54-
Benchmark_ToString-16 29504036 39.57 ns/op 40 B/op 2 allocs/op
55-
Benchmark_ToString-16 30738334 38.89 ns/op 40 B/op 2 allocs/op
56-
57-
Benchmark_GetMIME/fiber-16 28207086 41.84 ns/op 0 B/op 0 allocs/op
58-
Benchmark_GetMIME/fiber-16 28165773 41.83 ns/op 0 B/op 0 allocs/op
59-
Benchmark_GetMIME/default-16 12583132 94.04 ns/op 0 B/op 0 allocs/op
60-
Benchmark_GetMIME/default-16 12829614 93.50 ns/op 0 B/op 0 allocs/op
61-
62-
Benchmark_ParseVendorSpecificContentType/vendorContentType-16 30267411 38.72 ns/op 16 B/op 1 allocs/op
63-
Benchmark_ParseVendorSpecificContentType/vendorContentType-16 28543563 38.60 ns/op 16 B/op 1 allocs/op
64-
Benchmark_ParseVendorSpecificContentType/defaultContentType-16 249869286 4.830 ns/op 0 B/op 0 allocs/op
65-
Benchmark_ParseVendorSpecificContentType/defaultContentType-16 248999592 4.805 ns/op 0 B/op 0 allocs/op
66-
67-
Benchmark_StatusMessage/fiber-16 1000000000 0.6744 ns/op 0 B/op 0 allocs/op
68-
Benchmark_StatusMessage/fiber-16 1000000000 0.6788 ns/op 0 B/op 0 allocs/op
69-
Benchmark_StatusMessage/default-16 446818872 2.664 ns/op 0 B/op 0 allocs/op
70-
Benchmark_StatusMessage/default-16 447009616 2.661 ns/op 0 B/op 0 allocs/op
71-
72-
Benchmark_ToUpper/fiber-16 20480331 56.50 ns/op 80 B/op 1 allocs/op
73-
Benchmark_ToUpper/fiber-16 21541200 56.65 ns/op 80 B/op 1 allocs/op
74-
Benchmark_ToUpper/default-16 8433409 141.2 ns/op 80 B/op 1 allocs/op
75-
Benchmark_ToUpper/default-16 8473737 141.1 ns/op 80 B/op 1 allocs/op
76-
77-
Benchmark_ToLower/fiber-16 27248326 44.68 ns/op 80 B/op 1 allocs/op
78-
Benchmark_ToLower/fiber-16 26918443 44.70 ns/op 80 B/op 1 allocs/op
79-
Benchmark_ToLower/default-16 8447336 141.9 ns/op 80 B/op 1 allocs/op
80-
Benchmark_ToLower/default-16 8423156 140.6 ns/op 80 B/op 1 allocs/op
22+
Benchmark_ToLowerBytes/fiber-12 29715831 36.44 ns/op 0 B/op 0 allocs/op
23+
Benchmark_ToLowerBytes/fiber-12 33316479 36.28 ns/op 0 B/op 0 allocs/op
24+
Benchmark_ToLowerBytes/default-12 11894427 96.98 ns/op 80 B/op 1 allocs/op
25+
Benchmark_ToLowerBytes/default-12 12217050 97.43 ns/op 80 B/op 1 allocs/op
26+
27+
Benchmark_ToUpperBytes/fiber-12 22042162 46.92 ns/op 0 B/op 0 allocs/op
28+
Benchmark_ToUpperBytes/fiber-12 25859680 46.43 ns/op 0 B/op 0 allocs/op
29+
Benchmark_ToUpperBytes/default-12 10015346 117.2 ns/op 80 B/op 1 allocs/op
30+
Benchmark_ToUpperBytes/default-12 10185375 117.8 ns/op 80 B/op 1 allocs/op
31+
32+
Benchmark_EqualFoldBytes/fiber-12 22944849 47.14 ns/op 0 B/op 0 allocs/op
33+
Benchmark_EqualFoldBytes/fiber-12 26006342 46.82 ns/op 0 B/op 0 allocs/op
34+
Benchmark_EqualFoldBytes/default-12 5222006 222.5 ns/op 0 B/op 0 allocs/op
35+
Benchmark_EqualFoldBytes/default-12 5349398 223.2 ns/op 0 B/op 0 allocs/op
36+
37+
Benchmark_EqualFold/fiber-12 24761037 48.63 ns/op 0 B/op 0 allocs/op
38+
Benchmark_EqualFold/fiber-12 24159073 48.63 ns/op 0 B/op 0 allocs/op
39+
Benchmark_EqualFold/default-12 6322188 191.5 ns/op 0 B/op 0 allocs/op
40+
Benchmark_EqualFold/default-12 6319070 193.5 ns/op 0 B/op 0 allocs/op
41+
42+
Benchmark_UUID/fiber-12 22061482 49.13 ns/op 48 B/op 1 allocs/op
43+
Benchmark_UUID/fiber-12 24123198 48.40 ns/op 48 B/op 1 allocs/op
44+
Benchmark_UUID/default-12 3581961 336.9 ns/op 168 B/op 6 allocs/op
45+
Benchmark_UUID/default-12 3465946 344.8 ns/op 168 B/op 6 allocs/op
46+
47+
Benchmark_ConvertToBytes/fiber-12 53392819 23.19 ns/op 0 B/op 0 allocs/op
48+
Benchmark_ConvertToBytes/fiber-12 51117225 23.32 ns/op 0 B/op 0 allocs/op
49+
50+
Benchmark_UnsafeString/unsafe-12 1000000000 0.5672 ns/op 0 B/op 0 allocs/op
51+
Benchmark_UnsafeString/unsafe-12 1000000000 0.5683 ns/op 0 B/op 0 allocs/op
52+
Benchmark_UnsafeString/default-12 64000897 18.45 ns/op 16 B/op 1 allocs/op
53+
Benchmark_UnsafeString/default-12 64138909 18.13 ns/op 16 B/op 1 allocs/op
54+
55+
Benchmark_UnsafeBytes/unsafe-12 474777096 2.539 ns/op 0 B/op 0 allocs/op
56+
Benchmark_UnsafeBytes/unsafe-12 469340781 2.535 ns/op 0 B/op 0 allocs/op
57+
Benchmark_UnsafeBytes/default-12 53125656 22.15 ns/op 16 B/op 1 allocs/op
58+
Benchmark_UnsafeBytes/default-12 52615048 22.33 ns/op 16 B/op 1 allocs/op
59+
60+
Benchmark_ToString-12 22981430 51.72 ns/op 40 B/op 2 allocs/op
61+
Benchmark_ToString-12 22956476 52.93 ns/op 40 B/op 2 allocs/op
62+
63+
Benchmark_GetMIME/fiber-12 15782622 74.99 ns/op 0 B/op 0 allocs/op
64+
Benchmark_GetMIME/fiber-12 13992375 93.13 ns/op 0 B/op 0 allocs/op
65+
Benchmark_GetMIME/default-12 6825952 147.0 ns/op 0 B/op 0 allocs/op
66+
Benchmark_GetMIME/default-12 9158227 132.5 ns/op 0 B/op 0 allocs/op
67+
68+
ParseVendorSpecificContentType
69+
Benchmark_Parse.../vendorContentType-12 21334663 50.24 ns/op 16 B/op 1 allocs/op
70+
Benchmark_Parse.../vendorContentType-12 23121808 51.20 ns/op 16 B/op 1 allocs/op
71+
Benchmark_Parse.../defaultContentType-12 154423909 6.772 ns/op 0 B/op 0 allocs/op
72+
Benchmark_Parse.../defaultContentType-12 183285117 6.662 ns/op 0 B/op 0 allocs/op
73+
74+
Benchmark_StatusMessage/fiber-12 1000000000 0.9796 ns/op 0 B/op 0 allocs/op
75+
Benchmark_StatusMessage/fiber-12 1000000000 0.9706 ns/op 0 B/op 0 allocs/op
76+
Benchmark_StatusMessage/default-12 380260562 2.989 ns/op 0 B/op 0 allocs/op
77+
Benchmark_StatusMessage/default-12 403639642 3.124 ns/op 0 B/op 0 allocs/op
78+
79+
Benchmark_IsIPv4/fiber-12 53576214 21.07 ns/op 0 B/op 0 allocs/op
80+
Benchmark_IsIPv4/fiber-12 62672907 22.04 ns/op 0 B/op 0 allocs/op
81+
Benchmark_IsIPv4/default-12 21204613 62.23 ns/op 16 B/op 1 allocs/op
82+
Benchmark_IsIPv4/default-12 21399847 56.61 ns/op 16 B/op 1 allocs/op
83+
84+
Benchmark_IsIPv6/fiber-12 16754995 72.46 ns/op 0 B/op 0 allocs/op
85+
Benchmark_IsIPv6/fiber-12 17080897 74.31 ns/op 0 B/op 0 allocs/op
86+
Benchmark_IsIPv6/default-12 8160195 124.5 ns/op 16 B/op 1 allocs/op
87+
Benchmark_IsIPv6/default-12 9415326 119.8 ns/op 16 B/op 1 allocs/op
88+
89+
Benchmark_ToUpper/fiber-12 13175154 81.67 ns/op 80 B/op 1 allocs/op
90+
Benchmark_ToUpper/fiber-12 14285533 77.27 ns/op 80 B/op 1 allocs/op
91+
Benchmark_ToUpper/default-12 5332206 231.8 ns/op 80 B/op 1 allocs/op
92+
Benchmark_ToUpper/default-12 5364650 236.0 ns/op 80 B/op 1 allocs/op
93+
94+
Benchmark_ToLower/fiber-12 12996409 80.24 ns/op 80 B/op 1 allocs/op
95+
Benchmark_ToLower/fiber-12 16539536 69.27 ns/op 80 B/op 1 allocs/op
96+
Benchmark_ToLower/default-12 5132185 222.5 ns/op 80 B/op 1 allocs/op
97+
Benchmark_ToLower/default-12 5158561 225.3 ns/op 80 B/op 1 allocs/op
98+
99+
Benchmark_CalculateTimestamp/fiber-12 1000000000 0.2634 ns/op 0 B/op 0 allocs/op
100+
Benchmark_CalculateTimestamp/fiber-12 1000000000 0.2935 ns/op 0 B/op 0 allocs/op
101+
Benchmark_CalculateTimestamp/default-12 15740576 73.79 ns/op 0 B/op 0 allocs/op
102+
Benchmark_CalculateTimestamp/default-12 15789036 71.12 ns/op 0 B/op 0 allocs/op
81103
```
104+
105+
See all the benchmarks under https://gofiber.github.io/utils/

time.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package utils
2+
3+
import (
4+
"sync"
5+
"sync/atomic"
6+
"time"
7+
)
8+
9+
var (
10+
timestampTimer sync.Once
11+
// Timestamp please start the timer function before you use this value
12+
// please load the value with atomic `atomic.LoadUint32(&utils.Timestamp)`
13+
Timestamp uint32
14+
)
15+
16+
// StartTimeStampUpdater starts a concurrent function which stores the timestamp to an atomic value per second,
17+
// which is much better for performance than determining it at runtime each time
18+
func StartTimeStampUpdater() {
19+
timestampTimer.Do(func() {
20+
// set initial value
21+
atomic.StoreUint32(&Timestamp, uint32(time.Now().Unix()))
22+
go func(sleep time.Duration) {
23+
ticker := time.NewTicker(sleep)
24+
defer ticker.Stop()
25+
26+
for t := range ticker.C {
27+
// update timestamp
28+
atomic.StoreUint32(&Timestamp, uint32(t.Unix()))
29+
}
30+
}(1 * time.Second) // duration
31+
})
32+
}

time_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package utils
2+
3+
import (
4+
"sync/atomic"
5+
"testing"
6+
"time"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func checkTimeStamp(t testing.TB, expectedCurrent, actualCurrent uint32) {
12+
// test with some buffer in front and back of the expectedCurrent time -> because of the timing on the work machine
13+
require.Equal(t, true, actualCurrent >= expectedCurrent-1 || actualCurrent <= expectedCurrent+1)
14+
}
15+
16+
func Test_TimeStampUpdater(t *testing.T) {
17+
t.Parallel()
18+
19+
StartTimeStampUpdater()
20+
21+
now := uint32(time.Now().Unix())
22+
checkTimeStamp(t, now, atomic.LoadUint32(&Timestamp))
23+
// one second later
24+
time.Sleep(1 * time.Second)
25+
checkTimeStamp(t, now+1, atomic.LoadUint32(&Timestamp))
26+
// two seconds later
27+
time.Sleep(1 * time.Second)
28+
checkTimeStamp(t, now+2, atomic.LoadUint32(&Timestamp))
29+
}
30+
31+
func Benchmark_CalculateTimestamp(b *testing.B) {
32+
StartTimeStampUpdater()
33+
34+
var res uint32
35+
b.Run("fiber", func(b *testing.B) {
36+
for n := 0; n < b.N; n++ {
37+
res = atomic.LoadUint32(&Timestamp)
38+
}
39+
checkTimeStamp(b, uint32(time.Now().Unix()), res)
40+
})
41+
b.Run("default", func(b *testing.B) {
42+
for n := 0; n < b.N; n++ {
43+
res = uint32(time.Now().Unix())
44+
}
45+
checkTimeStamp(b, uint32(time.Now().Unix()), res)
46+
})
47+
}

0 commit comments

Comments
 (0)