Add benchmark code to compare ULID with UUID #14

The benchmark code was created in the direcory `benchmark`.

To execute the benchmark, run the script `./benchmark/run.sh`.

Sorry, there is no `run.bat` file for Windows.
This commit is contained in:
Fabio Lima 2021-07-25 17:55:29 -03:00
parent 0152fe3084
commit cd849c4e21
6 changed files with 316 additions and 18 deletions

View File

@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
## [Unreleased]
## Added
- Add benchmark code to compare ULID with UUID
## [3.2.0] - 2021-07-17
Simplified the use of `UlidFactory` with other random generators.

View File

@ -219,29 +219,27 @@ Benchmark
This section shows benchmarks comparing `UlidCreator` to `java.util.UUID`.
```
================================================================================
THROUGHPUT (operations/msec) Mode Cnt Score Error Units
================================================================================
Throughput.Uuid01_toString thrpt 5 2876,799 ± 39,938 ops/ms
Throughput.Uuid02_fromString thrpt 5 1936,569 ± 38,822 ops/ms
Throughput.Uuid03_RandomBased thrpt 5 2011,774 ± 21,198 ops/ms
--------------------------------------------------------------------------------
Throughput.UlidCreator01_toString thrpt 5 29487,382 ± 627,808 ops/ms
Throughput.UlidCreator02_fromString thrpt 5 21194,263 ± 706,398 ops/ms
Throughput.UlidCreator03_Ulid thrpt 5 2745,123 ± 41,326 ops/ms
Throughput.UlidCreator04_MonotonicUlid thrpt 5 19542,344 ± 423,271 ops/ms
================================================================================
Total time: 00:09:22
================================================================================
THROUGHPUT (operations/msec) Mode Cnt Score Error Units
--------------------------------------------------------------------------------
UUID_randomUUID thrpt 5 2051,918 ± 24,797 ops/ms
UUID_randomUUID_toString thrpt 5 1176,382 ± 32,709 ops/ms
UlidCreator_getUlid thrpt 5 2738,998 ± 51,092 ops/ms
UlidCreator_getUlid_toString thrpt 5 2548,178 ± 25,484 ops/ms
UlidCreator_getMonotonicUlid thrpt 5 19807,920 ± 410,996 ops/ms
UlidCreator_getMonotonicUlid_toString thrpt 5 13178,389 ± 147,323 ops/ms
--------------------------------------------------------------------------------
Total time: 00:08:01
--------------------------------------------------------------------------------
```
System: JVM 8, Ubuntu 20.04, CPU i5-3330, 8G RAM.
See: [uuid-creator-benchmark](https://github.com/fabiolimace/uuid-creator-benchmark)
To execute the benchmark, run `./benchmark/run.sh`.
Other generators
Other identifier generators
-------------------------------------------
* [UUID Creator](https://github.com/f4b6a3/uuid-creator): for generating UUIDs
* [TSID Creator](https://github.com/f4b6a3/tsid-creator): for generating TSIDs
* [KSUID Creator](https://github.com/f4b6a3/ksuid-creator): for generating KSUIDs
* [UUID Creator](https://github.com/f4b6a3/uuid-creator)
* [TSID Creator](https://github.com/f4b6a3/tsid-creator)
* [KSUID Creator](https://github.com/f4b6a3/ksuid-creator)

4
benchmark/README.md Normal file
View File

@ -0,0 +1,4 @@
To execute the benchmark, run the script `./benchmark/run.sh`.
Sorry, there is no `run.bat` file for Windows.

205
benchmark/pom.xml Normal file
View File

@ -0,0 +1,205 @@
<!--
Copyright (c) 2014, Oracle America, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Oracle nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
-->
<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.github.f4b6a3</groupId>
<artifactId>benchmark</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>JMH benchmark sample: Java</name>
<!--
This is the demo/sample template build script for building Java benchmarks with JMH.
Edit as needed.
-->
<properties>
<dependency.groupid>com.github.f4b6a3</dependency.groupid>
<dependency.artifactid>ulid-creator</dependency.artifactid>
<dependency.version>0.0.1-BENCHMARK</dependency.version>
<dependency.path>${project.basedir}/../target/${dependency.artifactid}-${dependency.version}.jar</dependency.path>
<!--
JMH version to use with this project.
-->
<jmh.version>1.32</jmh.version>
<!--
Java source/target to use for compilation.
-->
<javac.target>1.8</javac.target>
<!--
Name of the benchmark Uber-JAR to generate.
-->
<uberjar.name>benchmarks</uberjar.name>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${dependency.groupid}</groupId>
<artifactId>${dependency.artifactid}</artifactId>
<version>${dependency.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<compilerVersion>${javac.target}</compilerVersion>
<source>${javac.target}</source>
<target>${javac.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>3.0.0-M1</version>
<configuration>
<groupId>${dependency.groupid}</groupId>
<artifactId>${dependency.artifactid}</artifactId>
<version>${dependency.version}</version>
<packaging>jar</packaging>
<file>${dependency.path}</file>
<generatePom>true</generatePom>
</configuration>
<executions>
<execution>
<id>install-jar-lib</id>
<goals>
<goal>install-file</goal>
</goals>
<phase>validate</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${uberjar.name}</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.openjdk.jmh.Main</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
<filters>
<filter>
<!--
Shading signed JARs will fail without this.
http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
-->
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

28
benchmark/run.sh Executable file
View File

@ -0,0 +1,28 @@
#!/bin/bash
ARTIFACT_ID=ulid-creator
# find the script folder
SCRIPT_DIR=`dirname "$0"`
# go to the parent folder
cd ${SCRIPT_DIR}/..
# compile the parent project
mvn clean install
# create a symbolic link to be imported to the maven repository
cp ${PWD}/target/${ARTIFACT_ID}-*-SNAPSHOT.jar ${PWD}/target/${ARTIFACT_ID}-0.0.1-BENCHMARK.jar
# go to the benchmark folder
cd benchmark
# compile the benchmark project
mvn validate
mvn clean install
# run the benchmark
# /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java -jar target/benchmarks.jar
java -jar target/benchmarks.jar

View File

@ -0,0 +1,59 @@
package benchmark;
import java.util.UUID;
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.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
import com.github.f4b6a3.ulid.Ulid;
import com.github.f4b6a3.ulid.UlidCreator;
@Fork(1)
@Threads(1)
@State(Scope.Thread)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class Throughput {
@Benchmark
public UUID UUID_randomUUID() {
return UUID.randomUUID();
}
@Benchmark
public String UUID_randomUUID_toString() {
return UUID.randomUUID().toString();
}
@Benchmark
public Ulid UlidCreator_getUlid() {
return UlidCreator.getUlid();
}
@Benchmark
public String UlidCreator_getUlid_toString() {
return UlidCreator.getUlid().toString();
}
@Benchmark
public Ulid UlidCreator_getMonotonicUlid() {
return UlidCreator.getMonotonicUlid();
}
@Benchmark
public String UlidCreator_getMonotonicUlid_toString() {
return UlidCreator.getMonotonicUlid().toString();
}
}