mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-19 04:10:30 +08:00
增加若干内存缓存相关基准测试
This commit is contained in:
143
internal/caches/writer_memory_test.go
Normal file
143
internal/caches/writer_memory_test.go
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||||
|
|
||||||
|
package caches_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||||
|
"github.com/TeaOSLab/EdgeNode/internal/caches"
|
||||||
|
"math/rand"
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewMemoryWriter(t *testing.T) {
|
||||||
|
var storage = caches.NewMemoryStorage(&serverconfigs.HTTPCachePolicy{
|
||||||
|
Id: 0,
|
||||||
|
IsOn: false,
|
||||||
|
Name: "",
|
||||||
|
Description: "",
|
||||||
|
Capacity: &shared.SizeCapacity{
|
||||||
|
Count: 8,
|
||||||
|
Unit: shared.SizeCapacityUnitGB,
|
||||||
|
},
|
||||||
|
}, nil)
|
||||||
|
err := storage.Init()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
const size = 1 << 20
|
||||||
|
const chunkSize = 16 << 10
|
||||||
|
var data = bytes.Repeat([]byte{'A'}, chunkSize)
|
||||||
|
|
||||||
|
var before = time.Now()
|
||||||
|
|
||||||
|
var writer = caches.NewMemoryWriter(storage, "a", time.Now().Unix()+3600, 200, false, size, 1<<30, func(valueItem *caches.MemoryItem) {
|
||||||
|
t.Log(len(valueItem.BodyValue), "bytes")
|
||||||
|
})
|
||||||
|
|
||||||
|
for i := 0; i < size/chunkSize; i++ {
|
||||||
|
_, err = writer.Write(data)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = writer.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Log("cost:", time.Since(before).Seconds()*1000, "ms")
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkMemoryWriter_Capacity(b *testing.B) {
|
||||||
|
b.ReportAllocs()
|
||||||
|
|
||||||
|
var storage = caches.NewMemoryStorage(&serverconfigs.HTTPCachePolicy{
|
||||||
|
Id: 0,
|
||||||
|
IsOn: false,
|
||||||
|
Name: "",
|
||||||
|
Description: "",
|
||||||
|
Capacity: &shared.SizeCapacity{
|
||||||
|
Count: 8,
|
||||||
|
Unit: shared.SizeCapacityUnitGB,
|
||||||
|
},
|
||||||
|
}, nil)
|
||||||
|
initErr := storage.Init()
|
||||||
|
if initErr != nil {
|
||||||
|
b.Fatal(initErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
const size = 1 << 20
|
||||||
|
const chunkSize = 16 << 10
|
||||||
|
var data = bytes.Repeat([]byte{'A'}, chunkSize)
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
|
||||||
|
b.RunParallel(func(pb *testing.PB) {
|
||||||
|
for pb.Next() {
|
||||||
|
var writer = caches.NewMemoryWriter(storage, "a"+strconv.Itoa(rand.Int()), time.Now().Unix()+3600, 200, false, size, 1<<30, func(valueItem *caches.MemoryItem) {
|
||||||
|
})
|
||||||
|
|
||||||
|
for i := 0; i < size/chunkSize; i++ {
|
||||||
|
_, err := writer.Write(data)
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err := writer.Close()
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkMemoryWriter_Capacity_Disabled(b *testing.B) {
|
||||||
|
b.ReportAllocs()
|
||||||
|
|
||||||
|
var storage = caches.NewMemoryStorage(&serverconfigs.HTTPCachePolicy{
|
||||||
|
Id: 0,
|
||||||
|
IsOn: false,
|
||||||
|
Name: "",
|
||||||
|
Description: "",
|
||||||
|
Capacity: &shared.SizeCapacity{
|
||||||
|
Count: 8,
|
||||||
|
Unit: shared.SizeCapacityUnitGB,
|
||||||
|
},
|
||||||
|
}, nil)
|
||||||
|
initErr := storage.Init()
|
||||||
|
if initErr != nil {
|
||||||
|
b.Fatal(initErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
const size = 1 << 20
|
||||||
|
const chunkSize = 16 << 10
|
||||||
|
var data = bytes.Repeat([]byte{'A'}, chunkSize)
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
|
||||||
|
b.RunParallel(func(pb *testing.PB) {
|
||||||
|
for pb.Next() {
|
||||||
|
var writer = caches.NewMemoryWriter(storage, "a"+strconv.Itoa(rand.Int()), time.Now().Unix()+3600, 200, false, 0, 1<<30, func(valueItem *caches.MemoryItem) {
|
||||||
|
})
|
||||||
|
|
||||||
|
for i := 0; i < size/chunkSize; i++ {
|
||||||
|
_, err := writer.Write(data)
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err := writer.Close()
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -55,3 +55,9 @@ func StartMemoryStats(t *testing.T, callbacks ...func()) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReadMemoryStat() *runtime.MemStats {
|
||||||
|
var stat = &runtime.MemStats{}
|
||||||
|
runtime.ReadMemStats(stat)
|
||||||
|
return stat
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user