View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  
18  package org.apache.logging.log4j.perf.jmh;
19  
20  import java.util.concurrent.TimeUnit;
21  
22  import org.apache.logging.log4j.core.util.CachedClock;
23  import org.apache.logging.log4j.core.util.Clock;
24  import org.apache.logging.log4j.core.util.CoarseCachedClock;
25  import org.apache.logging.log4j.core.util.SystemClock;
26  import org.openjdk.jmh.annotations.BenchmarkMode;
27  import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
28  import org.openjdk.jmh.annotations.Level;
29  import org.openjdk.jmh.annotations.Mode;
30  import org.openjdk.jmh.annotations.OutputTimeUnit;
31  import org.openjdk.jmh.annotations.Scope;
32  import org.openjdk.jmh.annotations.Setup;
33  import org.openjdk.jmh.annotations.State;
34  
35  /**
36   * Tests performance of various clock implementation.
37   */
38  // ============================== HOW TO RUN THIS TEST: ====================================
39  //
40  // single thread:
41  // java -jar log4j-perf/target/microbenchmarks.jar ".*Clocks.*" -f 1 -wi 5 -i 5
42  //
43  // multiple threads (for example, 4 threads):
44  // java -jar log4j-perf/target/microbenchmarks.jar ".*Clocks.*" -f 1 -wi 5 -i 5 -t 4 -si true
45  //
46  // Usage help:
47  // java -jar log4j-perf/target/microbenchmarks.jar -help
48  //
49  @State(Scope.Thread)
50  public class ClocksBenchmark {
51  
52      Clock systemClock = new SystemClock();
53      Clock cachedClock;
54      Clock coarseCachedClock;
55      Clock fixedClock;
56      Clock fixedFinalClock;
57  
58      @Setup(Level.Trial)
59      public void up() {
60          cachedClock = CachedClock.instance();
61          coarseCachedClock = CoarseCachedClock.instance();
62          fixedClock = new FixedTimeClock(System.nanoTime());
63          fixedFinalClock = new FixedFinalTimeClock(System.nanoTime());
64      }
65  
66      @GenerateMicroBenchmark
67      @BenchmarkMode(Mode.SampleTime)
68      @OutputTimeUnit(TimeUnit.NANOSECONDS)
69      public void baseline() {
70      }
71  
72      @GenerateMicroBenchmark
73      @BenchmarkMode(Mode.SampleTime)
74      @OutputTimeUnit(TimeUnit.NANOSECONDS)
75      public long systemCurrentTimeMillis() {
76          return System.currentTimeMillis();
77      }
78  
79      @GenerateMicroBenchmark
80      @BenchmarkMode(Mode.SampleTime)
81      @OutputTimeUnit(TimeUnit.NANOSECONDS)
82      public long systemClock() {
83          return systemClock.currentTimeMillis();
84      }
85  
86      @GenerateMicroBenchmark
87      @BenchmarkMode(Mode.SampleTime)
88      @OutputTimeUnit(TimeUnit.NANOSECONDS)
89      public long cachedClock() {
90          return cachedClock.currentTimeMillis();
91      }
92  
93      @GenerateMicroBenchmark
94      @BenchmarkMode(Mode.SampleTime)
95      @OutputTimeUnit(TimeUnit.NANOSECONDS)
96      public long coarseCachedClock() {
97          return coarseCachedClock.currentTimeMillis();
98      }
99  
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 }