Skip to content

Instantly share code, notes, and snippets.

@ktoso
Created July 17, 2014 09:35
Show Gist options
  • Save ktoso/408b34e5aaaaf564def4 to your computer and use it in GitHub Desktop.
Save ktoso/408b34e5aaaaf564def4 to your computer and use it in GitHub Desktop.
Benching fastest way to keep logLevel publised safely - spoiler: volatile wins
/**
* Copyright (C) 2014 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.event
import java.util.concurrent.TimeUnit
import akka.actor._
import akka.testkit._
import akka.event.Logging.LogLevel
import org.openjdk.jmh.annotations._
@Fork(3)
@State(Scope.Benchmark)
@BenchmarkMode(Array(Mode.Throughput))
@Warmup(iterations = 10)
@Measurement(iterations = 20, timeUnit = TimeUnit.MILLISECONDS)
class LoggingBusBench {
/*
GUARD `ReentrantGuard`, 1 thread reading, 2 threads writing
a.e.LoggingBusBench.g:readWithGuard thrpt 10 1811.514 277.492 ops/ms
a.e.LoggingBusBench.g:writeDebugWithGuard thrpt 10 1174.610 62.096 ops/ms
a.e.LoggingBusBench.g:writeErrorWithGuard thrpt 10 1168.009 36.615 ops/ms
GUARD `ReentrantGuard`, 10 threads reading, 2 threads writing
a.e.LoggingBusBench.g:readWithGuard thrpt 10 12386.594 913.637 ops/ms
a.e.LoggingBusBench.g:writeDebugWithGuard thrpt 10 906.733 43.803 ops/ms
a.e.LoggingBusBench.g:writeErrorWithGuard thrpt 10 909.726 53.118 ops/ms
GUARD `ReentrantGuard`, 20 threads reading, 2 threads writing
a.e.LoggingBusBench.g thrpt 60 18567.983 537.772 ops/ms
a.e.LoggingBusBench.g:readWithGuard thrpt 60 17446.248 566.094 ops/ms
a.e.LoggingBusBench.g:writeDebugWithGuard thrpt 60 563.237 25.699 ops/ms
a.e.LoggingBusBench.g:writeErrorWithGuard thrpt 60 558.498 26.414 ops/ms
VOLATILE - `@volatile var _logLevel`, 1 thread reading, 2 threads writing
a.e.LoggingBusBench.g:readWithGuard thrpt 10 228537.352 12446.850 ops/ms
a.e.LoggingBusBench.g:writeDebugWithGuard thrpt 10 2359.561 106.098 ops/ms
a.e.LoggingBusBench.g:writeErrorWithGuard thrpt 10 1180.412 52.141 ops/ms
VOLATILE - `@volatile var _logLevel`, 10 threads reading, 2 threads writing
a.e.LoggingBusBench.g:readWithGuard thrpt 10 1398386.010 78949.306 ops/ms
a.e.LoggingBusBench.g:writeDebugWithGuard thrpt 10 2578.375 144.119 ops/ms
a.e.LoggingBusBench.g:writeErrorWithGuard thrpt 10 1891.309 194.537 ops/ms
VOLATILE - `@volatile var _logLevel`, 20 threads reading, 2 threads writing
a.e.LoggingBusBench.g:readWithGuard thrpt 60 1175143.477 36065.242 ops/ms
a.e.LoggingBusBench.g:writeDebugWithGuard thrpt 60 951.258 43.214 ops/ms
a.e.LoggingBusBench.g:writeErrorWithGuard thrpt 60 882.009 40.019 ops/ms
READ WRITE GUARD - `ReentrantReadWriteGuard`, 0 threads writing
a.e.LoggingBusBench.g thrpt 10 36055.192 999.268 ops/ms
READ WRITE GUARD - `ReentrantReadWriteGuard`, 1 threads writing
a.e.LoggingBusBench.g:readWithGuard thrpt 10 274.800 314.774 ops/ms
a.e.LoggingBusBench.g:writeErrorWithGuard thrpt 10 2835.826 670.500 ops/ms
READ WRITE GUARD - `ReentrantReadWriteGuard`, 2 threads writing
a.e.LoggingBusBench.g:readWithGuard thrpt 10 12.956 1.998 ops/ms
a.e.LoggingBusBench.g:writeDebugWithGuard thrpt 10 1420.432 75.205 ops/ms
a.e.LoggingBusBench.g:writeErrorWithGuard thrpt 10 1429.204 78.902 ops/ms
READ WRITE GUARD - `ReentrantReadWriteGuard`, 10 threads reading, 2 threads writing
a.e.LoggingBusBench.g:readWithGuard thrpt 10 1632481.289 109734.216 ops/ms
a.e.LoggingBusBench.g:writeDebugWithGuard thrpt 10 1974.814 158.129 ops/ms
a.e.LoggingBusBench.g:writeErrorWithGuard thrpt 10 1890.501 269.516 ops/ms
READ WRITE GUARD - `ReentrantReadWriteGuard`, 20 threads reading, 2 threads writing, V1
a.e.LoggingBusBench.g:readWithGuard thrpt 60 4890.522 92.922 ops/ms
a.e.LoggingBusBench.g:writeDebugWithGuard thrpt 60 0.221 0.289 ops/ms
a.e.LoggingBusBench.g:writeErrorWithGuard thrpt 60 0.139 0.055 ops/ms
*/
implicit var system: ActorSystem = _
var probe: TestProbe = _
var log: BusLogging = _
@Setup
def setup() {
system = ActorSystem("log-touching")
probe = TestProbe()
log = akka.event.Logging(system, probe.ref).asInstanceOf[BusLogging]
}
@TearDown
def shutdown() {
system.shutdown()
system.awaitTermination()
}
@Benchmark
@GroupThreads(20)
@Group("g")
def readWithGuard(): LogLevel =
log.bus.logLevel
@Benchmark
@Group("g")
def writeErrorWithGuard(): Unit =
log.bus.setLogLevel(Logging.ErrorLevel)
@Benchmark
@Group("g")
def writeDebugWithGuard(): Unit =
log.bus.setLogLevel(Logging.DebugLevel)
}
@viktorklang
Copy link

Wins with quite nice margin ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment