@ -2,13 +2,14 @@ package cache
import (
"bytes"
"github.com/stretchr/testify/assert"
"io/ioutil"
"runtime"
"strconv"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type TestStruct struct {
@ -346,15 +347,15 @@ func TestSerializeUnserializable(t *testing.T) {
}
}
func BenchmarkCacheGet Expiring( b * testing . B ) {
benchmarkCacheGet ( b , 5 * time . Minute )
func BenchmarkCacheGet String Expiring( b * testing . B ) {
benchmarkCacheGet String ( b , 5 * time . Minute )
}
func BenchmarkCacheGet NotExpiring( b * testing . B ) {
benchmarkCacheGet ( b , NoExpiration )
func BenchmarkCacheGet String NotExpiring( b * testing . B ) {
benchmarkCacheGet String ( b , NoExpiration )
}
func benchmarkCacheGet ( b * testing . B , exp time . Duration ) {
func benchmarkCacheGet String ( b * testing . B , exp time . Duration ) {
b . StopTimer ( )
tc := New [ string ] ( exp , 0 )
tc . Set ( "foo" , "bar" , DefaultExpiration )
@ -495,15 +496,15 @@ func benchmarkCacheGetManyConcurrent(b *testing.B, exp time.Duration) {
wg . Wait ( )
}
func BenchmarkCacheSet Expiring( b * testing . B ) {
benchmarkCacheSet ( b , 5 * time . Minute )
func BenchmarkCacheSet String Expiring( b * testing . B ) {
benchmarkCacheSet String ( b , 5 * time . Minute )
}
func BenchmarkCacheSet NotExpiring( b * testing . B ) {
benchmarkCacheSet ( b , NoExpiration )
func BenchmarkCacheSet String NotExpiring( b * testing . B ) {
benchmarkCacheSet String ( b , NoExpiration )
}
func benchmarkCacheSet ( b * testing . B , exp time . Duration ) {
func benchmarkCacheSet String ( b * testing . B , exp time . Duration ) {
b . StopTimer ( )
tc := New [ string ] ( exp , 0 )
b . StartTimer ( )
@ -638,3 +639,67 @@ func TestGetWithExpiration(t *testing.T) {
assert . Equal ( t , expiration . UnixNano ( ) , tc . items [ "e" ] . Expiration , "expiration for e is not the correct time" )
assert . Greater ( t , expiration . UnixNano ( ) , time . Now ( ) . UnixNano ( ) , "expiration for e is in the past" )
}
// Benchmark struct
func BenchmarkCacheGetStructExpiring ( b * testing . B ) {
benchmarkCacheGetStruct ( b , 5 * time . Minute )
}
func BenchmarkCacheGetStructNotExpiring ( b * testing . B ) {
benchmarkCacheGetStruct ( b , NoExpiration )
}
func benchmarkCacheGetStruct ( b * testing . B , exp time . Duration ) {
b . StopTimer ( )
tc := New [ * TestStruct ] ( exp , 0 )
tc . Set ( "foo" , & TestStruct { Num : 1 } , DefaultExpiration )
b . StartTimer ( )
for i := 0 ; i < b . N ; i ++ {
st , _ := tc . Get ( "foo" )
// just do something
st . Num ++
}
}
func BenchmarkCacheSetStructExpiring ( b * testing . B ) {
benchmarkCacheSetStruct ( b , 5 * time . Minute )
}
func BenchmarkCacheSetStructNotExpiring ( b * testing . B ) {
benchmarkCacheSetStruct ( b , NoExpiration )
}
func benchmarkCacheSetStruct ( b * testing . B , exp time . Duration ) {
b . StopTimer ( )
tc := New [ * TestStruct ] ( exp , 0 )
b . StartTimer ( )
for i := 0 ; i < b . N ; i ++ {
tc . Set ( "foo" , & TestStruct { Num : 1 } , DefaultExpiration )
}
}
func BenchmarkCacheSetFatStructExpiring ( b * testing . B ) {
b . StopTimer ( )
tc := New [ * TestStruct ] ( NoExpiration , 0 )
b . StartTimer ( )
for i := 0 ; i < b . N ; i ++ {
tc . Set ( "foo" , & TestStruct { Num : 1 , Children : [ ] * TestStruct {
& TestStruct { Num : 2 , Children : [ ] * TestStruct { } } } } , DefaultExpiration )
}
}
func BenchmarkCacheGetFatStructNotExpiring ( b * testing . B ) {
b . StopTimer ( )
tc := New [ * TestStruct ] ( NoExpiration , 0 )
tc . Set ( "foo" , & TestStruct { Num : 1 , Children : [ ] * TestStruct {
& TestStruct { Num : 2 , Children : [ ] * TestStruct { } } } } , DefaultExpiration )
b . StartTimer ( )
for i := 0 ; i < b . N ; i ++ {
st , _ := tc . Get ( "foo" )
// just do something
st . Num ++
}
}