001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017 018 package org.apache.logging.log4j.perf.jmh; 019 020 import java.util.concurrent.TimeUnit; 021 022 import org.apache.logging.log4j.core.util.CachedClock; 023 import org.apache.logging.log4j.core.util.Clock; 024 import org.apache.logging.log4j.core.util.CoarseCachedClock; 025 import org.apache.logging.log4j.core.util.SystemClock; 026 import org.openjdk.jmh.annotations.BenchmarkMode; 027 import org.openjdk.jmh.annotations.GenerateMicroBenchmark; 028 import org.openjdk.jmh.annotations.Level; 029 import org.openjdk.jmh.annotations.Mode; 030 import org.openjdk.jmh.annotations.OutputTimeUnit; 031 import org.openjdk.jmh.annotations.Scope; 032 import org.openjdk.jmh.annotations.Setup; 033 import org.openjdk.jmh.annotations.State; 034 035 /** 036 * Tests performance of various clock implementation. 037 */ 038 // ============================== HOW TO RUN THIS TEST: ==================================== 039 // 040 // single thread: 041 // java -jar log4j-perf/target/microbenchmarks.jar ".*Clocks.*" -f 1 -wi 5 -i 5 042 // 043 // multiple threads (for example, 4 threads): 044 // java -jar log4j-perf/target/microbenchmarks.jar ".*Clocks.*" -f 1 -wi 5 -i 5 -t 4 -si true 045 // 046 // Usage help: 047 // java -jar log4j-perf/target/microbenchmarks.jar -help 048 // 049 @State(Scope.Thread) 050 public class ClocksBenchmark { 051 052 Clock systemClock = new SystemClock(); 053 Clock cachedClock; 054 Clock coarseCachedClock; 055 Clock fixedClock; 056 Clock fixedFinalClock; 057 058 @Setup(Level.Trial) 059 public void up() { 060 cachedClock = CachedClock.instance(); 061 coarseCachedClock = CoarseCachedClock.instance(); 062 fixedClock = new FixedTimeClock(System.nanoTime()); 063 fixedFinalClock = new FixedFinalTimeClock(System.nanoTime()); 064 } 065 066 @GenerateMicroBenchmark 067 @BenchmarkMode(Mode.SampleTime) 068 @OutputTimeUnit(TimeUnit.NANOSECONDS) 069 public void baseline() { 070 } 071 072 @GenerateMicroBenchmark 073 @BenchmarkMode(Mode.SampleTime) 074 @OutputTimeUnit(TimeUnit.NANOSECONDS) 075 public long systemCurrentTimeMillis() { 076 return System.currentTimeMillis(); 077 } 078 079 @GenerateMicroBenchmark 080 @BenchmarkMode(Mode.SampleTime) 081 @OutputTimeUnit(TimeUnit.NANOSECONDS) 082 public long systemClock() { 083 return systemClock.currentTimeMillis(); 084 } 085 086 @GenerateMicroBenchmark 087 @BenchmarkMode(Mode.SampleTime) 088 @OutputTimeUnit(TimeUnit.NANOSECONDS) 089 public long cachedClock() { 090 return cachedClock.currentTimeMillis(); 091 } 092 093 @GenerateMicroBenchmark 094 @BenchmarkMode(Mode.SampleTime) 095 @OutputTimeUnit(TimeUnit.NANOSECONDS) 096 public long coarseCachedClock() { 097 return coarseCachedClock.currentTimeMillis(); 098 } 099 100 @GenerateMicroBenchmark 101 @BenchmarkMode(Mode.SampleTime) 102 @OutputTimeUnit(TimeUnit.NANOSECONDS) 103 public long fixedClock() { 104 return fixedClock.currentTimeMillis(); 105 } 106 107 @GenerateMicroBenchmark 108 @BenchmarkMode(Mode.SampleTime) 109 @OutputTimeUnit(TimeUnit.NANOSECONDS) 110 public long fixedFinalClock() { 111 return fixedFinalClock.currentTimeMillis(); 112 } 113 114 private static final class FixedTimeClock implements Clock { 115 private long fixedTime; 116 117 public FixedTimeClock(long fixedTime) { 118 this.fixedTime = fixedTime; 119 } 120 121 @Override 122 public long currentTimeMillis() { 123 return fixedTime; 124 } 125 } 126 127 private static final class FixedFinalTimeClock implements Clock { 128 private final long fixedFinalTime; 129 130 public FixedFinalTimeClock(long fixedTime) { 131 this.fixedFinalTime = fixedTime; 132 } 133 134 @Override 135 public long currentTimeMillis() { 136 return fixedFinalTime; 137 } 138 } 139 }