Merge pull request #15553 from ktoso/port-volatileLogLevel-ktoso

=act #15488 add @volatile logLevel, and benchmark (for validation)
This commit is contained in:
Konrad Malawski 2014-07-17 17:20:41 +02:00
commit 466c20e3cd
2 changed files with 57 additions and 4 deletions

View file

@ -32,9 +32,9 @@ trait LoggingBus extends ActorEventBus {
import Logging._
private val guard = new ReentrantGuard //Switch to ReentrantReadWrite
private val guard = new ReentrantGuard
private var loggers = Seq.empty[ActorRef]
private var _logLevel: LogLevel = _
@volatile private var _logLevel: LogLevel = _
/**
* Query currently set log level. See object Logging for more information.
@ -52,16 +52,17 @@ trait LoggingBus extends ActorEventBus {
* subscriptions!
*/
def setLogLevel(level: LogLevel): Unit = guard.withGuard {
val logLvl = _logLevel // saves (2 * AllLogLevel.size - 1) volatile reads (because of the loops below)
for {
l AllLogLevels
// subscribe if previously ignored and now requested
if l > _logLevel && l <= level
if l > logLvl && l <= level
log loggers
} subscribe(log, classFor(l))
for {
l AllLogLevels
// unsubscribe if previously registered and now ignored
if l <= _logLevel && l > level
if l <= logLvl && l > level
log loggers
} unsubscribe(log, classFor(l))
_logLevel = level

View file

@ -0,0 +1,52 @@
/**
* Copyright (C) 2014 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.event
import java.util.concurrent.TimeUnit
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 LogLevelAccessBenchmark {
/*
volatile logLevel, guard on loggers
20 readers, 2 writers
a.e.LogLevelAccessBenchmark.g thrpt 60 1862566.204 37860.541 ops/ms
a.e.LogLevelAccessBenchmark.g:readLogLevel thrpt 60 1860031.729 37834.335 ops/ms
a.e.LogLevelAccessBenchmark.g:writeLogLevel_1 thrpt 60 1289.452 45.403 ops/ms
a.e.LogLevelAccessBenchmark.g:writeLogLevel_2 thrpt 60 1245.023 51.071 ops/ms
*/
val NoopBus = new LoggingBus {
override def subscribe(subscriber: Subscriber, to: Classifier): Boolean = true
override def publish(event: Event): Unit = ()
override def unsubscribe(subscriber: Subscriber, from: Classifier): Boolean = true
override def unsubscribe(subscriber: Subscriber): Unit = ()
}
var log: BusLogging = akka.event.Logging(NoopBus, "").asInstanceOf[BusLogging]
@Benchmark
@GroupThreads(20)
@Group("g")
def readLogLevel(): LogLevel =
log.bus.logLevel
@Benchmark
@Group("g")
def setLogLevel_1(): Unit =
log.bus.setLogLevel(Logging.ErrorLevel)
@Benchmark
@Group("g")
def setLogLevel_2(): Unit =
log.bus.setLogLevel(Logging.DebugLevel)
}