...

Source file src/k8s.io/component-base/logs/json/json_benchmark_test.go

Documentation: k8s.io/component-base/logs/json

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package json
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"go.uber.org/zap/zapcore"
    24  )
    25  
    26  var writer = zapcore.AddSync(&writeSyncer{})
    27  
    28  func BenchmarkInfoLoggerInfo(b *testing.B) {
    29  	logger, _ := NewJSONLogger(0, writer, nil, nil)
    30  	b.ResetTimer()
    31  	b.RunParallel(func(pb *testing.PB) {
    32  		for pb.Next() {
    33  			logger.Info("test",
    34  				"str", "foo",
    35  				"int64A", int64(1),
    36  				"int64B", int64(1),
    37  				"float64", float64(1.0),
    38  				"string1", "\n",
    39  				"string2", "💩",
    40  				"string3", "🤔",
    41  				"string4", "🙊",
    42  				"bool", true,
    43  				"request", struct {
    44  					Method  string `json:"method"`
    45  					Timeout int    `json:"timeout"`
    46  					secret  string
    47  				}{
    48  					Method:  "GET",
    49  					Timeout: 10,
    50  					secret:  "pony",
    51  				},
    52  			)
    53  		}
    54  	})
    55  }
    56  
    57  func BenchmarkZapLoggerError(b *testing.B) {
    58  	logger, _ := NewJSONLogger(0, writer, nil, nil)
    59  	b.ResetTimer()
    60  	b.RunParallel(func(pb *testing.PB) {
    61  		for pb.Next() {
    62  			logger.Error(fmt.Errorf("test for error:%s", "default"),
    63  				"test",
    64  				"str", "foo",
    65  				"int64A", int64(1),
    66  				"int64B", int64(1),
    67  				"float64", float64(1.0),
    68  				"string1", "\n",
    69  				"string2", "💩",
    70  				"string3", "🤔",
    71  				"string4", "🙊",
    72  				"bool", true,
    73  				"request", struct {
    74  					Method  string `json:"method"`
    75  					Timeout int    `json:"timeout"`
    76  					secret  string
    77  				}{
    78  					Method:  "GET",
    79  					Timeout: 10,
    80  					secret:  "pony",
    81  				},
    82  			)
    83  		}
    84  	})
    85  }
    86  
    87  func BenchmarkZapLoggerV(b *testing.B) {
    88  	logger, _ := NewJSONLogger(1, writer, nil, nil)
    89  	b.ResetTimer()
    90  	b.RunParallel(func(pb *testing.PB) {
    91  		for pb.Next() {
    92  			logger.V(1).Info("test",
    93  				"str", "foo",
    94  				"int64A", int64(1),
    95  				"int64B", int64(1),
    96  				"float64", float64(1.0),
    97  				"string1", "\n",
    98  				"string2", "💩",
    99  				"string3", "🤔",
   100  				"string4", "🙊",
   101  				"bool", true,
   102  				"request", struct {
   103  					Method  string `json:"method"`
   104  					Timeout int    `json:"timeout"`
   105  					secret  string
   106  				}{
   107  					Method:  "GET",
   108  					Timeout: 10,
   109  					secret:  "pony",
   110  				},
   111  			)
   112  		}
   113  	})
   114  }
   115  
   116  type writeSyncer struct{}
   117  
   118  var _ zapcore.WriteSyncer = (*writeSyncer)(nil)
   119  
   120  func (w writeSyncer) Write(p []byte) (n int, err error) {
   121  	return len(p), nil
   122  }
   123  
   124  func (w writeSyncer) Sync() error {
   125  	return nil
   126  }
   127  

View as plain text