Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JMH-based set of benchmarks #127

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
id 'signing'
id 'jacoco'
id 'com.github.kt3k.coveralls' version '2.12.0'
id "me.champeau.jmh" version "0.6.6"
}

group = 'com.nulab-inc'
Expand All @@ -23,6 +24,9 @@ repositories {

dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.13.2'
jmh group: 'org.openjdk.jmh', name: 'jmh-core', version: '1.34'
jmh group: 'org.openjdk.jmh', name: 'jmh-generator-annprocess', version: '1.34'
jmh group: 'org.ow2.asm', name: 'asm', version: '9.3'
}

task compileModuleInfoJava(type: JavaCompile, dependsOn: compileJava) {
Expand Down
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);
}
}