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

Welcome to the gocypher-cybench-java wiki!

Quick start with Eclipse Java Project

Prerequisites

Must download and install:

Create a Java project and benchmark your code

  • Open Eclipse and switch it to new empty workspace
  • Create an new Java Project and name it "Benchmarks Demo" (double check that it uses JAVA 1.8 or above).
  • Right click mouse on the project name in the "Project Explorer" and select "Add CyBench Nature" in the context menu.

Create a Java class which contains any implementation and generate benchmark stubs for it

  • Right click mouse button on the project 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);
}
  • 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 benchmarks source folder "src-benchmarks" 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.
  • Open class StringUtilsBenchmarks located in "src-benchmarks" folder.
  • Update method concatStringsBenchmark implementation by adding rows which calls our 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 folder "src"):


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 folder "src-benchmarks"):


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() {
        
    }

}