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

Quick start with Eclipse Maven Project

Prerequisites

Must download and install:

Create Maven project and benchmark your code

Open Eclipse and switch it to new empty workspace.

Create a new Maven project (without archetype selection)

  • Select menu File -> New -> Other
  • Expand Maven -> select Maven project
  • Select checkbox "Create a simple project (skip archetype selection)" in the dialog window and click button "Next".
  • Enter project attributes:
    • Group id: com.benchmark.core
    • Artifact id: demo-benchmark
    • Name: Maven Benchmark Demo
    • Other fields leave default
    • Click button "Finish" .
  • The Eclipse will generate all required Maven JAVA project artifacts (folders, packages, files).
  • Using project explorer navigate to newly created project and open file pom.xml
  • Using tabs (at the bottom of the main window) navigate to section "pom.xml". This will open raw XML content.
  • 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>

and save the file.

  • Right click mouse button on the project "demo-benchmark", in the context menu select "Maven -> Update Project".
  • Verify that project "demo-benchmark" is checked in the dialog window and click button "OK".
  • This will apply JAVA compiler settings which we added to the "pom.xml" file.

Add CyBench Nature

  • Right click mouse on the project "demo-benchmark" in the "Project Explorer" and select "Add CyBench Nature" in the context menu.
  • This action will update:
    • project dependencies (see "pom.xml" file, inside tags <dependencies>")
    • annotation processors
    • creates sub folder for pre compiled code in "target" directory.

Create Java class which contains any implementation

  • Right click mouse on the project "demo-benchmark" in the "Project Explorer" and select "New" -> "Class"
  • Enter package name: com.benchmarks.demo
  • 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

  • Right mouse click on "StringUtils.java" file in the "Project Explorer" and select "CyBench Generate" in the context menu.
  • Select method "concatStrings" in the dialog and click "OK"
  • Benchmark stub class (StringUtilsBenchmarks) 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 "Benchmarks";
    • all CyBench generated benchmark stub methods has prefix "Benchmark";
    • benchmark stub class and methods also contains possible annotations for benchmark settings and metadata.

Write benchmark for your implementation

  • Open class StringUtilsBenchmarks 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 button on the benchmark implementation class file "StringUtilsBenchmarks.java" in the "Project Explorer"
  • Select "Run As" -> "Run on Cybench" in the context menu
  • The benchmark will start, see Console view
  • Once benchmark execution will finish then report will be generated and displayed in "CyBench Report Viewer" window at the bottom of the window.
  • 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 Eclipse menu -> Window -> Show View -> Other -> CyBench tools -> CyBench Explorer
  • CyBench report is stored in JSON format in folder "reports" which is located under root folder of 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 "StringUtils.java" located in the "src/main/test"):

package com.benchmarks.demo;

import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;

@State(Scope.Benchmark)
public class StringUtilsBenchmarks {


    @Setup(Level.Trial)
    public void setUp() {
        
    }

    @Setup(Level.Iteration)
    public void setUpIteration() {
       
    }

    @Benchmark
    @BenchmarkMode(Mode.Throughput)
    @OutputTimeUnit(TimeUnit.SECONDS)
    @Fork(1)
    @Threads(1)
    @Measurement(iterations = 2, time = 5, timeUnit = TimeUnit.SECONDS)
    @Warmup(iterations = 1, time = 5, timeUnit = TimeUnit.SECONDS)
    public void concatStringsBenchmark(Blackhole bh) {
    	String s = StringUtils.concatStrings("Demo", "Benchmark") ;
        bh.consume(s);
    }

    @TearDown(Level.Trial)
    public void cleanUp() {
       
    }

    @TearDown(Level.Iteration)
    public void cleanUpIteration() {
        
    }

}

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

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.benchmark.core</groupId>
  <artifactId>demo-benchmark</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Maven Benchmark Demo</name>
  <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>
        <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** ⚠️