-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from gofiber/add_timestamp_calculator
Add timestamp calculation functionality
- Loading branch information
Showing
3 changed files
with
168 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package utils | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
var ( | ||
timestampTimer sync.Once | ||
// Timestamp please start the timer function before you use this value | ||
// please load the value with atomic `atomic.LoadUint32(&utils.Timestamp)` | ||
Timestamp uint32 | ||
) | ||
|
||
// StartTimeStampUpdater starts a concurrent function which stores the timestamp to an atomic value per second, | ||
// which is much better for performance than determining it at runtime each time | ||
func StartTimeStampUpdater() { | ||
timestampTimer.Do(func() { | ||
// set initial value | ||
atomic.StoreUint32(&Timestamp, uint32(time.Now().Unix())) | ||
go func(sleep time.Duration) { | ||
ticker := time.NewTicker(sleep) | ||
defer ticker.Stop() | ||
|
||
for t := range ticker.C { | ||
// update timestamp | ||
atomic.StoreUint32(&Timestamp, uint32(t.Unix())) | ||
} | ||
}(1 * time.Second) // duration | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package utils | ||
|
||
import ( | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func checkTimeStamp(t testing.TB, expectedCurrent, actualCurrent uint32) { | ||
// test with some buffer in front and back of the expectedCurrent time -> because of the timing on the work machine | ||
require.Equal(t, true, actualCurrent >= expectedCurrent-1 || actualCurrent <= expectedCurrent+1) | ||
} | ||
|
||
func Test_TimeStampUpdater(t *testing.T) { | ||
t.Parallel() | ||
|
||
StartTimeStampUpdater() | ||
|
||
now := uint32(time.Now().Unix()) | ||
checkTimeStamp(t, now, atomic.LoadUint32(&Timestamp)) | ||
// one second later | ||
time.Sleep(1 * time.Second) | ||
checkTimeStamp(t, now+1, atomic.LoadUint32(&Timestamp)) | ||
// two seconds later | ||
time.Sleep(1 * time.Second) | ||
checkTimeStamp(t, now+2, atomic.LoadUint32(&Timestamp)) | ||
} | ||
|
||
func Benchmark_CalculateTimestamp(b *testing.B) { | ||
StartTimeStampUpdater() | ||
|
||
var res uint32 | ||
b.Run("fiber", func(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
res = atomic.LoadUint32(&Timestamp) | ||
} | ||
checkTimeStamp(b, uint32(time.Now().Unix()), res) | ||
}) | ||
b.Run("default", func(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
res = uint32(time.Now().Unix()) | ||
} | ||
checkTimeStamp(b, uint32(time.Now().Unix()), res) | ||
}) | ||
} |