go.temporal.io/server@v1.23.0/common/log/zap_logger_bench_test.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package log
    26  
    27  import (
    28  	"testing"
    29  
    30  	"go.uber.org/zap"
    31  
    32  	"go.temporal.io/server/common/log/tag"
    33  )
    34  
    35  /**
    36  $ go test -v -bench=. | grep -E "(Bench)|(ns/op)"
    37  BenchmarkZapLoggerWithFields
    38  BenchmarkZapLoggerWithFields-4            152793              7200 ns/op
    39  BenchmarkLoggerWithFields
    40  BenchmarkLoggerWithFields-4               146850              8370 ns/op
    41  BenchmarkZapLoggerWithoutFields
    42  BenchmarkZapLoggerWithoutFields-4         192972              5885 ns/op
    43  BenchmarkLoggerWithoutFields
    44  BenchmarkLoggerWithoutFields-4            162109              7211 ns/op
    45  */
    46  
    47  func BenchmarkZapLoggerWithFields(b *testing.B) {
    48  	zLogger := buildZapLogger(Config{Level: "info"}, false)
    49  
    50  	for i := 0; i < b.N; i++ {
    51  		zLoggerWith := zLogger.With(zap.Int64("wf-schedule-id", int64(i)), zap.String("cluster-name", "this is a very long value: 1234567890 1234567890 1234567890 1234567890"))
    52  		zLoggerWith.Info("msg to print log, 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890",
    53  			zap.String("wf-namespace", "test-namespace"))
    54  		zLoggerWith.Debug("msg NOT to print log, 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890",
    55  			zap.String("wf-namespace", "test-namespace"))
    56  	}
    57  }
    58  
    59  func BenchmarkLoggerWithFields(b *testing.B) {
    60  	logger := NewZapLogger(buildZapLogger(Config{Level: "info"}, true))
    61  
    62  	for i := 0; i < b.N; i++ {
    63  		loggerWith := logger.With(tag.WorkflowScheduledEventID(int64(i)), tag.ClusterName("this is a very long value: 1234567890 1234567890 1234567890 1234567890"))
    64  		loggerWith.Info("msg to print log, 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890",
    65  			tag.WorkflowNamespace("test-namespace"))
    66  		loggerWith.Debug("msg NOT to print log, 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890",
    67  			tag.WorkflowNamespace("test-namespace"))
    68  	}
    69  }
    70  
    71  func BenchmarkZapLoggerWithoutFields(b *testing.B) {
    72  	zLogger := buildZapLogger(Config{Level: "info"}, false)
    73  
    74  	for i := 0; i < b.N; i++ {
    75  		zLogger.Info("msg to print log, 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890",
    76  			zap.Int64("wf-schedule-id", int64(i)), zap.String("cluster-name", "this is a very long value: 1234567890 1234567890 1234567890 1234567890"),
    77  			zap.String("wf-namespace", "test-namespace"))
    78  		zLogger.Debug("msg NOT to print log, 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890",
    79  			zap.Int64("wf-schedule-id", int64(i)), zap.String("cluster-name", "this is a very long value: 1234567890 1234567890 1234567890 1234567890"),
    80  			zap.String("wf-namespace", "test-namespace"))
    81  	}
    82  }
    83  
    84  func BenchmarkLoggerWithoutFields(b *testing.B) {
    85  	logger := NewZapLogger(buildZapLogger(Config{Level: "info"}, true))
    86  
    87  	for i := 0; i < b.N; i++ {
    88  		logger.Info("msg to print log, 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890",
    89  			tag.WorkflowNamespace("test-namespace"),
    90  			tag.WorkflowScheduledEventID(int64(i)), tag.ClusterName("this is a very long value: 1234567890 1234567890 1234567890 1234567890"))
    91  		logger.Debug("msg NOT to print log, 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890",
    92  			tag.WorkflowNamespace("test-namespace"),
    93  			tag.WorkflowScheduledEventID(int64(i)), tag.ClusterName("this is a very long value: 1234567890 1234567890 1234567890 1234567890"))
    94  	}
    95  }