mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Add a benchmark for logger.Copier
Add a benchmark for measuring how the logger.Copier implementation handles logged lines of sizes ranging up from 64 bytes to 256KB. Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
This commit is contained in:
parent
b8f3cdfaec
commit
af439c7a8d
1 changed files with 92 additions and 0 deletions
|
@ -3,7 +3,9 @@ package logger
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -116,3 +118,93 @@ func TestCopierSlow(t *testing.T) {
|
||||||
case <-wait:
|
case <-wait:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type BenchmarkLoggerDummy struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *BenchmarkLoggerDummy) Log(m *Message) error { return nil }
|
||||||
|
|
||||||
|
func (l *BenchmarkLoggerDummy) Close() error { return nil }
|
||||||
|
|
||||||
|
func (l *BenchmarkLoggerDummy) Name() string { return "dummy" }
|
||||||
|
|
||||||
|
func BenchmarkCopier64(b *testing.B) {
|
||||||
|
benchmarkCopier(b, 1<<6)
|
||||||
|
}
|
||||||
|
func BenchmarkCopier128(b *testing.B) {
|
||||||
|
benchmarkCopier(b, 1<<7)
|
||||||
|
}
|
||||||
|
func BenchmarkCopier256(b *testing.B) {
|
||||||
|
benchmarkCopier(b, 1<<8)
|
||||||
|
}
|
||||||
|
func BenchmarkCopier512(b *testing.B) {
|
||||||
|
benchmarkCopier(b, 1<<9)
|
||||||
|
}
|
||||||
|
func BenchmarkCopier1K(b *testing.B) {
|
||||||
|
benchmarkCopier(b, 1<<10)
|
||||||
|
}
|
||||||
|
func BenchmarkCopier2K(b *testing.B) {
|
||||||
|
benchmarkCopier(b, 1<<11)
|
||||||
|
}
|
||||||
|
func BenchmarkCopier4K(b *testing.B) {
|
||||||
|
benchmarkCopier(b, 1<<12)
|
||||||
|
}
|
||||||
|
func BenchmarkCopier8K(b *testing.B) {
|
||||||
|
benchmarkCopier(b, 1<<13)
|
||||||
|
}
|
||||||
|
func BenchmarkCopier16K(b *testing.B) {
|
||||||
|
benchmarkCopier(b, 1<<14)
|
||||||
|
}
|
||||||
|
func BenchmarkCopier32K(b *testing.B) {
|
||||||
|
benchmarkCopier(b, 1<<15)
|
||||||
|
}
|
||||||
|
func BenchmarkCopier64K(b *testing.B) {
|
||||||
|
benchmarkCopier(b, 1<<16)
|
||||||
|
}
|
||||||
|
func BenchmarkCopier128K(b *testing.B) {
|
||||||
|
benchmarkCopier(b, 1<<17)
|
||||||
|
}
|
||||||
|
func BenchmarkCopier256K(b *testing.B) {
|
||||||
|
benchmarkCopier(b, 1<<18)
|
||||||
|
}
|
||||||
|
|
||||||
|
func piped(b *testing.B, iterations int, delay time.Duration, buf []byte) io.Reader {
|
||||||
|
r, w, err := os.Pipe()
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
for i := 0; i < iterations; i++ {
|
||||||
|
time.Sleep(delay)
|
||||||
|
if n, err := w.Write(buf); err != nil || n != len(buf) {
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
b.Fatal(fmt.Errorf("short write"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.Close()
|
||||||
|
}()
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkCopier(b *testing.B, length int) {
|
||||||
|
b.StopTimer()
|
||||||
|
buf := []byte{'A'}
|
||||||
|
for len(buf) < length {
|
||||||
|
buf = append(buf, buf...)
|
||||||
|
}
|
||||||
|
buf = append(buf[:length-1], []byte{'\n'}...)
|
||||||
|
b.StartTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
c := NewCopier(
|
||||||
|
map[string]io.Reader{
|
||||||
|
"buffer": piped(b, 10, time.Nanosecond, buf),
|
||||||
|
},
|
||||||
|
&BenchmarkLoggerDummy{})
|
||||||
|
c.Run()
|
||||||
|
c.Wait()
|
||||||
|
c.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue