You are on page 1of 15

Principles of

Performance Oriented Coding


MLE5109
2022-2023
Lab 2
Today - Covered topics

• Application and process performance


• Collections and third party collection libraries (2 courses)
• Functional programming (lambdas) in imperative languages
• String processing and regular expressions in practice
• Java I/O
• Concurrency (in small and large systems) (3 courses)
• Design patterns pitfalls (2 courses)
Today’s task - In memory repository
Today’s task - In memory repository
• Define multiple implementations for the InMemoryRepository, create
benchmark tests for each operation and analyse the results.
– Implementations for
• HashSet (JDK)
• ArrayList (JDK)
• TreeSet (JDK)
• Homework
– ConcurrentHashMap (JDK)
– Eclipse (gs) collections
– Fastutil/Koloboke/Trove4j (one of them, at your choice)
• Fastutil (bonus points)
• (or) Koloboke (bonus points)
• (or) Trove4j (bonus points)
– Benchmarks for operations:
• Add
• Remove
• Contains
JMH - Java Microbenchmark Harness

• Tool used to write benchmark tests for JVM


– Handles optimizations done at JVM/hardware level for code that
runs in isolation (not in real production env)
• Running a benchmark test implies several steps, from warmup to
actual iterations

public class MyBenchmark {


@Benchmark
public void testMethod() {
// benchmark code
}
}
JMH - sample Maven setup

https://gitlab.com/ubb-mle5109/2022/lab-samples/-/blob/main/lab2/pom.xml
JMH tests using Maven
mvn archetype:generate \
-DinteractiveMode=false \
-DarchetypeGroupId=org.openjdk.jmh \
-DarchetypeArtifactId=jmh-java-benchmark-archetype \
-DgroupId=org.sample \
-DartifactId=test \
-Dversion=1.0

mvn clean install

java -jar target/benchmarks.jar


JMH available profilers
• Profilers: java -jar target/benchmarks.jar -prof gc
java -jar target/benchmarks.jar -lprof

Supported profilers:
cl: Classloader profiling via standard MBeans
comp: JIT compiler profiling via standard
MBeans
gc: GC profiling via standard MBeans
jfr: Java Flight Recorder profiler
pauses: Pauses profiler
perfc2c: Linux perf c2c profiler
safepoints: Safepoints profiler
stack: Simple and naive Java stack profiler
JMH - useful links
• JMH tutorial
• OpenJDK-JMH
• Java Performance - JMH
• Samples
• Command Line Options
• JMH archetype
Favor composition over inheritance

● EJ3rd Item 18 - Favor composition over inheritance


○ Inheritance acceptable within a package only if a real subtype
relation exists
○ Inheritance breaks encapsulation
■ We’ll inherit any/all the existing flaws as well
■ Implementation details can be exposed
○ Inheritance creates a tight bond to the base class
■ Difficult to change if base class changes

○ From JDK
■ Stack is not a Vector
■ Properties is not a Hashtable
Favor composition over inheritance
● Instrumented set
# JMH version: 1.35
# VM version: JDK 11.0.8, OpenJDK 64-Bit Server VM, 11.0.8+10
# Warmup: 5 iterations, 10 s each
# Measurement: 5 iterations, 10 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time

Benchmark Mode Cnt Score Error Units


Benchmarks.add_Composition thrpt 5 34343994.604 ± 4461429.681 ops/s
Benchmarks.add_Inheritance thrpt 5 33233038.071 ± 2687183.038 ops/s

○ Lab samples > Lab2


■ Sample code and benchmarks
Libs - Eclipse collections - pocket guide
• Guide: Reference guide
• Source code: GitHub
• Maven dependencies:

<!-- Eclipse collections -->


<dependency>
<groupId>org.eclipse.collections</
groupId>
<artifactId>eclipse-collections-api</
artifactId>
<version>11.1.0</version>
</dependency>

<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections</artifactId>
<version>11.1.0</version>
</dependency>

<dependency>
<groupId>org.eclipse.collections</
groupId>
<artifactId>eclipse-collections-testutils</
artifactId>
<version>11.1.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.eclipse.collections</
groupId>
<artifactId>eclipse-collections-forkjoin</
artifactId>
<version>11.1.0</version>
</dependency>

12
Libs - Koloboke - pocket guide
• Guide: Reference guide
• Source code: GitHub
• Maven dependencies:

<!-- Koloboke -->


<dependency>
<groupId>com.koloboke</groupId>
<artifactId>koloboke-api-jdk8</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.koloboke</groupId>
<artifactId>koloboke-impl-jdk8</artifactId>
<version>1.0.0</version>
<scope>runtime</scope>
</dependency>

13
Libs - trove4j - pocket guide
• Guide: Reference guide
• Source code: Bitbucket
• Maven dependencies:

<!-- TROVE 4j -->


<dependency>
<groupId>net.sf.trove4j</groupId>
<artifactId>trove4j</artifactId>
<version>3.0.3</version>
</dependency>

14
Libs - Fastutil - pocket guide
• Guide: Reference guide
• Source code: GitHub
• Maven dependencies:

<!-- Fast Util -->


<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>
<version>8.5.9</version>
</dependency>

15

You might also like