A comprehensive benchmark suite for nanoid-java using JMH (Java Microbenchmark Harness), providing performance measurements for NanoID generation operations.
This project benchmarks the performance of the nanoid-java library, measuring throughput and timing statistics for various NanoID generation scenarios.
- JDK 17 or higher
- Apache Maven 3.6.0 or higher
-
Clone the repository:
git clone https://github.com/albahrani/nanoid-java-benchmark.git cd nanoid-java-benchmark -
Build the project:
mvn clean compile
-
Run all benchmarks:
mvn exec:java
The benchmark suite includes the following test scenarios:
| Benchmark | Description | Method |
|---|---|---|
| Default NanoID | Standard 21-character NanoID with default alphabet | NanoId.nanoid() |
| Custom Length | 10-character NanoID with default alphabet | NanoId.nanoid(10) |
| Custom Alphabet | 21-character NanoID with custom alphabet (abcdef) |
NanoId.customNanoid("abcdef", 21) |
You can run specific benchmarks using JMH's filtering capabilities:
# Run only default NanoID benchmark
mvn exec:java -Dexec.args=".*generateDefaultNanoId.*"
# Run only custom length benchmark
mvn exec:java -Dexec.args=".*generateNanoId10.*"
# Run only custom alphabet benchmark
mvn exec:java -Dexec.args=".*generateCustomNanoId.*"The benchmarks are configured with the following JMH settings:
- Warmup: 2 iterations
- Measurement: 5 iterations
- Fork: 0 (disabled for classpath compatibility)
- Time Unit: Microseconds
- State: Benchmark scope
JMH will output performance metrics including:
- Throughput: Operations per second (ops/s)
- Average Time: Average execution time per operation
- Error: Statistical error margin
- Score: Benchmark score with confidence intervals
Example output:
Benchmark Mode Cnt Score Error Units
NanoIdBenchmark.generateDefaultNanoId thrpt 5 123.456 ± 7.890 ops/us
NanoIdBenchmark.generateNanoId10 thrpt 5 234.567 ± 8.901 ops/us
NanoIdBenchmark.generateCustomNanoId thrpt 5 345.678 ± 9.012 ops/us
You can customize benchmark execution with additional JMH parameters:
# Run with custom iterations
mvn exec:java -Dexec.args="-wi 5 -i 10"
# Generate detailed reports
mvn exec:java -Dexec.args="-rf json -rff benchmark-results.json"
# Run with profilers
mvn exec:java -Dexec.args="-prof gc"gc: Garbage collection profilingstack: Stack profilingperf: Performance counter profiling (Linux only)
nanoid-java-benchmark/
├── src/
│ └── main/
│ └── java/
│ └── com/
│ └── nanoid/
│ └── NanoIdBenchmark.java
├── pom.xml
├── README.md
├── LICENSE
└── .gitignore
- nanoid-java: Core NanoID implementation
- JMH Core: Microbenchmark harness
- JMH Annotation Processor: JMH code generation
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- nanoid-java - Java implementation of NanoID
- nanoid - Original JavaScript implementation
This project is licensed under the MIT License - see the LICENSE file for details.