mirror of
https://github.com/datarhei/core.git
synced 2025-09-27 04:16:25 +08:00
Add benchmark
This commit is contained in:
@@ -1,8 +1,15 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
gorand "math/rand"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/datarhei/core/v16/math/rand"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@@ -28,3 +35,68 @@ func TestMemFromDir(t *testing.T) {
|
||||
"/sized.go",
|
||||
}, names)
|
||||
}
|
||||
|
||||
func BenchmarkMemReadFileWhileWriting(b *testing.B) {
|
||||
mem, err := NewMemFilesystem(MemConfig{})
|
||||
require.NoError(b, err)
|
||||
|
||||
nReaders := 500
|
||||
nWriters := 1000
|
||||
nFiles := 30
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
writerWg := sync.WaitGroup{}
|
||||
|
||||
data := []byte(rand.StringAlphanumeric(2 * 1024))
|
||||
|
||||
for i := 0; i < nWriters; i++ {
|
||||
writerWg.Add(1)
|
||||
|
||||
go func(ctx context.Context, from int) {
|
||||
for i := 0; i < nFiles; i++ {
|
||||
path := fmt.Sprintf("/%d.dat", i+from)
|
||||
mem.WriteFile(path, data)
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(40 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
|
||||
writerWg.Done()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
num := gorand.Intn(nFiles) + from
|
||||
path := fmt.Sprintf("/%d.dat", num)
|
||||
mem.WriteFile(path, data)
|
||||
}
|
||||
}
|
||||
}(ctx, i*nFiles)
|
||||
}
|
||||
|
||||
// Wait for all writers to be started
|
||||
writerWg.Wait()
|
||||
|
||||
b.ResetTimer()
|
||||
|
||||
readerWg := sync.WaitGroup{}
|
||||
|
||||
for i := 0; i < nReaders; i++ {
|
||||
readerWg.Add(1)
|
||||
go func() {
|
||||
defer readerWg.Done()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
num := gorand.Intn(nWriters * nFiles)
|
||||
f := mem.Open("/" + strconv.Itoa(num) + ".dat")
|
||||
f.Close()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
readerWg.Wait()
|
||||
}
|
||||
|
Reference in New Issue
Block a user