github.com/Laisky/zap@v1.27.0/zapcore/write_syncer_bench_test.go (about) 1 // Copyright (c) 2016 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package zapcore 22 23 import ( 24 "os" 25 "testing" 26 27 "github.com/Laisky/zap/internal/ztest" 28 "github.com/stretchr/testify/assert" 29 "github.com/stretchr/testify/require" 30 ) 31 32 func BenchmarkMultiWriteSyncer(b *testing.B) { 33 b.Run("2 discarder", func(b *testing.B) { 34 w := NewMultiWriteSyncer( 35 &ztest.Discarder{}, 36 &ztest.Discarder{}, 37 ) 38 b.ResetTimer() 39 b.RunParallel(func(pb *testing.PB) { 40 for pb.Next() { 41 if _, err := w.Write([]byte("foobarbazbabble")); err != nil { 42 b.Fatal(err) 43 } 44 } 45 }) 46 }) 47 b.Run("4 discarder", func(b *testing.B) { 48 w := NewMultiWriteSyncer( 49 &ztest.Discarder{}, 50 &ztest.Discarder{}, 51 &ztest.Discarder{}, 52 &ztest.Discarder{}, 53 ) 54 b.ResetTimer() 55 b.RunParallel(func(pb *testing.PB) { 56 for pb.Next() { 57 if _, err := w.Write([]byte("foobarbazbabble")); err != nil { 58 b.Fatal(err) 59 } 60 } 61 }) 62 }) 63 b.Run("4 discarder with buffer", func(b *testing.B) { 64 w := &BufferedWriteSyncer{ 65 WS: NewMultiWriteSyncer( 66 &ztest.Discarder{}, 67 &ztest.Discarder{}, 68 &ztest.Discarder{}, 69 &ztest.Discarder{}, 70 ), 71 } 72 defer func() { 73 assert.NoError(b, w.Stop(), "Unexpected error stopping buffered write syncer.") 74 }() 75 b.ResetTimer() 76 b.RunParallel(func(pb *testing.PB) { 77 for pb.Next() { 78 if _, err := w.Write([]byte("foobarbazbabble")); err != nil { 79 b.Fatal(err) 80 } 81 } 82 }) 83 }) 84 } 85 86 func BenchmarkWriteSyncer(b *testing.B) { 87 b.Run("write file with no buffer", func(b *testing.B) { 88 file, err := os.CreateTemp(b.TempDir(), "test.log") 89 require.NoError(b, err) 90 91 w := AddSync(file) 92 b.ResetTimer() 93 b.RunParallel(func(pb *testing.PB) { 94 for pb.Next() { 95 if _, err := w.Write([]byte("foobarbazbabble")); err != nil { 96 b.Fatal(err) 97 } 98 } 99 }) 100 }) 101 }