Files
pixa/internal/imgcache/counters_internal_test.go
T
sneak c3a6799763
check / check (push) Canceled after 0s
Count hits, misses, fetches and transforms as Prometheus counters (closes #311)
The five counts the cache_stats table held are now Prometheus counters in
memory, in internal/imgcache/counters.go, on the default registry that
/metrics serves. The Increment methods add to them, so counting makes no
database call.

cache_stats, Cache.Stats, Service.Stats, CacheStats and the pending counts
are gone, with their tests. The background goroutine now writes only access
times, and its names say so: StartPendingAccessTimeWrites,
StopPendingAccessTimeWrites and pendingaccesstimes.go.

The counters count for the whole process, so the tests that check how much
they rose do not call t.Parallel. Two stop tests whose write under way was a
hit count now use an access time.

Model: opus-5-5
2026-10-26 08:50:41 +00:00

68 lines
2.2 KiB
Go

package imgcache
import (
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
)
// counterValues holds the values of the counters in counters.go.
type counterValues struct {
hitCount int64
missCount int64
upstreamFetchCount int64
upstreamFetchBytes int64
transformCount int64
}
// readCounters reads the counters in counters.go. They count for the whole
// process, and every test that serves an image adds to them, so a test that
// checks how much they rose does not call t.Parallel. Go runs the tests that
// do not call it one at a time, and the tests that do only after them.
func readCounters() counterValues {
return counterValues{
hitCount: int64(testutil.ToFloat64(cacheHits)),
missCount: int64(testutil.ToFloat64(cacheMisses)),
upstreamFetchCount: int64(testutil.ToFloat64(upstreamFetches)),
upstreamFetchBytes: int64(testutil.ToFloat64(upstreamFetchBytes)),
transformCount: int64(testutil.ToFloat64(transforms)),
}
}
// minus returns how much each counter rose from before to v.
func (v counterValues) minus(before counterValues) counterValues {
return counterValues{
hitCount: v.hitCount - before.hitCount,
missCount: v.missCount - before.missCount,
upstreamFetchCount: v.upstreamFetchCount - before.upstreamFetchCount,
upstreamFetchBytes: v.upstreamFetchBytes - before.upstreamFetchBytes,
transformCount: v.transformCount - before.transformCount,
}
}
// TestCounters_AreOnTheRegistryMetricsServes checks that the counters in
// counters.go are on Prometheus' default registry, which /metrics serves,
// under the names README.md gives them.
func TestCounters_AreOnTheRegistryMetricsServes(t *testing.T) {
t.Parallel()
names := []string{
"pixa_cache_hits_total",
"pixa_cache_misses_total",
"pixa_upstream_fetches_total",
"pixa_upstream_fetch_bytes_total",
"pixa_transforms_total",
}
count, err := testutil.GatherAndCount(prometheus.DefaultGatherer, names...)
if err != nil {
t.Fatalf("testutil.GatherAndCount() error = %v", err)
}
if count != len(names) {
t.Errorf("%d of the counters %v are on the default registry, want %d",
count, names, len(names))
}
}