General library improvements.

This commit is contained in:
2025-05-23 12:28:16 +01:00
parent 4d4eac8a20
commit 067841471f
16 changed files with 1175 additions and 89 deletions
+11 -7
View File
@@ -8,22 +8,24 @@ import (
// Assume that New function initializes the cache and it is defined somewhere in the libpack_cache package.
func BenchmarkCacheSet(b *testing.B) {
cache := New(30 * time.Second) // Initializing the cache with a TTL of 30 seconds
cache := New(5 * time.Second) // Use shorter TTL for tests
defer cache.Stop()
key := "benchmark-key"
value := []byte("benchmark-value")
b.ResetTimer() // Reset the timer to exclude the setup time from the benchmark
for i := 0; i < b.N; i++ {
cache.Set(key, value, 5*time.Second)
cache.Set(key, value, 2*time.Second)
}
}
func BenchmarkCacheGet(b *testing.B) {
cache := New(30 * time.Second) // Initializing the cache
cache := New(5 * time.Second) // Use shorter TTL for tests
defer cache.Stop()
key := "benchmark-key"
value := []byte("benchmark-value")
cache.Set(key, value, 5*time.Second) // Pre-set a value to retrieve
cache.Set(key, value, 2*time.Second) // Pre-set a value to retrieve
b.ResetTimer() // Start timing
@@ -33,14 +35,16 @@ func BenchmarkCacheGet(b *testing.B) {
}
func BenchmarkCacheExpire(b *testing.B) {
key := "benchmark-expire-key"
cache := New(5 * time.Second) // Use shorter TTL for tests and reuse cache
defer cache.Stop()
value := []byte("benchmark-value")
ttl := 5 * time.Millisecond // Setting a short TTL for quick expiration
b.ResetTimer()
for i := 0; i < b.N; i++ {
cache := New(30 * time.Second)
key := "benchmark-expire-key-" + string(rune(i)) // Use unique keys
cache.Set(key, value, ttl)
time.Sleep(ttl) // Wait for the key to expire
time.Sleep(ttl + time.Millisecond) // Wait for the key to expire with buffer
_, _ = cache.Get(key)
}
}