-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from yasuyuki-baba/add_performance_benchmarks…
…_with_jmh_again JMH-based set of benchmarks
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/jmh/java/com/nulabinc/zxcvbn/RandomPasswordMeasureBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.nulabinc.zxcvbn; | ||
|
||
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.Param; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
|
||
import java.io.IOException; | ||
import java.util.Random; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
@State(Scope.Thread) | ||
@Warmup(iterations = 2) | ||
@Measurement(iterations = 3) | ||
@Fork(1) | ||
public class RandomPasswordMeasureBenchmark { | ||
|
||
@Param({"8", "32", "128", "512", "1024"}) | ||
private int passwordLength; | ||
private String password; | ||
Zxcvbn zxcvbn; | ||
|
||
@Setup | ||
public void setup() throws IOException { | ||
zxcvbn = new ZxcvbnBuilder() | ||
.dictionaries(StandardDictionaries.loadAllDictionaries()) | ||
.keyboards(StandardKeyboards.loadAllKeyboards()) | ||
.build(); | ||
|
||
Random random = new Random(42); | ||
StringBuilder sb = new StringBuilder(passwordLength); | ||
for (int i = 0; i < passwordLength; i++) { | ||
char c = (char) (random.nextInt() % Character.MAX_VALUE); | ||
sb.append(c); | ||
} | ||
password = sb.toString(); | ||
} | ||
|
||
@Benchmark | ||
public Strength measure() { | ||
return zxcvbn.measure(password); | ||
} | ||
} |