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

Function benchmarkCacheReadWrite

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

Source from the content-addressed store, hash-verified

72}
73
74func benchmarkCacheReadWrite(b *testing.B, storageType string, cacheSize int, sampleSize int, delayWrites bool) { //nolint:gocognit,gocyclo,thelper
75 b.Run(fmt.Sprintf("CacheReadWrite_%s_%d_%d_%v", storageType, cacheSize, sampleSize, delayWrites), func(b *testing.B) {
76 // Setup Benchmark.
77
78 // Create database.
79 dbName := fmt.Sprintf("cache-rw-benchmark-%s-%d-%d-%v", storageType, cacheSize, sampleSize, delayWrites)
80 _, err := Register(&Database{
81 Name: dbName,
82 Description: fmt.Sprintf("Cache Benchmark Database for %s", storageType),
83 StorageType: storageType,
84 })
85 if err != nil {
86 b.Fatal(err)
87 }
88
89 // Create benchmark interface.
90 options := &Options{
91 Local: true,
92 Internal: true,
93 CacheSize: cacheSize,
94 }
95 if cacheSize > 0 && delayWrites {
96 options.DelayCachedWrites = dbName
97 }
98 db := NewInterface(options)
99
100 // Start
101 m := mgr.New("Cache read/write benchmark test")
102 var wg sync.WaitGroup
103 if cacheSize > 0 && delayWrites {
104 wg.Add(1)
105 m.Go("Cache read/write benchmark worker", func(wc *mgr.WorkerCtx) error {
106 err := db.DelayedCacheWriter(wc)
107 if err != nil {
108 panic(err)
109 }
110 wg.Done()
111 return nil
112 })
113 }
114
115 // Start Benchmark.
116 b.ResetTimer()
117 writing := true
118 for i := range b.N {
119 testRecordID := i % sampleSize
120 key := dbName + ":" + strconv.Itoa(testRecordID)
121
122 if i > 0 && testRecordID == 0 {
123 writing = !writing // switch between reading and writing every samplesize
124 }
125
126 if writing {
127 r := NewExample(key, "A", 1)
128 err = db.Put(r)
129 } else {
130 _, err = db.Get(key)
131 }

Callers 1

BenchmarkCacheFunction · 0.85

Calls 13

DelayedCacheWriterMethod · 0.95
PutMethod · 0.95
GetMethod · 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

Tested by

no test coverage detected