Skip to content

Commit

Permalink
benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoon committed Jan 15, 2024
1 parent 21f975e commit e3c09c6
Showing 1 changed file with 36 additions and 56 deletions.
92 changes: 36 additions & 56 deletions benchmarks/src/main/scala/zio/logging/FilterBenchmarks.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package zio.logging

import org.openjdk.jmh.annotations._
import zio.{ ConfigProvider, LogLevel, Runtime, Unsafe, ZIO, ZLayer }
import zio.{ LogLevel, Runtime, Unsafe, ZIO, ZLayer }

import java.util.concurrent.TimeUnit
import scala.util.Random
Expand Down Expand Up @@ -42,65 +42,40 @@ class FilterBenchmarks {
.install
}

val filterByLogLevelAndNameLogging: ZLayer[Any, Nothing, Unit] = {
val filter = LogFilter
.LogLevelByNameConfig(
LogLevel.Debug,
"a.b.c" -> LogLevel.Info,
"a.b.d" -> LogLevel.Warning,
"e" -> LogLevel.Info,
"f.g" -> LogLevel.Error,
"f" -> LogLevel.Info
)
.toFilter
val filterConfig: LogFilter.LogLevelByNameConfig = LogFilter.LogLevelByNameConfig(
LogLevel.Debug,
"a.b.c" -> LogLevel.Info,
"a.b.d" -> LogLevel.Warning,
"e" -> LogLevel.Info,
"f.g" -> LogLevel.Error,
"f" -> LogLevel.Info
)

val filterByLogLevelAndNameLogging: ZLayer[Any, Nothing, Unit] =
Runtime.removeDefaultLoggers >>> makeSystemOutLogger(LogFormat.default.toLogger)
.filter(filter)
.filter(filterConfig.toFilter)
.install
}

val cachedFilterByLogLevelAndNameLogging: ZLayer[Any, Nothing, Unit] = {
val filter = LogFilter
.LogLevelByNameConfig(
LogLevel.Debug,
"a.b.c" -> LogLevel.Info,
"a.b.d" -> LogLevel.Warning,
"e" -> LogLevel.Info,
"f.g" -> LogLevel.Error,
"f" -> LogLevel.Info
)
.toFilter
.cached

val cachedFilterByLogLevelAndNameLogging: ZLayer[Any, Nothing, Unit] =
Runtime.removeDefaultLoggers >>> makeSystemOutLogger(LogFormat.default.toLogger)
.filter(filter)
.filter(filterConfig.toFilter.cached)
.install
}

val reconfigurableFilterByLogLevelAndNameLogging: ZLayer[Any, Nothing, Unit] = {
val logFormat =
"%label{timestamp}{%fixed{32}{%timestamp}} %label{level}{%level} %label{thread}{%fiberId} %label{message}{%message} %label{cause}{%cause}"

val configProvider: ConfigProvider = ConfigProvider.fromMap(
Map(
"logger/format" -> logFormat,
"logger/filter/rootLevel" -> LogLevel.Debug.label,
"logger/filter/mappings/a.b.c" -> LogLevel.Info.label,
"logger/filter/mappings/a.b.d" -> LogLevel.Warning.label,
"logger/filter/mappings/e" -> LogLevel.Info.label,
"logger/filter/mappings/f.g" -> LogLevel.Error.label,
"logger/filter/mappings/f" -> LogLevel.Info.label
),
"/"
)
val reconfigurableFilterByLogLevelAndNameLogging: ZLayer[Any, Nothing, Unit] =
Runtime.removeDefaultLoggers >>> ReconfigurableLogger
.make[Any, Nothing, String, Any, ConsoleLoggerConfig](
ZIO.succeed(ConsoleLoggerConfig(LogFormat.default, filterConfig)),
(config, _) => makeSystemOutLogger(config.format.toLogger).filter(config.toFilter)
)
.installUnscoped[Any]

Runtime.removeDefaultLoggers >>> Runtime.setConfigProvider(configProvider) >>> ReconfigurableLogger
val reconfigurableCachedFilterByLogLevelAndNameLogging: ZLayer[Any, Nothing, Unit] =
Runtime.removeDefaultLoggers >>> ReconfigurableLogger
.make[Any, Nothing, String, Any, ConsoleLoggerConfig](
ConsoleLoggerConfig.load().orDie,
(config, _) => makeConsoleLogger(config)
ZIO.succeed(ConsoleLoggerConfig(LogFormat.default, filterConfig)),
(config, _) => makeSystemOutLogger(config.format.toLogger).filter(config.toFilter.cached)
)
.installUnscoped[Any]
}

val names: List[String] = List(
"a",
Expand Down Expand Up @@ -138,16 +113,17 @@ class FilterBenchmarks {
}

/**
* 2023/12/25 Initial results
* 2023/12/26 Initial results
*
* jmh:run -i 3 -wi 3 -f1 -t1 .*FilterBenchmarks.*
*
* Benchmark Mode Cnt Score Error Units
* FilterBenchmarks.cachedFilterByLogLevelAndNameLog thrpt 3 15098.312 ± 4204.210 ops/s
* FilterBenchmarks.filterByLogLevelAndNameLog thrpt 3 13100.786 ± 2017.585 ops/s
* FilterBenchmarks.handWrittenFilterLog thrpt 3 10864.716 ± 482.042 ops/s
* FilterBenchmarks.noFilteringLog thrpt 3 8813.185 ± 10371.239 ops/s
* FilterBenchmarks.reconfigurableFilterByLogLevelAndNameLog thrpt 3 3334.433 ± 216.060 ops/s
* Benchmark Mode Cnt Score Error Units
* FilterBenchmarks.cachedFilterByLogLevelAndNameLog thrpt 3 14795.547 ± 1372.850 ops/s
* FilterBenchmarks.filterByLogLevelAndNameLog thrpt 3 15093.994 ± 1230.494 ops/s
* FilterBenchmarks.handWrittenFilterLog thrpt 3 13157.888 ± 10193.287 ops/s
* FilterBenchmarks.noFilteringLog thrpt 3 11043.746 ± 230.514 ops/s
* FilterBenchmarks.reconfigurableCachedFilterByLogLevelAndNameLog thrpt 3 7532.412 ± 415.760 ops/s
* FilterBenchmarks.reconfigurableFilterByLogLevelAndNameLog thrpt 3 7482.096 ± 628.534 ops/s
*/

@Benchmark
Expand All @@ -170,4 +146,8 @@ class FilterBenchmarks {
def reconfigurableFilterByLogLevelAndNameLog(): Unit =
testLoggingWith(reconfigurableFilterByLogLevelAndNameLogging)

@Benchmark
def reconfigurableCachedFilterByLogLevelAndNameLog(): Unit =
testLoggingWith(reconfigurableCachedFilterByLogLevelAndNameLogging)

}

0 comments on commit e3c09c6

Please sign in to comment.