Getting started with Intellij Maven project - Nastel/gocypher-cybench-java GitHub Wiki

Quick start with Intellij Maven Project

Video Tutorial

This tutorial also has a video version, which can be found here.

Prerequisites

Must download and install:

Create Maven project and benchmark your code

  • Open Intellij and select menu "File"->"New"->"Project".
  • Select "Maven", check that "Project SDK" points to JAVA 1.8 or above and click button "Next".
  • Enter project attributes:
    • name - demo-benchmark
    • location - c:\development\intellij-tutorial-ws\
    • Expand "Artifact coordinates" and enter "GroupId" - com.benchmark.core, "ArtifactId" shall remain the same as field "Name".
  • Click button "Finish".
  • Select "Open in new window" when Intellij will ask where to open project.
  • The Intellij will generate all required Maven JAVA project artifacts (folders, packages, files).
  • Using project explorer navigate to newly created project and open file pom.xml.
  • Configure project to use JAVA 1.8 (or above) by adding these lines just above project close tag (</project>):
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <compilerVersion>1.8</compilerVersion>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Create Java class which contains any implementation

  • Right click mouse on the project "demo-benchmark" java source folder ("src/main/java")and select "New" -> "Package".
  • Enter package name: com.benchmarks.demo.
  • Right click mouse on the project "demo-benchmark" newly created package and select "New" -> "Java Class".
  • Enter class name: StringUtils.
  • Create a public method which concatenates two strings.
	public static String concatStrings (String s1, String s2){
		return s1.concat(s2);
	}

Generate benchmark stub class for your implementation

  • Open class StringUtils.

  • Right mouse click on the class name in the code and select _"Generate"->"CyBench benchmark" in the context menu.

  • Diloag window opnes which contains "CyBench" benchmarks stub generation options, leave defaults and click button "OK".

  • Destination directory selection dialog window opens, selec option ...\src\test\java\com\benchmarks\demo and click button "OK".

  • If error message box appears "Libraries not found. Add?", choose "OK".

  • Benchmark stub class (StringUtilsBenchmark) for implementation class StringUtils will be generated in the default Maven source folder for tests "src/test/java" and the same package as code class:

    • all CyBench generated benchmark stub classes has prefix "Benchmark";
    • all CyBench generated benchmark stub methods has prefix "Benchmark";
    • benchmark stub class and methods also contains possible annotations for benchmark settings and metadata.

Note - if dependencies to CyBench tools are not added to pom.xml file , then add them manually

<dependencies>
    <dependency>
        <groupId>org.openjdk.jmh</groupId>
        <artifactId>jmh-core</artifactId>
        <version>1.26</version>
    </dependency>
    <dependency>
        <groupId>org.openjdk.jmh</groupId>
        <artifactId>jmh-generator-annprocess</artifactId>
        <version>1.26</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

Note - if generated class StringUtilsBenchmark does not contain any implementation then repeat benchmark stub generation process once gain.

Write benchmark for your implementation

  • Open class StringUtilsBenchmark located in "src/test/java" folder.
  • Update method concatStringsBenchmark implementation by adding rows which calls source code.
    String s = StringUtils.concatStrings("Demo", "Benchmark") ;
    bh.consume(s);

Launch the benchmark and measure String concatenation performance

  • Right click mouse ion the class name (or click on the gutter button which is displayed on the left side of the class declaration and marked with CyBench icon).
  • Select Run 'StringUtilsBenchmark' (for the quick launch).
  • The benchmark will start, see run window named "StringUtilsBenchmark" for messages at the bottom of the screen.
  • Once benchmark execution will finish then report will be generated and displayed in "CyBench Report" viewer window at the bottom of the screen (marked with "CyBench icon").
  • Score usually represents number of operations per second so in our case number for strings concatenations per second.
  • All workspace reports can be explored using "CyBench Explorer" view which can be opened via right side navigation bar by clicking on "CyBench Explorer" item (this opens "CyBench Explorer" widget).
  • CyBench report is stored in JSON format in folder "reports" which is located under root folder of the project.

** Note - if "CyBench Explorer" widget does not show reports ,use open button (located in the toolbar of the widget) and select reports folder in the project.

Here is what the whole thing will look like

Implementation class (file "StringUtils.java" located in the "src/main/java"):

package com.benchmarks.demo;

public class StringUtils {
	public static String concatStrings (String s1, String s2){
		return s1.concat(s2) ;
	}
}

Benchmark class (file "StringUtilsBenchmark.java" located in the "src/main/test"):

package com.benchmarks.demo;

import com.gocypher.cybench.core.annotation.BenchmarkTag;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;

import java.util.concurrent.TimeUnit;

@State(Scope.Benchmark)
public class StringUtilsBenchmark {
    @Setup
    public void setup() {
    }

    @TearDown
    public void teardown() {
    }

    @BenchmarkTag(tag = "20ad88bf-2d58-4a65-ae3e-1c8bae006439")
    @OutputTimeUnit(TimeUnit.SECONDS)
    @BenchmarkMode(Mode.Throughput)
    @Benchmark
    public void concatStringsBenchmark(Blackhole bh) {
        String s = StringUtils.concatStrings("Demo", "Benchmark") ;
        bh.consume(s);
    }
}

Maven Project object model (file "pom.xml" locate in the project root folder):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.benchmark.core</groupId>
    <artifactId>demo-benchmark</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-core</artifactId>
            <version>1.26</version>
        </dependency>
        <dependency>
            <groupId>org.openjdk.jmh</groupId>
            <artifactId>jmh-generator-annprocess</artifactId>
            <version>1.26</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <compilerVersion>1.8</compilerVersion>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
⚠️ **GitHub.com Fallback** ⚠️