craigcondit commented on code in PR #611: URL: https://github.com/apache/yunikorn-k8shim/pull/611#discussion_r1231328943
########## pkg/log/logger_test.go: ########## @@ -0,0 +1,244 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package log + +import ( + "fmt" + "os" + "testing" + "time" + + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "gotest.tools/v3/assert" +) + +var logDir string +var logFile string + +var iterations = 100000 + +func TestLoggerIds(t *testing.T) { + _ = Logger() + + // validate logger count + assert.Equal(t, 4, len(loggers), "wrong logger count") + + // validate that all loggers are populated and have sequential ids + for i := 0; i < len(loggers); i++ { + handle := loggers[i] + assert.Assert(t, handle != nil, "nil handle for index", i) + assert.Equal(t, handle.id, i+1, "wrong id", handle.name) + } +} + +func BenchmarkLegacyLoggerDebug(b *testing.B) { + benchmarkLegacyLoggerDebug(b.N) +} + +func TestLegacyLoggerDebug(t *testing.T) { + nsOp := benchmarkLegacyLoggerDebug(iterations) + RootLogger().Info("log.Logger() performance", zap.Int64("debug (ns/op)", nsOp)) +} + +func benchmarkLegacyLoggerDebug(iterations int) int64 { + _ = Logger() + initTestLogger() + defer resetTestLogger() + start := time.Now() + for i := 0; i < iterations; i++ { + RootLogger().Debug("test", zap.String("foo", "bar")) + } + return (time.Since(start).Nanoseconds()) / int64(iterations) +} + +func BenchmarkLegacyLoggerInfo(b *testing.B) { + benchmarkLegacyLoggerInfo(b.N) +} + +func TestLegacyLoggerInfo(t *testing.T) { + nsOp := benchmarkLegacyLoggerInfo(iterations) + RootLogger().Info("log.Logger() performance", zap.Int64("info (ns/op)", nsOp)) +} + +func benchmarkLegacyLoggerInfo(iterations int) int64 { + _ = Logger() + initTestLogger() + defer resetTestLogger() + start := time.Now() + for i := 0; i < iterations; i++ { + RootLogger().Info("test", zap.String("foo", "bar")) + } + return (time.Since(start).Nanoseconds()) / int64(iterations) +} + +func BenchmarkScopedLoggerDebug(b *testing.B) { + benchmarkScopedLoggerDebug(b.N) +} + +func TestScopedLoggerDebug(t *testing.T) { + nsOp := benchmarkScopedLoggerDebug(iterations) + Log(Test).Info("log.Log(...) performance (root=INFO)", zap.Int64("debug (ns/op)", nsOp)) +} + +func benchmarkScopedLoggerDebug(iterations int) int64 { + _ = Logger() + initTestLogger() + defer resetTestLogger() + UpdateLoggingConfig(map[string]string{ + "log.level": "INFO", + }) + start := time.Now() + for i := 0; i < iterations; i++ { + Log(K8Shim).Debug("test", zap.String("foo", "bar")) + } + return (time.Since(start).Nanoseconds()) / int64(iterations) +} + +func BenchmarkScopedLoggerInfo(b *testing.B) { + benchmarkScopedLoggerInfo(b.N) +} + +func TestScopedLoggerInfo(t *testing.T) { + nsOp := benchmarkScopedLoggerInfo(iterations) + Log(Test).Info("log.Log(...) performance (root=INFO)", zap.Int64("info (ns/op)", nsOp)) +} + +func benchmarkScopedLoggerInfo(iterations int) int64 { + _ = Logger() + initTestLogger() + defer resetTestLogger() + UpdateLoggingConfig(map[string]string{ + "log.level": "INFO", + }) + start := time.Now() + for i := 0; i < iterations; i++ { + Log(K8Shim).Info("test", zap.String("foo", "bar")) + } + return (time.Since(start).Nanoseconds()) / int64(iterations) +} + +func BenchmarkScopedLoggerDebugEnabled(b *testing.B) { + benchmarkScopedLoggerDebugEnabled(b.N) +} + +func TestScopedLoggerDebugEnabled(t *testing.T) { + nsOp := benchmarkScopedLoggerDebugEnabled(iterations) + Log(Test).Info("log.Log(...) performance (root=DEBUG)", zap.Int64("debug (ns/op)", nsOp)) +} + +func benchmarkScopedLoggerDebugEnabled(iterations int) int64 { + _ = Logger() + initTestLogger() + defer resetTestLogger() + UpdateLoggingConfig(map[string]string{ + "log.test.level": "DEBUG", + }) + start := time.Now() + for i := 0; i < iterations; i++ { + Log(Test).Debug("test", zap.String("foo", "bar")) + } + return (time.Since(start).Nanoseconds()) / int64(iterations) +} + +func BenchmarkScopedLoggerInfoFiltered(b *testing.B) { + benchmarkScopedLoggerInfoFiltered(b.N) +} + +func TestScopedLoggerInfoFiltered(t *testing.T) { + nsOp := benchmarkScopedLoggerInfoFiltered(iterations) + Log(Test).Info("log.Log(...) performance (root=DEBUG)", zap.Int64("info (ns/op)", nsOp)) +} + +func benchmarkScopedLoggerInfoFiltered(iterations int) int64 { + _ = Logger() + initTestLogger() + defer resetTestLogger() + UpdateLoggingConfig(map[string]string{ + "log.test.level": "DEBUG", Review Comment: Because the log level is implemented as a pre-check before we get to the root (real) logger, there's two scenarios: 1) Log level matches at both levels (i.e. root is INFO, specific logger is also INFO) 2) Log level does not match (i.e. root is DEBUG, specific logger is INFO) In the second case this is measuring the performance of logging at DEBUG level when the logger is set to INFO, so the filtering has to happen in our code vs. in zap. The overhead should be minimal, but this test just verifies that. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
