Skip to content

Commit 6d77160

Browse files
Thomas StrombergThomas Stromberg
authored andcommitted
more tuning
1 parent 65c0ed3 commit 6d77160

File tree

5 files changed

+211
-109
lines changed

5 files changed

+211
-109
lines changed

benchmarks/benchmark_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ func BenchmarkLRUSet(b *testing.B) { benchLRUSet(b) }
543543
// Concurrent Throughput Benchmarks
544544
// =============================================================================
545545

546-
const concurrentDuration = 1 * time.Second
546+
const concurrentDuration = 2 * time.Second
547547

548548
type concurrentResult struct {
549549
name string

cache_persist_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -555,15 +555,15 @@ func TestCache_GhostQueue(t *testing.T) {
555555
func TestCache_MainQueueEviction(t *testing.T) {
556556
ctx := context.Background()
557557

558-
// Create cache with capacity divisible by 32 shards (64 = 2 per shard)
559-
cache, err := New[string, int](ctx, WithMemorySize(64))
558+
// Create cache with 20000 capacity (approx 10 per shard with 2048 shards)
559+
cache, err := New[string, int](ctx, WithMemorySize(20000))
560560
if err != nil {
561561
t.Fatalf("New: %v", err)
562562
}
563563
defer func() { _ = cache.Close() }() //nolint:errcheck // Test cleanup
564564

565565
// Insert and access items to get them into Main queue
566-
for i := range 96 {
566+
for i := range 25000 {
567567
key := fmt.Sprintf("key%d", i)
568568
if err := cache.Set(ctx, key, i, 0); err != nil {
569569
t.Fatalf("Set: %v", err)
@@ -573,17 +573,17 @@ func TestCache_MainQueueEviction(t *testing.T) {
573573
}
574574

575575
// Insert more items to trigger eviction from Main queue
576-
for i := range 64 {
577-
key := fmt.Sprintf("key%d", i+100)
578-
if err := cache.Set(ctx, key, i+100, 0); err != nil {
576+
for i := range 10000 {
577+
key := fmt.Sprintf("key%d", i+30000)
578+
if err := cache.Set(ctx, key, i+30000, 0); err != nil {
579579
t.Fatalf("Set: %v", err)
580580
}
581581
_, _, _ = cache.Get(ctx, key) //nolint:errcheck // Exercising code path
582582
}
583583

584-
// Verify cache is at capacity
585-
if cache.Len() > 64 {
586-
t.Errorf("Cache length %d exceeds capacity 64", cache.Len())
584+
// Verify cache is at capacity (with 2048 shards, effective capacity is ~20480)
585+
if cache.Len() > 20480 {
586+
t.Errorf("Cache length %d exceeds capacity 20480", cache.Len())
587587
}
588588
}
589589

cache_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -489,29 +489,30 @@ func TestCache_DeleteNonExistent(t *testing.T) {
489489

490490
func TestCache_EvictFromMain(t *testing.T) {
491491
ctx := context.Background()
492-
// Cache with capacity divisible by 32 shards (64 = 2 per shard)
493-
cache, err := New[int, int](ctx, WithMemorySize(64))
492+
// Cache with 20000 capacity (approx 10 per shard with 2048 shards)
493+
cache, err := New[int, int](ctx, WithMemorySize(20000))
494494
if err != nil {
495495
t.Fatalf("New: %v", err)
496496
}
497497
defer func() { _ = cache.Close() }() //nolint:errcheck // Test cleanup
498498

499-
// Fill small queue and promote items to main by accessing them twice
500-
for i := range 96 {
499+
// Fill cache and promote items to main by accessing them
500+
for i := range 25000 {
501501
_ = cache.Set(ctx, i, i, 0) //nolint:errcheck // Test fixture
502502
// Access immediately to promote to main
503503
_, _, _ = cache.Get(ctx, i) //nolint:errcheck // Exercising code path
504504
}
505505

506506
// Add more items to force eviction from main queue
507-
for i := range 64 {
508-
_ = cache.Set(ctx, i+100, i+100, 0) //nolint:errcheck // Test fixture
509-
_, _, _ = cache.Get(ctx, i+100) //nolint:errcheck // Exercising code path
507+
for i := range 10000 {
508+
_ = cache.Set(ctx, i+30000, i+30000, 0) //nolint:errcheck // Test fixture
509+
_, _, _ = cache.Get(ctx, i+30000) //nolint:errcheck // Exercising code path
510510
}
511511

512-
// Cache should not exceed capacity
513-
if cache.Len() > 64 {
514-
t.Errorf("cache length = %d; should not exceed 64", cache.Len())
512+
// Cache should not exceed configured capacity by more than shard overhead
513+
// With 2048 shards and rounding, effective capacity is ~20480
514+
if cache.Len() > 20480 {
515+
t.Errorf("cache length = %d; should not exceed 20480", cache.Len())
515516
}
516517
}
517518

0 commit comments

Comments
 (0)