Skip to content

Getting started with Intellij Java project

Spencer Spitz edited this page Feb 14, 2022 · 2 revisions

Quick start with Intellij JAVA Project

Prerequisites

Must download and install:

Create JAVA project and benchmark your code

  • Open Intellij and select menu "File"->"New"->"Project".
  • Select "Java", check that "Project SDK" points to JAVA 1.8 or above and click button "Next".
  • Do not select any templates and click button "Next".
  • Enter project attributes:
    • name - demo-benchmark
    • location - c:\development\intellij-tutorial-ws\
    • module name - demo-benchmark
    • content root and module file location shall remain the same as project location
  • Click button "Finish".
  • Select "Open in new window" when Intellij will ask where to open project.
  • Intellij will generate all required JAVA project artifacts (folders, packages, files).

Create Java class which contains any implementation

  • Right click mouse on the project "demo-benchmark" java source folder ("src")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.

  • Dialog window opens which contains "CyBench" benchmarks stub generation options, leave defaults 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 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 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 "com.benchmarks.demo" package.
  • 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.

Here is what the whole thing will look like

Implementation class (file "StringUtils.java" located in the package "com.benchmarks.demo"):

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 package "com.benchmarks.demo"):

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 = "b31b4841-2f46-4352-8e7e-a0df5e7dd659")
    @OutputTimeUnit(TimeUnit.SECONDS)
    @BenchmarkMode(Mode.Throughput)
    @Benchmark
    public void concatStringsBenchmark(Blackhole bh) {
        String s = StringUtils.concatStrings("Demo", "Benchmark") ;
        bh.consume(s);
    }
}