benchmark_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package zap
  2. import (
  3. "errors"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/require"
  7. "github.com/ydb-platform/ydb/library/go/core/log"
  8. "go.uber.org/zap"
  9. )
  10. func BenchmarkZapLogger(b *testing.B) {
  11. // use config for both loggers
  12. cfg := NewDeployConfig()
  13. cfg.OutputPaths = nil
  14. cfg.ErrorOutputPaths = nil
  15. b.Run("stock", func(b *testing.B) {
  16. for _, level := range log.Levels() {
  17. b.Run(level.String(), func(b *testing.B) {
  18. cfg.Level = zap.NewAtomicLevelAt(ZapifyLevel(level))
  19. logger, err := cfg.Build()
  20. require.NoError(b, err)
  21. funcs := []func(string, ...zap.Field){
  22. logger.Debug,
  23. logger.Info,
  24. logger.Warn,
  25. logger.Error,
  26. logger.Fatal,
  27. }
  28. message := "test"
  29. fields := []zap.Field{
  30. zap.String("test", "test"),
  31. zap.Bool("test", true),
  32. zap.Int("test", 42),
  33. }
  34. b.ReportAllocs()
  35. b.ResetTimer()
  36. for i := 0; i < b.N; i++ {
  37. funcs[i%(len(funcs)-1)](message, fields...)
  38. }
  39. })
  40. }
  41. })
  42. b.Run("wrapped", func(b *testing.B) {
  43. for _, level := range log.Levels() {
  44. b.Run(level.String(), func(b *testing.B) {
  45. cfg.Level = zap.NewAtomicLevelAt(ZapifyLevel(level))
  46. logger, err := New(cfg)
  47. require.NoError(b, err)
  48. funcs := []func(string, ...log.Field){
  49. logger.Debug,
  50. logger.Info,
  51. logger.Warn,
  52. logger.Error,
  53. logger.Fatal,
  54. }
  55. message := "test"
  56. fields := []log.Field{
  57. log.String("test", "test"),
  58. log.Bool("test", true),
  59. log.Int("test", 42),
  60. }
  61. b.ReportAllocs()
  62. b.ResetTimer()
  63. for i := 0; i < b.N; i++ {
  64. funcs[i%(len(funcs)-1)](message, fields...)
  65. }
  66. })
  67. }
  68. })
  69. }
  70. func BenchmarkZapifyField(b *testing.B) {
  71. fields := []log.Field{
  72. log.Nil("test"),
  73. log.String("test", "test"),
  74. log.Binary("test", []byte("test")),
  75. log.Bool("test", true),
  76. log.Int("test", 42),
  77. log.UInt("test", 42),
  78. log.Float64("test", 42),
  79. log.Time("test", time.Now()),
  80. log.Duration("test", time.Second),
  81. log.NamedError("test", errors.New("test")),
  82. log.Strings("test", []string{"test"}),
  83. log.Any("test", "test"),
  84. log.Reflect("test", "test"),
  85. log.ByteString("test", []byte("test")),
  86. }
  87. b.ReportAllocs()
  88. b.ResetTimer()
  89. for i := 0; i < b.N; i++ {
  90. zapifyField(fields[i%(len(fields)-1)])
  91. }
  92. }
  93. func BenchmarkZapifyFields(b *testing.B) {
  94. fields := []log.Field{
  95. log.Nil("test"),
  96. log.String("test", "test"),
  97. log.Binary("test", []byte("test")),
  98. log.Bool("test", true),
  99. log.Int("test", 42),
  100. log.UInt("test", 42),
  101. log.Float64("test", 42),
  102. log.Time("test", time.Now()),
  103. log.Duration("test", time.Second),
  104. log.NamedError("test", errors.New("test")),
  105. log.Strings("test", []string{"test"}),
  106. log.Any("test", "test"),
  107. log.Reflect("test", "test"),
  108. log.ByteString("test", []byte("test")),
  109. }
  110. b.ReportAllocs()
  111. b.ResetTimer()
  112. for i := 0; i < b.N; i++ {
  113. zapifyFields(fields...)
  114. }
  115. }