Benchmark for rotator

BenchmarkRotator/1KB-8            200000              5572 ns/op
BenchmarkRotator/2KB-8            200000              8338 ns/op
BenchmarkRotator/4KB-8            100000             14246 ns/op
BenchmarkRotator/8KB-8             50000             25279 ns/op
BenchmarkRotator/16KB-8            30000             48602 ns/op
BenchmarkRotator/32KB-8            20000             92159 ns/op
BenchmarkRotator/64KB-8            10000            154766 ns/op
BenchmarkRotator/128KB-8            5000            296872 ns/op
BenchmarkRotator/256KB-8            3000            551793 ns/op
This commit is contained in:
Alex Dadgar
2018-05-09 17:06:52 -07:00
parent 9966194730
commit 99182736e8
2 changed files with 47 additions and 1 deletions

View File

@@ -16,7 +16,7 @@ import (
const (
// logBufferSize is the size of the buffer.
logBufferSize = 32768
logBufferSize = 32 * 1024
// bufferFlushDuration is the duration at which we flush the buffer.
bufferFlushDuration = 100 * time.Millisecond

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
"path/filepath"
"testing"
@@ -289,3 +290,48 @@ func TestFileRotator_PurgeOldFiles(t *testing.T) {
t.Fatalf("%v", lastErr)
})
}
func BenchmarkRotator(b *testing.B) {
kb := 1024
for _, inputSize := range []int{kb, 2 * kb, 4 * kb, 8 * kb, 16 * kb, 32 * kb, 64 * kb, 128 * kb, 256 * kb} {
b.Run(fmt.Sprintf("%dKB", inputSize/kb), func(b *testing.B) {
benchmarkRotatorWithInputSize(inputSize, b)
})
}
}
func benchmarkRotatorWithInputSize(size int, b *testing.B) {
var path string
var err error
if path, err = ioutil.TempDir("", pathPrefix); err != nil {
b.Fatalf("test setup err: %v", err)
}
defer os.RemoveAll(path)
fr, err := NewFileRotator(path, baseFileName, 5, 1024*1024, logger)
if err != nil {
b.Fatalf("test setup err: %v", err)
}
b.ResetTimer()
// run the Fib function b.N times
for n := 0; n < b.N; n++ {
// Generate some input
data := make([]byte, size)
_, err := rand.Read(data)
if err != nil {
b.Fatalf("Error generating date: %v", err)
}
// Insert random new lines
for i := 0; i < 100; i++ {
index := rand.Intn(size)
data[index] = '\n'
}
// Write the data
if _, err := fr.Write(data); err != nil {
b.Fatalf("Failed to write data: %v", err)
}
}
}