copier_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package logger
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "os"
  8. "sync"
  9. "testing"
  10. "time"
  11. )
  12. type TestLoggerJSON struct {
  13. *json.Encoder
  14. mu sync.Mutex
  15. delay time.Duration
  16. }
  17. func (l *TestLoggerJSON) Log(m *Message) error {
  18. if l.delay > 0 {
  19. time.Sleep(l.delay)
  20. }
  21. l.mu.Lock()
  22. defer l.mu.Unlock()
  23. return l.Encode(m)
  24. }
  25. func (l *TestLoggerJSON) Close() error { return nil }
  26. func (l *TestLoggerJSON) Name() string { return "json" }
  27. func TestCopier(t *testing.T) {
  28. stdoutLine := "Line that thinks that it is log line from docker stdout"
  29. stderrLine := "Line that thinks that it is log line from docker stderr"
  30. var stdout bytes.Buffer
  31. var stderr bytes.Buffer
  32. for i := 0; i < 30; i++ {
  33. if _, err := stdout.WriteString(stdoutLine + "\n"); err != nil {
  34. t.Fatal(err)
  35. }
  36. if _, err := stderr.WriteString(stderrLine + "\n"); err != nil {
  37. t.Fatal(err)
  38. }
  39. }
  40. var jsonBuf bytes.Buffer
  41. jsonLog := &TestLoggerJSON{Encoder: json.NewEncoder(&jsonBuf)}
  42. c := NewCopier(
  43. map[string]io.Reader{
  44. "stdout": &stdout,
  45. "stderr": &stderr,
  46. },
  47. jsonLog)
  48. c.Run()
  49. wait := make(chan struct{})
  50. go func() {
  51. c.Wait()
  52. close(wait)
  53. }()
  54. select {
  55. case <-time.After(1 * time.Second):
  56. t.Fatal("Copier failed to do its work in 1 second")
  57. case <-wait:
  58. }
  59. dec := json.NewDecoder(&jsonBuf)
  60. for {
  61. var msg Message
  62. if err := dec.Decode(&msg); err != nil {
  63. if err == io.EOF {
  64. break
  65. }
  66. t.Fatal(err)
  67. }
  68. if msg.Source != "stdout" && msg.Source != "stderr" {
  69. t.Fatalf("Wrong Source: %q, should be %q or %q", msg.Source, "stdout", "stderr")
  70. }
  71. if msg.Source == "stdout" {
  72. if string(msg.Line) != stdoutLine {
  73. t.Fatalf("Wrong Line: %q, expected %q", msg.Line, stdoutLine)
  74. }
  75. }
  76. if msg.Source == "stderr" {
  77. if string(msg.Line) != stderrLine {
  78. t.Fatalf("Wrong Line: %q, expected %q", msg.Line, stderrLine)
  79. }
  80. }
  81. }
  82. }
  83. func TestCopierSlow(t *testing.T) {
  84. stdoutLine := "Line that thinks that it is log line from docker stdout"
  85. var stdout bytes.Buffer
  86. for i := 0; i < 30; i++ {
  87. if _, err := stdout.WriteString(stdoutLine + "\n"); err != nil {
  88. t.Fatal(err)
  89. }
  90. }
  91. var jsonBuf bytes.Buffer
  92. //encoder := &encodeCloser{Encoder: json.NewEncoder(&jsonBuf)}
  93. jsonLog := &TestLoggerJSON{Encoder: json.NewEncoder(&jsonBuf), delay: 100 * time.Millisecond}
  94. c := NewCopier(map[string]io.Reader{"stdout": &stdout}, jsonLog)
  95. c.Run()
  96. wait := make(chan struct{})
  97. go func() {
  98. c.Wait()
  99. close(wait)
  100. }()
  101. <-time.After(150 * time.Millisecond)
  102. c.Close()
  103. select {
  104. case <-time.After(200 * time.Millisecond):
  105. t.Fatalf("failed to exit in time after the copier is closed")
  106. case <-wait:
  107. }
  108. }
  109. type BenchmarkLoggerDummy struct {
  110. }
  111. func (l *BenchmarkLoggerDummy) Log(m *Message) error { return nil }
  112. func (l *BenchmarkLoggerDummy) Close() error { return nil }
  113. func (l *BenchmarkLoggerDummy) Name() string { return "dummy" }
  114. func BenchmarkCopier64(b *testing.B) {
  115. benchmarkCopier(b, 1<<6)
  116. }
  117. func BenchmarkCopier128(b *testing.B) {
  118. benchmarkCopier(b, 1<<7)
  119. }
  120. func BenchmarkCopier256(b *testing.B) {
  121. benchmarkCopier(b, 1<<8)
  122. }
  123. func BenchmarkCopier512(b *testing.B) {
  124. benchmarkCopier(b, 1<<9)
  125. }
  126. func BenchmarkCopier1K(b *testing.B) {
  127. benchmarkCopier(b, 1<<10)
  128. }
  129. func BenchmarkCopier2K(b *testing.B) {
  130. benchmarkCopier(b, 1<<11)
  131. }
  132. func BenchmarkCopier4K(b *testing.B) {
  133. benchmarkCopier(b, 1<<12)
  134. }
  135. func BenchmarkCopier8K(b *testing.B) {
  136. benchmarkCopier(b, 1<<13)
  137. }
  138. func BenchmarkCopier16K(b *testing.B) {
  139. benchmarkCopier(b, 1<<14)
  140. }
  141. func BenchmarkCopier32K(b *testing.B) {
  142. benchmarkCopier(b, 1<<15)
  143. }
  144. func BenchmarkCopier64K(b *testing.B) {
  145. benchmarkCopier(b, 1<<16)
  146. }
  147. func BenchmarkCopier128K(b *testing.B) {
  148. benchmarkCopier(b, 1<<17)
  149. }
  150. func BenchmarkCopier256K(b *testing.B) {
  151. benchmarkCopier(b, 1<<18)
  152. }
  153. func piped(b *testing.B, iterations int, delay time.Duration, buf []byte) io.Reader {
  154. r, w, err := os.Pipe()
  155. if err != nil {
  156. b.Fatal(err)
  157. return nil
  158. }
  159. go func() {
  160. for i := 0; i < iterations; i++ {
  161. time.Sleep(delay)
  162. if n, err := w.Write(buf); err != nil || n != len(buf) {
  163. if err != nil {
  164. b.Fatal(err)
  165. }
  166. b.Fatal(fmt.Errorf("short write"))
  167. }
  168. }
  169. w.Close()
  170. }()
  171. return r
  172. }
  173. func benchmarkCopier(b *testing.B, length int) {
  174. b.StopTimer()
  175. buf := []byte{'A'}
  176. for len(buf) < length {
  177. buf = append(buf, buf...)
  178. }
  179. buf = append(buf[:length-1], []byte{'\n'}...)
  180. b.StartTimer()
  181. for i := 0; i < b.N; i++ {
  182. c := NewCopier(
  183. map[string]io.Reader{
  184. "buffer": piped(b, 10, time.Nanosecond, buf),
  185. },
  186. &BenchmarkLoggerDummy{})
  187. c.Run()
  188. c.Wait()
  189. c.Close()
  190. }
  191. }