Skip to content

Instantly share code, notes, and snippets.

@rmpestano
Last active February 24, 2016 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmpestano/d1e5a264e2d1b5e85ba4 to your computer and use it in GitHub Desktop.
Save rmpestano/d1e5a264e2d1b5e85ba4 to your computer and use it in GitHub Desktop.
JMH example that runs on travis
JMH bench example
Figure 1. travis log print

JMH benchmark example

src/perf/java/com/github/cukedoctor/CukedoctorBenchmark
@State(Scope.Benchmark)
@BenchmarkMode(Mode.Throughput)
public class CukedoctorBenchmark {

    private static CukedoctorConverter cukedoctorConverter;
    private static List<Feature> asciidoctorFeatures;
    private static Asciidoctor asciidoctor;


    @State(Scope.Thread)
    public static class BenchmarkContext {

        @Setup
        public void init() throws RunnerException {
            Logger.getLogger(FileUtil.class.getName()).setLevel(Level.SEVERE);
            String asciidoctorFeaturesPath = FileUtil.findJsonFile(Thread.currentThread().getContextClassLoader().getResource("asciidoctor-features.json").getPath());
            asciidoctorFeatures = FeatureParser.parse(asciidoctorFeaturesPath);
            cukedoctorConverter = Cukedoctor.instance(asciidoctorFeatures);
        }


    }

    @Benchmark
    public void convert(BenchmarkContext ctx) {
        String livingDocumentation = cukedoctorConverter.renderDocumentation();
        File adocFile = FileUtil.saveFile("target/benchmark/" + UUID.randomUUID().toString() + ".adoc", livingDocumentation);
        //Asciidoctor.Factory.create().convertFile(adocFile, org.asciidoctor.OptionsBuilder.options().backend("html").safe(SafeMode.UNSAFE).asMap());
    }


    public static void main(String[] args) throws RunnerException, InterruptedException {
        try {
            new Runner(new OptionsBuilder().
                    forks(3).
                    threads(8).
                    warmupIterations(5).
                    warmupForks(1).
                    measurementIterations(10).
                    include(CukedoctorBenchmark.class.getSimpleName()).
                    measurementTime(TimeValue.milliseconds(350)).
                    build()
            ).run();
        }finally { (1)
            List<String> files = FileUtil.findFiles("target/benchmark", new String[]{"**/*.adoc"});
            Logger.getLogger(CukedoctorBenchmark.class.getName()).info("Number of files converted: " + files.size());
            CukedoctorBenchmark.removeAdocFIles();
        }

    }

    private static void removeAdocFIles() {
        List<String> files = FileUtil.findFiles("target/benchmark", new String[]{"**/*.adoc"});
        if (files != null) {
            for (String file : files) {
                FileUtil.removeFile(file);
            }
        }
    }

}
  1. @TearDown will not help cause its called per iteration

pom.xml
<profile>
    <id>perf</id>
    <build>
        <resources>
            <resource>
                <directory>src/perf/resources</directory>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <compilerVersion>1.7</compilerVersion>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <executable>java</executable>
                    <arguments>
                        <argument>-classpath</argument>
                        <classpath/>
                        <argument>com.github.cukedoctor.CukedoctorBenchmark</argument>
                    </arguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>src/perf/java</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>
travis.yml
sudo: false
language: java
jdk: oraclejdk8
cache:
  directories:
    - '$HOME/.m2/repository'

script:
    - mvn compile exec:exec -Pperf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment