mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Fix benchmarks and remove more unnecessary code.
Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
parent
231c5cbd50
commit
a06ad2792a
5 changed files with 103 additions and 149 deletions
|
@ -14,7 +14,7 @@ import (
|
||||||
"github.com/docker/docker/daemon/logger"
|
"github.com/docker/docker/daemon/logger"
|
||||||
"github.com/docker/docker/daemon/logger/loggerutils"
|
"github.com/docker/docker/daemon/logger/loggerutils"
|
||||||
"github.com/docker/docker/pkg/jsonlog"
|
"github.com/docker/docker/pkg/jsonlog"
|
||||||
"github.com/docker/go-units"
|
units "github.com/docker/go-units"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
@ -106,10 +106,8 @@ func writeMessageBuf(w io.Writer, m *logger.Message, extra json.RawMessage, buf
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
logger.PutMessage(m)
|
logger.PutMessage(m)
|
||||||
if _, err := w.Write(buf.Bytes()); err != nil {
|
_, err := w.Write(buf.Bytes())
|
||||||
return errors.Wrap(err, "error writing log entry")
|
return errors.Wrap(err, "error writing log entry")
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func marshalMessage(msg *logger.Message, extra json.RawMessage, buf *bytes.Buffer) error {
|
func marshalMessage(msg *logger.Message, extra json.RawMessage, buf *bytes.Buffer) error {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package jsonfilelog
|
package jsonfilelog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
@ -12,6 +13,8 @@ import (
|
||||||
|
|
||||||
"github.com/docker/docker/daemon/logger"
|
"github.com/docker/docker/daemon/logger"
|
||||||
"github.com/docker/docker/pkg/jsonlog"
|
"github.com/docker/docker/pkg/jsonlog"
|
||||||
|
"github.com/gotestyourself/gotestyourself/fs"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestJSONFileLogger(t *testing.T) {
|
func TestJSONFileLogger(t *testing.T) {
|
||||||
|
@ -54,36 +57,38 @@ func TestJSONFileLogger(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkJSONFileLogger(b *testing.B) {
|
func BenchmarkJSONFileLoggerLog(b *testing.B) {
|
||||||
cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657"
|
tmp := fs.NewDir(b, "bench-jsonfilelog")
|
||||||
tmp, err := ioutil.TempDir("", "docker-logger-")
|
defer tmp.Remove()
|
||||||
if err != nil {
|
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tmp)
|
|
||||||
filename := filepath.Join(tmp, "container.log")
|
|
||||||
l, err := New(logger.Info{
|
|
||||||
ContainerID: cid,
|
|
||||||
LogPath: filename,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
defer l.Close()
|
|
||||||
|
|
||||||
testLine := "Line that thinks that it is log line from docker\n"
|
jsonlogger, err := New(logger.Info{
|
||||||
msg := &logger.Message{Line: []byte(testLine), Source: "stderr", Timestamp: time.Now().UTC()}
|
ContainerID: "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657",
|
||||||
jsonlog, err := (&jsonlog.JSONLog{Log: string(msg.Line) + "\n", Stream: msg.Source, Created: msg.Timestamp}).MarshalJSON()
|
LogPath: tmp.Join("container.log"),
|
||||||
if err != nil {
|
Config: map[string]string{
|
||||||
b.Fatal(err)
|
"labels": "first,second",
|
||||||
|
},
|
||||||
|
ContainerLabels: map[string]string{
|
||||||
|
"first": "label_value",
|
||||||
|
"second": "label_foo",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
require.NoError(b, err)
|
||||||
|
defer jsonlogger.Close()
|
||||||
|
|
||||||
|
msg := &logger.Message{
|
||||||
|
Line: []byte("Line that thinks that it is log line from docker\n"),
|
||||||
|
Source: "stderr",
|
||||||
|
Timestamp: time.Now().UTC(),
|
||||||
}
|
}
|
||||||
b.SetBytes(int64(len(jsonlog)+1) * 30)
|
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
|
require.NoError(b, marshalMessage(msg, jsonlogger.(*JSONFileLogger).extra, buf))
|
||||||
|
b.SetBytes(int64(buf.Len()))
|
||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
for j := 0; j < 30; j++ {
|
if err := jsonlogger.Log(msg); err != nil {
|
||||||
if err := l.Log(msg); err != nil {
|
b.Fatal(err)
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -200,50 +205,3 @@ func TestJSONFileLoggerWithLabelsEnv(t *testing.T) {
|
||||||
t.Fatalf("Wrong log attrs: %q, expected %q", extra, expected)
|
t.Fatalf("Wrong log attrs: %q, expected %q", extra, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkJSONFileLoggerWithReader(b *testing.B) {
|
|
||||||
b.StopTimer()
|
|
||||||
b.ResetTimer()
|
|
||||||
cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657"
|
|
||||||
dir, err := ioutil.TempDir("", "json-logger-bench")
|
|
||||||
if err != nil {
|
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(dir)
|
|
||||||
|
|
||||||
l, err := New(logger.Info{
|
|
||||||
ContainerID: cid,
|
|
||||||
LogPath: filepath.Join(dir, "container.log"),
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
defer l.Close()
|
|
||||||
msg := &logger.Message{Line: []byte("line"), Source: "src1"}
|
|
||||||
jsonlog, err := (&jsonlog.JSONLog{Log: string(msg.Line) + "\n", Stream: msg.Source, Created: msg.Timestamp}).MarshalJSON()
|
|
||||||
if err != nil {
|
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
b.SetBytes(int64(len(jsonlog)+1) * 30)
|
|
||||||
|
|
||||||
b.StartTimer()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
for j := 0; j < 30; j++ {
|
|
||||||
l.Log(msg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
l.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
lw := l.(logger.LogReader).ReadLogs(logger.ReadConfig{Follow: true})
|
|
||||||
watchClose := lw.WatchClose()
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-lw.Msg:
|
|
||||||
case <-watchClose:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
65
daemon/logger/jsonfilelog/read_test.go
Normal file
65
daemon/logger/jsonfilelog/read_test.go
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
package jsonfilelog
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"bytes"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/docker/docker/daemon/logger"
|
||||||
|
"github.com/gotestyourself/gotestyourself/fs"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BenchmarkJSONFileLoggerReadLogs(b *testing.B) {
|
||||||
|
tmp := fs.NewDir(b, "bench-jsonfilelog")
|
||||||
|
defer tmp.Remove()
|
||||||
|
|
||||||
|
jsonlogger, err := New(logger.Info{
|
||||||
|
ContainerID: "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657",
|
||||||
|
LogPath: tmp.Join("container.log"),
|
||||||
|
Config: map[string]string{
|
||||||
|
"labels": "first,second",
|
||||||
|
},
|
||||||
|
ContainerLabels: map[string]string{
|
||||||
|
"first": "label_value",
|
||||||
|
"second": "label_foo",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
require.NoError(b, err)
|
||||||
|
defer jsonlogger.Close()
|
||||||
|
|
||||||
|
msg := &logger.Message{
|
||||||
|
Line: []byte("Line that thinks that it is log line from docker\n"),
|
||||||
|
Source: "stderr",
|
||||||
|
Timestamp: time.Now().UTC(),
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
|
require.NoError(b, marshalMessage(msg, jsonlogger.(*JSONFileLogger).extra, buf))
|
||||||
|
b.SetBytes(int64(buf.Len()))
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
|
||||||
|
chError := make(chan error, b.N+1)
|
||||||
|
go func() {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
chError <- jsonlogger.Log(msg)
|
||||||
|
}
|
||||||
|
chError <- jsonlogger.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
lw := jsonlogger.(*JSONFileLogger).ReadLogs(logger.ReadConfig{Follow: true})
|
||||||
|
watchClose := lw.WatchClose()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-lw.Msg:
|
||||||
|
case <-watchClose:
|
||||||
|
return
|
||||||
|
case err := <-chError:
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,8 +4,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// JSONLog represents a log message, typically a single entry from a given log stream.
|
// JSONLog is a log message, typically a single entry from a given log stream.
|
||||||
// JSONLogs can be easily serialized to and from JSON and support custom formatting.
|
|
||||||
type JSONLog struct {
|
type JSONLog struct {
|
||||||
// Log is the log message
|
// Log is the log message
|
||||||
Log string `json:"log,omitempty"`
|
Log string `json:"log,omitempty"`
|
||||||
|
|
|
@ -17,8 +17,8 @@ type JSONLogs struct {
|
||||||
RawAttrs json.RawMessage `json:"attrs,omitempty"`
|
RawAttrs json.RawMessage `json:"attrs,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSONBuf is based on the same method from JSONLog
|
// MarshalJSONBuf is an optimized JSON marshaller that avoids reflection
|
||||||
// It has been modified to take into account the necessary changes.
|
// and unnecessary allocation.
|
||||||
func (mj *JSONLogs) MarshalJSONBuf(buf *bytes.Buffer) error {
|
func (mj *JSONLogs) MarshalJSONBuf(buf *bytes.Buffer) error {
|
||||||
var first = true
|
var first = true
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ func (mj *JSONLogs) MarshalJSONBuf(buf *bytes.Buffer) error {
|
||||||
buf.WriteString(`,`)
|
buf.WriteString(`,`)
|
||||||
}
|
}
|
||||||
buf.WriteString(`"stream":`)
|
buf.WriteString(`"stream":`)
|
||||||
ffjsonWriteJSONString(buf, mj.Stream)
|
ffjsonWriteJSONBytesAsString(buf, []byte(mj.Stream))
|
||||||
}
|
}
|
||||||
if len(mj.RawAttrs) > 0 {
|
if len(mj.RawAttrs) > 0 {
|
||||||
if first {
|
if first {
|
||||||
|
@ -61,72 +61,6 @@ func (mj *JSONLogs) MarshalJSONBuf(buf *bytes.Buffer) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ffjsonWriteJSONString(buf *bytes.Buffer, s string) {
|
|
||||||
const hex = "0123456789abcdef"
|
|
||||||
|
|
||||||
buf.WriteByte('"')
|
|
||||||
start := 0
|
|
||||||
for i := 0; i < len(s); {
|
|
||||||
if b := s[i]; b < utf8.RuneSelf {
|
|
||||||
if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' {
|
|
||||||
i++
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if start < i {
|
|
||||||
buf.WriteString(s[start:i])
|
|
||||||
}
|
|
||||||
switch b {
|
|
||||||
case '\\', '"':
|
|
||||||
buf.WriteByte('\\')
|
|
||||||
buf.WriteByte(b)
|
|
||||||
case '\n':
|
|
||||||
buf.WriteByte('\\')
|
|
||||||
buf.WriteByte('n')
|
|
||||||
case '\r':
|
|
||||||
buf.WriteByte('\\')
|
|
||||||
buf.WriteByte('r')
|
|
||||||
default:
|
|
||||||
|
|
||||||
buf.WriteString(`\u00`)
|
|
||||||
buf.WriteByte(hex[b>>4])
|
|
||||||
buf.WriteByte(hex[b&0xF])
|
|
||||||
}
|
|
||||||
i++
|
|
||||||
start = i
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
c, size := utf8.DecodeRuneInString(s[i:])
|
|
||||||
if c == utf8.RuneError && size == 1 {
|
|
||||||
if start < i {
|
|
||||||
buf.WriteString(s[start:i])
|
|
||||||
}
|
|
||||||
buf.WriteString(`\ufffd`)
|
|
||||||
i += size
|
|
||||||
start = i
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if c == '\u2028' || c == '\u2029' {
|
|
||||||
if start < i {
|
|
||||||
buf.WriteString(s[start:i])
|
|
||||||
}
|
|
||||||
buf.WriteString(`\u202`)
|
|
||||||
buf.WriteByte(hex[c&0xF])
|
|
||||||
i += size
|
|
||||||
start = i
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
i += size
|
|
||||||
}
|
|
||||||
if start < len(s) {
|
|
||||||
buf.WriteString(s[start:])
|
|
||||||
}
|
|
||||||
buf.WriteByte('"')
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is based on ffjsonWriteJSONString. It has been changed
|
|
||||||
// to accept a string passed as a slice of bytes.
|
|
||||||
// TODO: remove duplication with ffjsonWriteJSONString
|
|
||||||
func ffjsonWriteJSONBytesAsString(buf *bytes.Buffer, s []byte) {
|
func ffjsonWriteJSONBytesAsString(buf *bytes.Buffer, s []byte) {
|
||||||
const hex = "0123456789abcdef"
|
const hex = "0123456789abcdef"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue