2018-02-05 21:05:59 +00:00
|
|
|
package logger // import "github.com/docker/docker/daemon/logger"
|
2015-02-03 23:41:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2016-06-06 15:50:09 +00:00
|
|
|
"os"
|
2016-05-02 11:50:08 +00:00
|
|
|
"strings"
|
2016-05-25 04:19:01 +00:00
|
|
|
"sync"
|
2015-02-03 23:41:26 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestLoggerJSON struct {
|
|
|
|
*json.Encoder
|
2016-05-25 04:19:01 +00:00
|
|
|
mu sync.Mutex
|
2016-01-15 13:42:23 +00:00
|
|
|
delay time.Duration
|
2015-02-03 23:41:26 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 13:42:23 +00:00
|
|
|
func (l *TestLoggerJSON) Log(m *Message) error {
|
|
|
|
if l.delay > 0 {
|
|
|
|
time.Sleep(l.delay)
|
|
|
|
}
|
2016-05-25 04:19:01 +00:00
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
2016-01-15 13:42:23 +00:00
|
|
|
return l.Encode(m)
|
|
|
|
}
|
2015-02-03 23:41:26 +00:00
|
|
|
|
2015-04-09 04:23:30 +00:00
|
|
|
func (l *TestLoggerJSON) Close() error { return nil }
|
|
|
|
|
|
|
|
func (l *TestLoggerJSON) Name() string { return "json" }
|
2015-02-03 23:41:26 +00:00
|
|
|
|
2017-08-22 17:52:52 +00:00
|
|
|
type TestSizedLoggerJSON struct {
|
|
|
|
*json.Encoder
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *TestSizedLoggerJSON) Log(m *Message) error {
|
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
return l.Encode(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*TestSizedLoggerJSON) Close() error { return nil }
|
|
|
|
|
|
|
|
func (*TestSizedLoggerJSON) Name() string { return "sized-json" }
|
|
|
|
|
|
|
|
func (*TestSizedLoggerJSON) BufSize() int {
|
|
|
|
return 32 * 1024
|
|
|
|
}
|
|
|
|
|
2015-02-03 23:41:26 +00:00
|
|
|
func TestCopier(t *testing.T) {
|
|
|
|
stdoutLine := "Line that thinks that it is log line from docker stdout"
|
|
|
|
stderrLine := "Line that thinks that it is log line from docker stderr"
|
2016-05-02 11:50:08 +00:00
|
|
|
stdoutTrailingLine := "stdout trailing line"
|
|
|
|
stderrTrailingLine := "stderr trailing line"
|
|
|
|
|
2015-02-03 23:41:26 +00:00
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
for i := 0; i < 30; i++ {
|
|
|
|
if _, err := stdout.WriteString(stdoutLine + "\n"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := stderr.WriteString(stderrLine + "\n"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 11:50:08 +00:00
|
|
|
// Test remaining lines without line-endings
|
|
|
|
if _, err := stdout.WriteString(stdoutTrailingLine); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := stderr.WriteString(stderrTrailingLine); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonBuf bytes.Buffer
|
|
|
|
|
|
|
|
jsonLog := &TestLoggerJSON{Encoder: json.NewEncoder(&jsonBuf)}
|
|
|
|
|
|
|
|
c := NewCopier(
|
|
|
|
map[string]io.Reader{
|
|
|
|
"stdout": &stdout,
|
|
|
|
"stderr": &stderr,
|
|
|
|
},
|
|
|
|
jsonLog)
|
|
|
|
c.Run()
|
|
|
|
wait := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
c.Wait()
|
|
|
|
close(wait)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("Copier failed to do its work in 1 second")
|
|
|
|
case <-wait:
|
|
|
|
}
|
|
|
|
dec := json.NewDecoder(&jsonBuf)
|
|
|
|
for {
|
|
|
|
var msg Message
|
|
|
|
if err := dec.Decode(&msg); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if msg.Source != "stdout" && msg.Source != "stderr" {
|
|
|
|
t.Fatalf("Wrong Source: %q, should be %q or %q", msg.Source, "stdout", "stderr")
|
|
|
|
}
|
|
|
|
if msg.Source == "stdout" {
|
|
|
|
if string(msg.Line) != stdoutLine && string(msg.Line) != stdoutTrailingLine {
|
|
|
|
t.Fatalf("Wrong Line: %q, expected %q or %q", msg.Line, stdoutLine, stdoutTrailingLine)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if msg.Source == "stderr" {
|
|
|
|
if string(msg.Line) != stderrLine && string(msg.Line) != stderrTrailingLine {
|
|
|
|
t.Fatalf("Wrong Line: %q, expected %q or %q", msg.Line, stderrLine, stderrTrailingLine)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCopierLongLines tests long lines without line breaks
|
|
|
|
func TestCopierLongLines(t *testing.T) {
|
2017-08-22 17:52:52 +00:00
|
|
|
// Long lines (should be split at "defaultBufSize")
|
|
|
|
stdoutLongLine := strings.Repeat("a", defaultBufSize)
|
|
|
|
stderrLongLine := strings.Repeat("b", defaultBufSize)
|
2016-05-02 11:50:08 +00:00
|
|
|
stdoutTrailingLine := "stdout trailing line"
|
|
|
|
stderrTrailingLine := "stderr trailing line"
|
|
|
|
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
if _, err := stdout.WriteString(stdoutLongLine); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := stderr.WriteString(stderrLongLine); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := stdout.WriteString(stdoutTrailingLine); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := stderr.WriteString(stderrTrailingLine); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-02-03 23:41:26 +00:00
|
|
|
var jsonBuf bytes.Buffer
|
|
|
|
|
|
|
|
jsonLog := &TestLoggerJSON{Encoder: json.NewEncoder(&jsonBuf)}
|
|
|
|
|
2016-05-31 19:46:55 +00:00
|
|
|
c := NewCopier(
|
2015-02-03 23:41:26 +00:00
|
|
|
map[string]io.Reader{
|
|
|
|
"stdout": &stdout,
|
|
|
|
"stderr": &stderr,
|
|
|
|
},
|
|
|
|
jsonLog)
|
|
|
|
c.Run()
|
2015-03-18 18:44:14 +00:00
|
|
|
wait := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
c.Wait()
|
|
|
|
close(wait)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("Copier failed to do its work in 1 second")
|
|
|
|
case <-wait:
|
|
|
|
}
|
2015-02-03 23:41:26 +00:00
|
|
|
dec := json.NewDecoder(&jsonBuf)
|
|
|
|
for {
|
|
|
|
var msg Message
|
|
|
|
if err := dec.Decode(&msg); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if msg.Source != "stdout" && msg.Source != "stderr" {
|
|
|
|
t.Fatalf("Wrong Source: %q, should be %q or %q", msg.Source, "stdout", "stderr")
|
|
|
|
}
|
|
|
|
if msg.Source == "stdout" {
|
2016-05-02 11:50:08 +00:00
|
|
|
if string(msg.Line) != stdoutLongLine && string(msg.Line) != stdoutTrailingLine {
|
|
|
|
t.Fatalf("Wrong Line: %q, expected 'stdoutLongLine' or 'stdoutTrailingLine'", msg.Line)
|
2015-02-03 23:41:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if msg.Source == "stderr" {
|
2016-05-02 11:50:08 +00:00
|
|
|
if string(msg.Line) != stderrLongLine && string(msg.Line) != stderrTrailingLine {
|
|
|
|
t.Fatalf("Wrong Line: %q, expected 'stderrLongLine' or 'stderrTrailingLine'", msg.Line)
|
2015-02-03 23:41:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-15 13:42:23 +00:00
|
|
|
|
|
|
|
func TestCopierSlow(t *testing.T) {
|
|
|
|
stdoutLine := "Line that thinks that it is log line from docker stdout"
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
for i := 0; i < 30; i++ {
|
|
|
|
if _, err := stdout.WriteString(stdoutLine + "\n"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonBuf bytes.Buffer
|
2019-11-27 14:42:27 +00:00
|
|
|
// encoder := &encodeCloser{Encoder: json.NewEncoder(&jsonBuf)}
|
2016-01-15 13:42:23 +00:00
|
|
|
jsonLog := &TestLoggerJSON{Encoder: json.NewEncoder(&jsonBuf), delay: 100 * time.Millisecond}
|
|
|
|
|
2016-05-31 19:46:55 +00:00
|
|
|
c := NewCopier(map[string]io.Reader{"stdout": &stdout}, jsonLog)
|
2016-01-15 13:42:23 +00:00
|
|
|
c.Run()
|
|
|
|
wait := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
c.Wait()
|
|
|
|
close(wait)
|
|
|
|
}()
|
|
|
|
<-time.After(150 * time.Millisecond)
|
|
|
|
c.Close()
|
|
|
|
select {
|
|
|
|
case <-time.After(200 * time.Millisecond):
|
2017-02-21 08:53:29 +00:00
|
|
|
t.Fatal("failed to exit in time after the copier is closed")
|
2016-01-15 13:42:23 +00:00
|
|
|
case <-wait:
|
|
|
|
}
|
|
|
|
}
|
2016-06-06 15:50:09 +00:00
|
|
|
|
2017-08-22 17:52:52 +00:00
|
|
|
func TestCopierWithSized(t *testing.T) {
|
2021-01-20 21:18:41 +00:00
|
|
|
t.Run("as is", func(t *testing.T) {
|
|
|
|
testCopierWithSized(t, func(l SizedLogger) SizedLogger {
|
|
|
|
return l
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("With RingLogger", func(t *testing.T) {
|
|
|
|
testCopierWithSized(t, func(l SizedLogger) SizedLogger {
|
|
|
|
return newRingLogger(l, Info{}, defaultRingMaxSize)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCopierWithSized(t *testing.T, loggerFactory func(SizedLogger) SizedLogger) {
|
2017-08-22 17:52:52 +00:00
|
|
|
var jsonBuf bytes.Buffer
|
|
|
|
expectedMsgs := 2
|
2021-01-20 21:18:41 +00:00
|
|
|
sizedLogger := loggerFactory(&TestSizedLoggerJSON{Encoder: json.NewEncoder(&jsonBuf)})
|
|
|
|
|
|
|
|
size := sizedLogger.BufSize()
|
|
|
|
if size < 0 {
|
|
|
|
size = 100
|
|
|
|
}
|
|
|
|
logbuf := bytes.NewBufferString(strings.Repeat(".", size*expectedMsgs))
|
2017-08-22 17:52:52 +00:00
|
|
|
c := NewCopier(map[string]io.Reader{"stdout": logbuf}, sizedLogger)
|
|
|
|
|
|
|
|
c.Run()
|
|
|
|
// Wait for Copier to finish writing to the buffered logger.
|
|
|
|
c.Wait()
|
|
|
|
c.Close()
|
|
|
|
|
2021-01-20 21:18:41 +00:00
|
|
|
sizedLogger.Close()
|
|
|
|
|
2017-08-22 17:52:52 +00:00
|
|
|
recvdMsgs := 0
|
|
|
|
dec := json.NewDecoder(&jsonBuf)
|
|
|
|
for {
|
|
|
|
var msg Message
|
|
|
|
if err := dec.Decode(&msg); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if msg.Source != "stdout" {
|
|
|
|
t.Fatalf("Wrong Source: %q, should be %q", msg.Source, "stdout")
|
|
|
|
}
|
|
|
|
if len(msg.Line) != sizedLogger.BufSize() {
|
|
|
|
t.Fatalf("Line was not of expected max length %d, was %d", sizedLogger.BufSize(), len(msg.Line))
|
|
|
|
}
|
|
|
|
recvdMsgs++
|
|
|
|
}
|
|
|
|
if recvdMsgs != expectedMsgs {
|
2021-01-20 21:18:41 +00:00
|
|
|
t.Fatalf("expected to receive %d messages, actually received %d %q", expectedMsgs, recvdMsgs, jsonBuf.String())
|
2017-08-22 17:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 02:26:55 +00:00
|
|
|
func checkIdentical(t *testing.T, msg Message, expectedID string, expectedTS time.Time) {
|
|
|
|
if msg.PLogMetaData.ID != expectedID {
|
|
|
|
t.Fatalf("IDs are not he same across partials. Expected: %s Received: %s",
|
|
|
|
expectedID, msg.PLogMetaData.ID)
|
|
|
|
}
|
|
|
|
if msg.Timestamp != expectedTS {
|
|
|
|
t.Fatalf("Timestamps are not the same across partials. Expected: %v Received: %v",
|
|
|
|
expectedTS.Format(time.UnixDate), msg.Timestamp.Format(time.UnixDate))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Have long lines and make sure that it comes out with PartialMetaData
|
|
|
|
func TestCopierWithPartial(t *testing.T) {
|
|
|
|
stdoutLongLine := strings.Repeat("a", defaultBufSize)
|
|
|
|
stderrLongLine := strings.Repeat("b", defaultBufSize)
|
|
|
|
stdoutTrailingLine := "stdout trailing line"
|
|
|
|
stderrTrailingLine := "stderr trailing line"
|
|
|
|
normalStr := "This is an impartial message :)"
|
|
|
|
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
var normalMsg bytes.Buffer
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
if _, err := stdout.WriteString(stdoutLongLine); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := stderr.WriteString(stderrLongLine); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := stdout.WriteString(stdoutTrailingLine + "\n"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := stderr.WriteString(stderrTrailingLine + "\n"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := normalMsg.WriteString(normalStr + "\n"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonBuf bytes.Buffer
|
|
|
|
|
|
|
|
jsonLog := &TestLoggerJSON{Encoder: json.NewEncoder(&jsonBuf)}
|
|
|
|
|
|
|
|
c := NewCopier(
|
|
|
|
map[string]io.Reader{
|
|
|
|
"stdout": &stdout,
|
|
|
|
"normal": &normalMsg,
|
|
|
|
"stderr": &stderr,
|
|
|
|
},
|
|
|
|
jsonLog)
|
|
|
|
c.Run()
|
|
|
|
wait := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
c.Wait()
|
|
|
|
close(wait)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("Copier failed to do its work in 1 second")
|
|
|
|
case <-wait:
|
|
|
|
}
|
|
|
|
|
|
|
|
dec := json.NewDecoder(&jsonBuf)
|
|
|
|
expectedMsgs := 9
|
|
|
|
recvMsgs := 0
|
|
|
|
var expectedPartID1, expectedPartID2 string
|
|
|
|
var expectedTS1, expectedTS2 time.Time
|
|
|
|
|
|
|
|
for {
|
|
|
|
var msg Message
|
|
|
|
|
|
|
|
if err := dec.Decode(&msg); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if msg.Source != "stdout" && msg.Source != "stderr" && msg.Source != "normal" {
|
|
|
|
t.Fatalf("Wrong Source: %q, should be %q or %q or %q", msg.Source, "stdout", "stderr", "normal")
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Source == "stdout" {
|
|
|
|
if string(msg.Line) != stdoutLongLine && string(msg.Line) != stdoutTrailingLine {
|
|
|
|
t.Fatalf("Wrong Line: %q, expected 'stdoutLongLine' or 'stdoutTrailingLine'", msg.Line)
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.PLogMetaData.ID == "" {
|
|
|
|
t.Fatalf("Expected partial metadata. Got nothing")
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.PLogMetaData.Ordinal == 1 {
|
|
|
|
expectedPartID1 = msg.PLogMetaData.ID
|
|
|
|
expectedTS1 = msg.Timestamp
|
|
|
|
} else {
|
|
|
|
checkIdentical(t, msg, expectedPartID1, expectedTS1)
|
|
|
|
}
|
|
|
|
if msg.PLogMetaData.Ordinal == 4 && !msg.PLogMetaData.Last {
|
|
|
|
t.Fatalf("Last is not set for last chunk")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Source == "stderr" {
|
|
|
|
if string(msg.Line) != stderrLongLine && string(msg.Line) != stderrTrailingLine {
|
|
|
|
t.Fatalf("Wrong Line: %q, expected 'stderrLongLine' or 'stderrTrailingLine'", msg.Line)
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.PLogMetaData.ID == "" {
|
|
|
|
t.Fatalf("Expected partial metadata. Got nothing")
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.PLogMetaData.Ordinal == 1 {
|
|
|
|
expectedPartID2 = msg.PLogMetaData.ID
|
|
|
|
expectedTS2 = msg.Timestamp
|
|
|
|
} else {
|
|
|
|
checkIdentical(t, msg, expectedPartID2, expectedTS2)
|
|
|
|
}
|
|
|
|
if msg.PLogMetaData.Ordinal == 4 && !msg.PLogMetaData.Last {
|
|
|
|
t.Fatalf("Last is not set for last chunk")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Source == "normal" && msg.PLogMetaData != nil {
|
|
|
|
t.Fatalf("Normal messages should not have PartialLogMetaData")
|
|
|
|
}
|
|
|
|
recvMsgs++
|
|
|
|
}
|
|
|
|
|
|
|
|
if expectedMsgs != recvMsgs {
|
|
|
|
t.Fatalf("Expected msgs: %d Recv msgs: %d", expectedMsgs, recvMsgs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-06 15:50:09 +00:00
|
|
|
type BenchmarkLoggerDummy struct {
|
|
|
|
}
|
|
|
|
|
2016-12-12 14:54:20 +00:00
|
|
|
func (l *BenchmarkLoggerDummy) Log(m *Message) error { PutMessage(m); return nil }
|
2016-06-06 15:50:09 +00:00
|
|
|
|
|
|
|
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 {
|
daemon/logger/BenchmarkCopy: don't call t.Fatal from a goroutine
staticcheck go linter says:
> daemon/logger/copier_test.go:451:2: SA2002: the goroutine calls T.Fatal, which must be called in the same goroutine as the test (staticcheck)
What it doesn't say is why. The reason is, t.Fatal() calls t.FailNow(),
which is expected to stop test execution right now. It does so by
calling runtime.Goexit(), which, unless called from a main goroutine,
does not stop test execution.
Anyway, long story short, if we don't care much about stopping the test
case immediately, we can just replace t.Fatalf() with t.Errorf() which
still marks the test case as failed, but won't stop it immediately.
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2019-08-06 23:41:42 +00:00
|
|
|
b.Error(err)
|
2016-06-06 15:50:09 +00:00
|
|
|
}
|
daemon/logger/BenchmarkCopy: don't call t.Fatal from a goroutine
staticcheck go linter says:
> daemon/logger/copier_test.go:451:2: SA2002: the goroutine calls T.Fatal, which must be called in the same goroutine as the test (staticcheck)
What it doesn't say is why. The reason is, t.Fatal() calls t.FailNow(),
which is expected to stop test execution right now. It does so by
calling runtime.Goexit(), which, unless called from a main goroutine,
does not stop test execution.
Anyway, long story short, if we don't care much about stopping the test
case immediately, we can just replace t.Fatalf() with t.Errorf() which
still marks the test case as failed, but won't stop it immediately.
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2019-08-06 23:41:42 +00:00
|
|
|
b.Error("short write")
|
2016-06-06 15:50:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|