Skip to content

Commit a45ed98

Browse files
committed
Add benchmarks that use expiring items (time.Now calls) and rename BenchmarkDeleteExpired to BenchmarkDeleteExpiredLoop for clarity
1 parent 28ab885 commit a45ed98

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

cache_test.go

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,9 +1425,17 @@ func TestSerializeUnserializable(t *testing.T) {
14251425
}
14261426
}
14271427

1428-
func BenchmarkCacheGet(b *testing.B) {
1428+
func BenchmarkCacheGetExpiring(b *testing.B) {
1429+
benchmarkCacheGet(b, 5 * time.Minute)
1430+
}
1431+
1432+
func BenchmarkCacheGetNotExpiring(b *testing.B) {
1433+
benchmarkCacheGet(b, NoExpiration)
1434+
}
1435+
1436+
func benchmarkCacheGet(b *testing.B, exp time.Duration) {
14291437
b.StopTimer()
1430-
tc := New(DefaultExpiration, 0)
1438+
tc := New(exp, 0)
14311439
tc.Set("foo", "bar", DefaultExpiration)
14321440
b.StartTimer()
14331441
for i := 0; i < b.N; i++ {
@@ -1449,9 +1457,17 @@ func BenchmarkRWMutexMapGet(b *testing.B) {
14491457
}
14501458
}
14511459

1452-
func BenchmarkCacheGetConcurrent(b *testing.B) {
1460+
func BenchmarkCacheGetConcurrentExpiring(b *testing.B) {
1461+
benchmarkCacheGetConcurrent(b, 5 * time.Minute)
1462+
}
1463+
1464+
func BenchmarkCacheGetConcurrentNotExpiring(b *testing.B) {
1465+
benchmarkCacheGetConcurrent(b, NoExpiration)
1466+
}
1467+
1468+
func benchmarkCacheGetConcurrent(b *testing.B, exp time.Duration) {
14531469
b.StopTimer()
1454-
tc := New(DefaultExpiration, 0)
1470+
tc := New(exp, 0)
14551471
tc.Set("foo", "bar", DefaultExpiration)
14561472
wg := new(sync.WaitGroup)
14571473
workers := runtime.NumCPU()
@@ -1493,13 +1509,21 @@ func BenchmarkRWMutexMapGetConcurrent(b *testing.B) {
14931509
wg.Wait()
14941510
}
14951511

1496-
func BenchmarkCacheGetManyConcurrent(b *testing.B) {
1512+
func BenchmarkCacheGetManyConcurrentExpiring(b *testing.B) {
1513+
benchmarkCacheGetManyConcurrent(b, 5 * time.Minute)
1514+
}
1515+
1516+
func BenchmarkCacheGetManyConcurrentNotExpiring(b *testing.B) {
1517+
benchmarkCacheGetManyConcurrent(b, NoExpiration)
1518+
}
1519+
1520+
func benchmarkCacheGetManyConcurrent(b *testing.B, exp time.Duration) {
14971521
// This is the same as BenchmarkCacheGetConcurrent, but its result
14981522
// can be compared against BenchmarkShardedCacheGetManyConcurrent
14991523
// in sharded_test.go.
15001524
b.StopTimer()
15011525
n := 10000
1502-
tc := New(DefaultExpiration, 0)
1526+
tc := New(exp, 0)
15031527
keys := make([]string, n)
15041528
for i := 0; i < n; i++ {
15051529
k := "foo" + strconv.Itoa(n)
@@ -1521,9 +1545,17 @@ func BenchmarkCacheGetManyConcurrent(b *testing.B) {
15211545
wg.Wait()
15221546
}
15231547

1524-
func BenchmarkCacheSet(b *testing.B) {
1548+
func BenchmarkCacheSetExpiring(b *testing.B) {
1549+
benchmarkCacheSet(b, 5 * time.Minute)
1550+
}
1551+
1552+
func BenchmarkCacheSetNotExpiring(b *testing.B) {
1553+
benchmarkCacheSet(b, NoExpiration)
1554+
}
1555+
1556+
func benchmarkCacheSet(b *testing.B, exp time.Duration) {
15251557
b.StopTimer()
1526-
tc := New(DefaultExpiration, 0)
1558+
tc := New(exp, 0)
15271559
b.StartTimer()
15281560
for i := 0; i < b.N; i++ {
15291561
tc.Set("foo", "bar", DefaultExpiration)
@@ -1602,7 +1634,7 @@ func BenchmarkIncrementInt(b *testing.B) {
16021634
}
16031635
}
16041636

1605-
func BenchmarkDeleteExpired(b *testing.B) {
1637+
func BenchmarkDeleteExpiredLoop(b *testing.B) {
16061638
b.StopTimer()
16071639
tc := New(5 * time.Minute, 0)
16081640
tc.mu.Lock()

0 commit comments

Comments
 (0)