github.com/LazyboyChen7/engine@v17.12.1-ce-rc2+incompatible/daemon/logger/jsonfilelog/read_test.go (about)

     1  package jsonfilelog
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/docker/docker/daemon/logger"
     9  	"github.com/gotestyourself/gotestyourself/fs"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func BenchmarkJSONFileLoggerReadLogs(b *testing.B) {
    14  	tmp := fs.NewDir(b, "bench-jsonfilelog")
    15  	defer tmp.Remove()
    16  
    17  	jsonlogger, err := New(logger.Info{
    18  		ContainerID: "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657",
    19  		LogPath:     tmp.Join("container.log"),
    20  		Config: map[string]string{
    21  			"labels": "first,second",
    22  		},
    23  		ContainerLabels: map[string]string{
    24  			"first":  "label_value",
    25  			"second": "label_foo",
    26  		},
    27  	})
    28  	require.NoError(b, err)
    29  	defer jsonlogger.Close()
    30  
    31  	msg := &logger.Message{
    32  		Line:      []byte("Line that thinks that it is log line from docker\n"),
    33  		Source:    "stderr",
    34  		Timestamp: time.Now().UTC(),
    35  	}
    36  
    37  	buf := bytes.NewBuffer(nil)
    38  	require.NoError(b, marshalMessage(msg, nil, buf))
    39  	b.SetBytes(int64(buf.Len()))
    40  
    41  	b.ResetTimer()
    42  
    43  	chError := make(chan error, b.N+1)
    44  	go func() {
    45  		for i := 0; i < b.N; i++ {
    46  			chError <- jsonlogger.Log(msg)
    47  		}
    48  		chError <- jsonlogger.Close()
    49  	}()
    50  
    51  	lw := jsonlogger.(*JSONFileLogger).ReadLogs(logger.ReadConfig{Follow: true})
    52  	watchClose := lw.WatchClose()
    53  	for {
    54  		select {
    55  		case <-lw.Msg:
    56  		case <-watchClose:
    57  			return
    58  		case err := <-chError:
    59  			if err != nil {
    60  				b.Fatal(err)
    61  			}
    62  		}
    63  	}
    64  }