Benchmark the time it takes to write x bytes of data to a logger, and optionally rotate and compress it.
(b *testing.B)
| 110 | |
| 111 | // Benchmark the time it takes to write x bytes of data to a logger, and optionally rotate and compress it. |
| 112 | func BenchmarkLogRotation(b *testing.B) { |
| 113 | |
| 114 | tests := []struct { |
| 115 | rotate bool |
| 116 | compress bool |
| 117 | numBytes int |
| 118 | }{ |
| 119 | {rotate: false, compress: false, numBytes: 0}, |
| 120 | {rotate: false, compress: false, numBytes: 1024 * 1000}, // 1MB |
| 121 | {rotate: false, compress: false, numBytes: 1024 * 100000}, // 100MB |
| 122 | {rotate: true, compress: false, numBytes: 0}, |
| 123 | {rotate: true, compress: false, numBytes: 1024 * 1000}, // 1MB |
| 124 | {rotate: true, compress: false, numBytes: 1024 * 100000}, // 100MB |
| 125 | {rotate: true, compress: true, numBytes: 0}, |
| 126 | {rotate: true, compress: true, numBytes: 1024 * 1000}, // 1MB |
| 127 | {rotate: true, compress: true, numBytes: 1024 * 100000}, // 100MB |
| 128 | } |
| 129 | |
| 130 | for _, test := range tests { |
| 131 | b.Run(fmt.Sprintf("rotate:%t-compress:%t-bytes:%v", test.rotate, test.compress, test.numBytes), func(bm *testing.B) { |
| 132 | data := FastRandBytes(bm, test.numBytes) |
| 133 | |
| 134 | logPath := b.TempDir() |
| 135 | logger := lumberjack.Logger{Filename: filepath.Join(logPath, "output.log"), Compress: test.compress} |
| 136 | |
| 137 | bm.ResetTimer() |
| 138 | for i := 0; i < bm.N; i++ { |
| 139 | _, _ = logger.Write(data) |
| 140 | if test.rotate { |
| 141 | _ = logger.Rotate() |
| 142 | } |
| 143 | } |
| 144 | bm.StopTimer() |
| 145 | |
| 146 | // Tidy up temp log files in a retry loop because |
| 147 | // we can't remove temp dir while the async compression is still writing log files |
| 148 | assert.NoError(bm, logger.Close()) |
| 149 | ctx := TestCtx(bm) |
| 150 | err, _ := RetryLoop(ctx, "benchmark-logrotate-teardown", |
| 151 | func() (shouldRetry bool, err error, value interface{}) { |
| 152 | err = os.RemoveAll(logPath) |
| 153 | return err != nil, err, nil |
| 154 | }, |
| 155 | CreateDoublingSleeperFunc(3, 250), |
| 156 | ) |
| 157 | assert.NoError(bm, err) |
| 158 | }) |
| 159 | } |
| 160 | |
| 161 | } |
| 162 | |
| 163 | func TestLogColor(t *testing.T) { |
| 164 | origColor := consoleLogger.Load().ColorEnabled |
nothing calls this directly
no test coverage detected