Skip to content

Commit bb9943f

Browse files
committed
Add and build benchmarks.
1 parent ee18ade commit bb9943f

File tree

3 files changed

+145
-1
lines changed

3 files changed

+145
-1
lines changed

pom.xml

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,101 @@
6161
<version>5.4.2</version>
6262
<scope>test</scope>
6363
</dependency>
64+
65+
<dependency>
66+
<groupId>org.openjdk.jmh</groupId>
67+
<artifactId>jmh-core</artifactId>
68+
<version>1.21</version>
69+
<scope>test</scope>
70+
</dependency>
71+
72+
<dependency>
73+
<groupId>org.openjdk.jmh</groupId>
74+
<artifactId>jmh-generator-annprocess</artifactId>
75+
<version>1.21</version>
76+
<scope>test</scope>
77+
</dependency>
6478
</dependencies>
6579

6680
<build>
6781
<plugins>
82+
<!-- Add benchmarks as a source directory -->
83+
<plugin>
84+
<groupId>org.codehaus.mojo</groupId>
85+
<artifactId>build-helper-maven-plugin</artifactId>
86+
<version>3.0.0</version>
87+
<executions>
88+
<execution>
89+
<id>add-test-source</id>
90+
<phase>generate-test-sources</phase>
91+
<goals>
92+
<goal>add-test-source</goal>
93+
</goals>
94+
<configuration>
95+
<sources>
96+
<source>src/benchmark/java</source>
97+
</sources>
98+
</configuration>
99+
</execution>
100+
</executions>
101+
</plugin>
102+
68103
<plugin>
69104
<groupId>org.apache.maven.plugins</groupId>
70105
<artifactId>maven-compiler-plugin</artifactId>
71-
<version>3.1</version>
106+
<version>3.8.1</version>
72107
<configuration>
73108
<source>1.8</source>
74109
<target>1.8</target>
75110
</configuration>
111+
112+
<executions>
113+
<execution>
114+
<goals>
115+
<goal>testCompile</goal>
116+
</goals>
117+
118+
<configuration>
119+
<annotationProcessorPaths>
120+
<path>
121+
<groupId>org.openjdk.jmh</groupId>
122+
<artifactId>jmh-generator-annprocess</artifactId>
123+
<version>1.21</version>
124+
</path>
125+
</annotationProcessorPaths>
126+
</configuration>
127+
</execution>
128+
</executions>
129+
</plugin>
130+
131+
<plugin>
132+
<groupId>org.apache.maven.plugins</groupId>
133+
<artifactId>maven-assembly-plugin</artifactId>
134+
<version>3.1.1</version>
135+
136+
<configuration>
137+
<descriptors>
138+
<descriptor>src/assembly/benchmark.xml</descriptor>
139+
</descriptors>
140+
</configuration>
141+
142+
<executions>
143+
<execution>
144+
<id>make-assembly</id>
145+
<phase>package</phase>
146+
<goals>
147+
<goal>single</goal>
148+
</goals>
149+
<configuration>
150+
<attach>true</attach>
151+
<archive>
152+
<manifest>
153+
<mainClass>org.openjdk.jmh.Main</mainClass>
154+
</manifest>
155+
</archive>
156+
</configuration>
157+
</execution>
158+
</executions>
76159
</plugin>
77160

78161
<plugin>

src/assembly/benchmark.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
3+
<id>benchmark</id>
4+
<formats>
5+
<format>jar</format>
6+
</formats>
7+
<includeBaseDirectory>false</includeBaseDirectory>
8+
<dependencySets>
9+
<dependencySet>
10+
<outputDirectory>/</outputDirectory>
11+
<useProjectArtifact>true</useProjectArtifact>
12+
<unpack>true</unpack>
13+
<scope>test</scope>
14+
</dependencySet>
15+
</dependencySets>
16+
<fileSets>
17+
<fileSet>
18+
<directory>${project.build.directory}/test-classes</directory>
19+
<outputDirectory>/</outputDirectory>
20+
<includes>
21+
<include>**/*</include>
22+
</includes>
23+
<useDefaultExcludes>true</useDefaultExcludes>
24+
</fileSet>
25+
</fileSets>
26+
</assembly>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.eatthepath.otp;
2+
3+
import org.openjdk.jmh.annotations.Benchmark;
4+
import org.openjdk.jmh.annotations.Scope;
5+
import org.openjdk.jmh.annotations.Setup;
6+
import org.openjdk.jmh.annotations.State;
7+
8+
import javax.crypto.KeyGenerator;
9+
import java.security.InvalidKeyException;
10+
import java.security.Key;
11+
import java.security.NoSuchAlgorithmException;
12+
13+
@State(Scope.Thread)
14+
public class HmacOneTimePasswordGeneratorBenchmark {
15+
16+
private HmacOneTimePasswordGenerator hotp;
17+
private Key key;
18+
19+
private int counter = 0;
20+
21+
@Setup
22+
public void setUp() throws NoSuchAlgorithmException {
23+
this.hotp = new HmacOneTimePasswordGenerator();
24+
25+
final KeyGenerator keyGenerator = KeyGenerator.getInstance(this.hotp.getAlgorithm());
26+
keyGenerator.init(512);
27+
28+
this.key = keyGenerator.generateKey();
29+
}
30+
31+
@Benchmark
32+
public int benchmarkGenerateOneTimePassword() throws InvalidKeyException {
33+
return this.hotp.generateOneTimePassword(this.key, this.counter++);
34+
}
35+
}

0 commit comments

Comments
 (0)