From cd849c4e2171c5c2b2142c66571a73f1c444d23b Mon Sep 17 00:00:00 2001 From: Fabio Lima Date: Sun, 25 Jul 2021 17:55:29 -0300 Subject: [PATCH] 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. --- CHANGELOG.md | 4 + README.md | 34 ++- benchmark/README.md | 4 + benchmark/pom.xml | 205 ++++++++++++++++++ benchmark/run.sh | 28 +++ .../src/main/java/benchmark/Throughput.java | 59 +++++ 6 files changed, 316 insertions(+), 18 deletions(-) create mode 100644 benchmark/README.md create mode 100644 benchmark/pom.xml create mode 100755 benchmark/run.sh create mode 100644 benchmark/src/main/java/benchmark/Throughput.java diff --git a/CHANGELOG.md b/CHANGELOG.md index abb1f03..314dd62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index a989177..ded0587 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/benchmark/README.md b/benchmark/README.md new file mode 100644 index 0000000..db2664c --- /dev/null +++ b/benchmark/README.md @@ -0,0 +1,4 @@ + +To execute the benchmark, run the script `./benchmark/run.sh`. + +Sorry, there is no `run.bat` file for Windows. diff --git a/benchmark/pom.xml b/benchmark/pom.xml new file mode 100644 index 0000000..7284dc7 --- /dev/null +++ b/benchmark/pom.xml @@ -0,0 +1,205 @@ + + + + 4.0.0 + + com.github.f4b6a3 + benchmark + 0.0.1-SNAPSHOT + jar + + JMH benchmark sample: Java + + + + + + com.github.f4b6a3 + ulid-creator + 0.0.1-BENCHMARK + ${project.basedir}/../target/${dependency.artifactid}-${dependency.version}.jar + + + 1.32 + + + 1.8 + + + benchmarks + + UTF-8 + + + + + org.openjdk.jmh + jmh-core + ${jmh.version} + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh.version} + provided + + + ${dependency.groupid} + ${dependency.artifactid} + ${dependency.version} + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.0 + + ${javac.target} + ${javac.target} + ${javac.target} + + + + org.apache.maven.plugins + maven-install-plugin + 3.0.0-M1 + + ${dependency.groupid} + ${dependency.artifactid} + ${dependency.version} + jar + ${dependency.path} + true + + + + install-jar-lib + + install-file + + validate + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.1 + + + package + + shade + + + ${uberjar.name} + + + org.openjdk.jmh.Main + + + + + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + + + + + + maven-clean-plugin + 2.5 + + + maven-deploy-plugin + 2.8.1 + + + maven-install-plugin + 2.5.1 + + + maven-jar-plugin + 2.4 + + + maven-javadoc-plugin + 2.9.1 + + + maven-resources-plugin + 2.6 + + + maven-site-plugin + 3.3 + + + maven-source-plugin + 2.2.1 + + + maven-surefire-plugin + 2.17 + + + + + + diff --git a/benchmark/run.sh b/benchmark/run.sh new file mode 100755 index 0000000..011cab1 --- /dev/null +++ b/benchmark/run.sh @@ -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 + + diff --git a/benchmark/src/main/java/benchmark/Throughput.java b/benchmark/src/main/java/benchmark/Throughput.java new file mode 100644 index 0000000..5c782f7 --- /dev/null +++ b/benchmark/src/main/java/benchmark/Throughput.java @@ -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(); + } +}