MCPcopy
hub / github.com/safing/portmaster / benchmarkCacheWriting

Function benchmarkCacheWriting

base/database/interface_cache_test.go:12–72  ·  view source on GitHub ↗
(b *testing.B, storageType string, cacheSize int, sampleSize int, delayWrites bool)

Source from the content-addressed store, hash-verified

10)
11
12func benchmarkCacheWriting(b *testing.B, storageType string, cacheSize int, sampleSize int, delayWrites bool) { //nolint:gocognit,gocyclo,thelper
13 b.Run(fmt.Sprintf("CacheWriting_%s_%d_%d_%v", storageType, cacheSize, sampleSize, delayWrites), func(b *testing.B) {
14 // Setup Benchmark.
15
16 // Create database.
17 dbName := fmt.Sprintf("cache-w-benchmark-%s-%d-%d-%v", storageType, cacheSize, sampleSize, delayWrites)
18 _, err := Register(&Database{
19 Name: dbName,
20 Description: fmt.Sprintf("Cache Benchmark Database for %s", storageType),
21 StorageType: storageType,
22 })
23 if err != nil {
24 b.Fatal(err)
25 }
26
27 // Create benchmark interface.
28 options := &Options{
29 Local: true,
30 Internal: true,
31 CacheSize: cacheSize,
32 }
33 if cacheSize > 0 && delayWrites {
34 options.DelayCachedWrites = dbName
35 }
36 db := NewInterface(options)
37
38 // Start
39 m := mgr.New("Cache writing benchmark test")
40 var wg sync.WaitGroup
41 if cacheSize > 0 && delayWrites {
42 wg.Add(1)
43 m.Go("Cache writing benchmark worker", func(wc *mgr.WorkerCtx) error {
44 err := db.DelayedCacheWriter(wc)
45 if err != nil {
46 panic(err)
47 }
48 wg.Done()
49 return nil
50 })
51 }
52
53 // Start Benchmark.
54 b.ResetTimer()
55 for i := range b.N {
56 testRecordID := i % sampleSize
57 r := NewExample(
58 dbName+":"+strconv.Itoa(testRecordID),
59 "A",
60 1,
61 )
62 err = db.Put(r)
63 if err != nil {
64 b.Fatal(err)
65 }
66 }
67
68 // End cache writer and wait
69 m.Cancel()

Callers 1

BenchmarkCacheFunction · 0.85

Calls 12

DelayedCacheWriterMethod · 0.95
PutMethod · 0.95
NewFunction · 0.92
NewInterfaceFunction · 0.85
NewExampleFunction · 0.85
RegisterFunction · 0.70
RunMethod · 0.65
WaitMethod · 0.65
AddMethod · 0.45
GoMethod · 0.45
DoneMethod · 0.45
CancelMethod · 0.45

Tested by

no test coverage detected