-
Notifications
You must be signed in to change notification settings - Fork 50
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 #12 from patrickfav/feat-11-jmh-benchmark
Add JMH Benchmark
- Loading branch information
Showing
6 changed files
with
202 additions
and
3 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
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
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
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,96 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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> | ||
|
||
<parent> | ||
<artifactId>bcrypt-parent</artifactId> | ||
<groupId>at.favre.lib</groupId> | ||
<version>0.5.0</version> | ||
<relativePath>../../</relativePath> | ||
</parent> | ||
|
||
<artifactId>benchmark-jmh</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<name>BCrypt JMH Benchmark</name> | ||
|
||
<properties> | ||
<maven.deploy.skip>true</maven.deploy.skip> | ||
<jmh.version>1.21</jmh.version> | ||
</properties> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-checkstyle-plugin</artifactId> | ||
<configuration> | ||
<skip>true</skip> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.jacoco</groupId> | ||
<artifactId>jacoco-maven-plugin</artifactId> | ||
<configuration> | ||
<skip>true</skip> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>com.github.chrisdchristo</groupId> | ||
<artifactId>capsule-maven-plugin</artifactId> | ||
<version>1.5.1</version> | ||
<configuration> | ||
<appClass>org.openjdk.jmh.Main</appClass> | ||
<fileDesc>-full</fileDesc> | ||
<type>fat</type> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<id>build-fat-jar</id> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>build</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<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>at.favre.lib</groupId> | ||
<artifactId>bcrypt</artifactId> | ||
<version>0.5.0</version> | ||
<classifier>optimized</classifier> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mindrot</groupId> | ||
<artifactId>jbcrypt</artifactId> | ||
<version>${project.jbcryptVersion}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.bouncycastle</groupId> | ||
<artifactId>bcprov-jdk15on</artifactId> | ||
<version>${project.bcVersion}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.github.fzakaria</groupId> | ||
<artifactId>ascii85</artifactId> | ||
<version>1.1</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
93 changes: 93 additions & 0 deletions
93
...les/benchmark-jmh/src/main/java/at/favre/lib/crypto/bcrypt/benchmark/BcryptBenchmark.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,93 @@ | ||
package at.favre.lib.crypto.bcrypt.benchmark; | ||
|
||
import at.favre.lib.bytes.BinaryToTextEncoding; | ||
import at.favre.lib.bytes.Bytes; | ||
import at.favre.lib.crypto.bcrypt.BCrypt; | ||
import com.github.fzakaria.ascii85.Ascii85; | ||
import org.openjdk.jmh.annotations.*; | ||
|
||
import java.nio.ByteOrder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@SuppressWarnings("CheckStyle") | ||
@State(Scope.Thread) | ||
@Fork(1) | ||
@Warmup(iterations = 2, time = 4) | ||
@Measurement(iterations = 3, time = 12) | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
//CHECKSTYLE.OFF | ||
public class BcryptBenchmark { | ||
|
||
private AbstractBcrypt favreBcrypt; | ||
private AbstractBcrypt jBcrypt; | ||
private AbstractBcrypt bcBcrypt; | ||
private BinaryToTextEncoding.Encoder ascii85 = new BinaryToTextEncoding.Encoder() { | ||
@Override | ||
public String encode(byte[] bytes, ByteOrder byteOrder) { | ||
return Ascii85.encode(bytes); | ||
} | ||
}; | ||
|
||
@Param({"10", "12"}) | ||
private int cost; | ||
private byte[] pw; | ||
|
||
@Setup(Level.Trial) | ||
public void setupBenchmark() { | ||
favreBcrypt = new FavreBcrypt(); | ||
jBcrypt = new JBcrypt(); | ||
bcBcrypt = new BC(); | ||
} | ||
|
||
@Setup(Level.Invocation) | ||
public void setup() { | ||
pw = Bytes.random(36).encode(ascii85).getBytes(StandardCharsets.UTF_8); | ||
} | ||
|
||
@Benchmark | ||
public byte[] benchmarkFavreBcrypt() { | ||
return benchmark(favreBcrypt, cost, pw); | ||
} | ||
|
||
//@Benchmark | ||
public byte[] benchmarkJBcrypt() { | ||
return benchmark(jBcrypt, cost, pw); | ||
} | ||
|
||
//@Benchmark | ||
public byte[] benchmarkBcBcrypt() { | ||
return benchmark(bcBcrypt, cost, pw); | ||
} | ||
|
||
private byte[] benchmark(AbstractBcrypt bcrypt, int logRounds, byte[] pw) { | ||
return bcrypt.bcrypt(logRounds, pw); | ||
} | ||
|
||
static final class FavreBcrypt implements AbstractBcrypt { | ||
@Override | ||
public byte[] bcrypt(int cost, byte[] password) { | ||
return BCrypt.withDefaults().hash(cost, password); | ||
} | ||
} | ||
|
||
static final class JBcrypt implements AbstractBcrypt { | ||
@Override | ||
public byte[] bcrypt(int cost, byte[] password) { | ||
return org.mindrot.jbcrypt.BCrypt.hashpw(new String(password, StandardCharsets.UTF_8), org.mindrot.jbcrypt.BCrypt.gensalt(cost)).getBytes(StandardCharsets.UTF_8); | ||
} | ||
} | ||
|
||
static final class BC implements AbstractBcrypt { | ||
@Override | ||
public byte[] bcrypt(int cost, byte[] password) { | ||
return org.bouncycastle.crypto.generators.BCrypt.generate(Bytes.from(password).append((byte) 0).array(), Bytes.random(16).array(), cost); | ||
} | ||
} | ||
|
||
interface AbstractBcrypt { | ||
byte[] bcrypt(int cost, byte[] password); | ||
} | ||
} | ||
//CHECKSTYLE.ON |
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