View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j;
33  
34  import java.io.IOException;
35  import org.junit.Test;
36  import org.openjdk.jmh.results.format.ResultFormatType;
37  import org.openjdk.jmh.runner.Runner;
38  import org.openjdk.jmh.runner.RunnerException;
39  import org.openjdk.jmh.runner.options.Options;
40  import org.openjdk.jmh.runner.options.OptionsBuilder;
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  import org.spf4j.stackmonitor.JmhFlightRecorderProfiler;
44  import org.spf4j.stackmonitor.Spf4jJmhProfiler;
45  import org.spf4j.test.log.TestUtils;
46  
47  /**
48   * @author zoly
49   */
50  public final class JmhTest {
51  
52    private static final Logger LOG = LoggerFactory.getLogger(JmhTest.class);
53  
54    @Test
55    public void runJmh() throws RunnerException, IOException {
56      if (TestUtils.isExecutedInCI()
57              || "true".equalsIgnoreCase(System.getProperty("is.release", "false"))
58              || !System.getProperty("project.version", "").contains("SNAPSHOT")) {
59        LOG.info("Benchmarks disabled in travis, not enough resources for this...");
60        return;
61      }
62      final String destinationFolder = System.getProperty("basedir",
63              org.spf4j.base.Runtime.USER_DIR) + "/target";
64      final String profile = System.getProperty("basedir",
65              org.spf4j.base.Runtime.USER_DIR) + "/src/main/jfc/profile.jfc";
66      Options opt = new OptionsBuilder()
67              //               .include(".*StringsBenchmark")
68              //                .include(".*Reflections.*")
69              //                .addProfiler(JmhProfiler.class)
70              //                .addProfiler(CompilerProfiler.class)
71              .addProfiler(JmhFlightRecorderProfiler.class)
72              .addProfiler(Spf4jJmhProfiler.class)
73              //                .addProfiler(GCProfiler.class)
74              //"-XX:+PrintCompilation", "-XX:+UseG1GC", "-XX:MinTLABSize=1m", "-XX:MaxInlineLevel=12"
75              // "-XX:+PrintInlining", "-XX:+TraceDeoptimization", "-XX:+DebugDeoptimization", "-XX:+LogEvents"
76              //"-XX:+UnlockDiagnosticVMOptions", "-XX:+LogEvents", "-XX:+PrintCodeCache", "-XX:MaxInlineLevel=12",
77              //         "-XX:+PrintInlining", "-XX:+PrintCompilation", "-XX:+LogCompilation"
78              .jvmArgs("-XX:MaxInlineLevel=12", "-XX:-RestrictContended",
79                      "-XX:BiasedLockingStartupDelay=0",
80                      "-Xmx256m", "-Xms256m", "-XX:+UnlockCommercialFeatures",
81                      "-Djmh.stack.profiles=" + destinationFolder,
82  //                    "-Dspf4j.timeSource=systemTime",
83                      "-Dspf4j.executors.defaultExecutor.daemon=true",
84                    "-Djmh.executor=FJP",
85                      "-Djmh.fr.options=defaultrecording=true,settings=" + profile)
86              .result(destinationFolder + "/" + "benchmarkResults.csv")
87              .resultFormat(ResultFormatType.CSV)
88              .warmupIterations(3)
89              .measurementIterations(5)
90              .forks(1)
91              .build();
92      new Runner(opt).run();
93    }
94  
95  }