mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-06-05 23:03:48 +00:00
61d7a45d00
Update cache library, Update logging library, use miniredis for testing, add additional benchmarks.
55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package libpack_cache_memory
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Assume that New function initializes the cache and it is defined somewhere in the libpack_cache package.
|
|
|
|
func BenchmarkMemCacheSet(b *testing.B) {
|
|
cache := New(30 * time.Second) // Initializing the cache with a TTL of 30 seconds
|
|
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)
|
|
}
|
|
}
|
|
|
|
func BenchmarkMemCacheGet(b *testing.B) {
|
|
cache := New(30 * time.Second) // Initializing the cache
|
|
key := "benchmark-key"
|
|
value := []byte("benchmark-value")
|
|
cache.Set(key, value, 5*time.Second) // Pre-set a value to retrieve
|
|
|
|
b.ResetTimer() // Start timing
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
_, _ = cache.Get(key)
|
|
}
|
|
}
|
|
|
|
func BenchmarkMemCacheExpire(b *testing.B) {
|
|
key := "benchmark-expire-key"
|
|
value := []byte("benchmark-value")
|
|
ttl := 5 * time.Millisecond // Setting a short TTL for quick expiration
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
cache := New(30 * time.Second)
|
|
cache.Set(key, value, ttl)
|
|
time.Sleep(ttl) // Wait for the key to expire
|
|
_, _ = cache.Get(key)
|
|
}
|
|
}
|
|
|
|
func BenchmarkMemCacheStats(b *testing.B) {
|
|
cache := New(30 * time.Second) // Initializing the cache
|
|
key := "benchmark-key"
|
|
value := []byte("benchmark-value")
|
|
cache.Set(key, value, 5*time.Second) // Pre-set a value to retrieve
|
|
cache.Get(key)
|
|
}
|