GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — cache/hotkeys_test.go at main · ubgo/cache

Expand file tree

/

hotkeys_test.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
69 lines (60 loc) · 1.75 KB
/

hotkeys_test.go

Copy path

File metadata and controls

69 lines (60 loc) · 1.75 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// hotkeys_test.go — tests for the Space-Saving HotKeyTracker (hotkeys.go).
package cache_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/ubgo/cache"
"github.com/ubgo/cache/cachetest"
)
func TestHotKeyTrackerRanksHotKey(t *testing.T) {
ctx := context.Background()
hk := cache.NewHotKeyTracker(cachetest.NewMock(), 50)
_ = hk.Set(ctx, "hot", []byte("v"), time.Minute)
for i := 0; i < 200; i++ {
_ = hk.Set(ctx, fmt.Sprintf("cold:%d", i), []byte("v"), time.Minute)
}
for i := 0; i < 500; i++ {
_, _ = hk.Get(ctx, "hot")
}
for i := 0; i < 200; i++ {
_, _ = hk.Get(ctx, fmt.Sprintf("cold:%d", i)) // each cold key once
}
top := hk.Top(1)
if len(top) != 1 || top[0].Key != "hot" {
t.Fatalf("hot key not ranked first: %+v", top)
}
if top[0].Est < 400 { // Space-Saving lower-bounds the true 500 well here
t.Fatalf("hot estimate too low: %d", top[0].Est)
}
}
func TestHotKeyTrackerBoundedMemory(t *testing.T) {
ctx := context.Background()
hk := cache.NewHotKeyTracker(cachetest.NewMock(), 16)
for i := 0; i < 10_000; i++ {
k := fmt.Sprintf("k:%d", i)
_ = hk.Set(ctx, k, []byte("v"), time.Minute)
_, _ = hk.Get(ctx, k)
}
if got := len(hk.Top(0)); got > 16 {
t.Fatalf("tracker exceeded capacity: %d counters", got)
}
}
func TestHotKeyTrackerResetAndConformance(t *testing.T) {
// Still a valid cache.Cache.
cachetest.Run(t, func(_ *testing.T) cache.Cache {
return cache.NewHotKeyTracker(cachetest.NewMock(), 32)
})
ctx := context.Background()
hk := cache.NewHotKeyTracker(cachetest.NewMock(), 8)
_ = hk.Set(ctx, "k", []byte("v"), time.Minute)
_, _ = hk.Get(ctx, "k")
if len(hk.Top(0)) == 0 {
t.Fatal("expected tracked keys before reset")
}
hk.Reset()
if len(hk.Top(0)) != 0 {
t.Fatal("Reset should clear counters")
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.