Skip to content

Commit 133bbe1

Browse files
ramansahasimaibin
authored andcommitted
Introduced jmh benchmarking (eugenp#2549)
1 parent 839a68f commit 133bbe1

6 files changed

Lines changed: 117 additions & 159 deletions

File tree

‎core-java/pom.xml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,16 @@
201201
<artifactId>vavr</artifactId>
202202
<version>${vavr.version}</version>
203203
</dependency>
204+
<dependency>
205+
<groupId>org.openjdk.jmh</groupId>
206+
<artifactId>jmh-core</artifactId>
207+
<version>1.19</version>
208+
</dependency>
209+
<dependency>
210+
<groupId>org.openjdk.jmh</groupId>
211+
<artifactId>jmh-generator-annprocess</artifactId>
212+
<version>1.19</version>
213+
</dependency>
204214
</dependencies>
205215

206216
<build>
Lines changed: 55 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,67 @@
11
package com.baeldung.numberofdigits;
22

3-
import static com.baeldung.designpatterns.util.LogerUtil.LOG;;
4-
5-
public class Benchmarking {;
6-
7-
private static final int LOWER_BOUND = 1;
8-
private static final int UPPER_BOUND = 999999999;
9-
10-
11-
public static void main(String[] args) {
12-
LOG.info("Testing all methods...");
13-
14-
long length = test_stringBasedSolution();
15-
LOG.info("String Based Solution : " + length);
16-
17-
length = test_logarithmicApproach();
18-
LOG.info("Logarithmic Approach : " + length);
19-
20-
length = test_repeatedMultiplication();
21-
LOG.info("Repeated Multiplication : " + length);
22-
23-
length = test_shiftOperators();
24-
LOG.info("Shift Operators : " + length);
25-
26-
length = test_dividingWithPowersOf2();
27-
LOG.info("Dividing with Powers of 2 : " + length);
28-
29-
length = test_divideAndConquer();
30-
LOG.info("Divide And Conquer : " + length);
31-
3+
import java.io.IOException;
4+
import java.util.concurrent.TimeUnit;
5+
6+
import org.openjdk.jmh.annotations.Benchmark;
7+
import org.openjdk.jmh.annotations.BenchmarkMode;
8+
import org.openjdk.jmh.annotations.Mode;
9+
import org.openjdk.jmh.annotations.OutputTimeUnit;
10+
import org.openjdk.jmh.annotations.Scope;
11+
import org.openjdk.jmh.annotations.State;
12+
import org.openjdk.jmh.runner.RunnerException;
13+
14+
public class Benchmarking {
15+
public static void main(String[] args) throws RunnerException, IOException {
16+
org.openjdk.jmh.Main.main(args);
3217
}
3318

34-
private static long test_stringBasedSolution() {
35-
36-
long startTime, stopTime, elapsedTime;
37-
startTime = System.currentTimeMillis();
38-
39-
int total = 0;
40-
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
41-
total += NumberOfDigits.stringBasedSolution(i);
42-
}
43-
44-
stopTime = System.currentTimeMillis();
45-
elapsedTime = stopTime - startTime;
46-
47-
return elapsedTime;
19+
@State(Scope.Thread)
20+
public static class ExecutionPlan {
21+
public int number = Integer.MAX_VALUE;
22+
public int length = 0;
23+
public NumberOfDigits numberOfDigits= new NumberOfDigits();
4824
}
49-
50-
private static long test_logarithmicApproach() {
51-
52-
long startTime, stopTime, elapsedTime;
53-
startTime = System.currentTimeMillis();
54-
55-
int total = 0;
56-
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
57-
total += NumberOfDigits.logarithmicApproach(i);
58-
}
59-
60-
stopTime = System.currentTimeMillis();
61-
elapsedTime = stopTime - startTime;
62-
63-
return elapsedTime;
64-
}
65-
66-
private static long test_repeatedMultiplication() {
67-
68-
long startTime, stopTime, elapsedTime;
69-
startTime = System.currentTimeMillis();
70-
71-
int total = 0;
72-
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
73-
total += NumberOfDigits.repeatedMultiplication(i);
74-
}
75-
76-
stopTime = System.currentTimeMillis();
77-
elapsedTime = stopTime - startTime;
78-
79-
return elapsedTime;
25+
26+
@Benchmark
27+
@BenchmarkMode(Mode.AverageTime)
28+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
29+
public void stringBasedSolution(ExecutionPlan plan) {
30+
plan.length = plan.numberOfDigits.stringBasedSolution(plan.number);
8031
}
81-
82-
private static long test_shiftOperators() {
83-
84-
long startTime, stopTime, elapsedTime;
85-
startTime = System.currentTimeMillis();
86-
87-
int total = 0;
88-
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
89-
total += NumberOfDigits.shiftOperators(i);
90-
}
91-
92-
stopTime = System.currentTimeMillis();
93-
elapsedTime = stopTime - startTime;
94-
95-
return elapsedTime;
32+
33+
@Benchmark
34+
@BenchmarkMode(Mode.AverageTime)
35+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
36+
public void logarithmicApproach(ExecutionPlan plan) {
37+
plan.length = plan.numberOfDigits.logarithmicApproach(plan.number);
9638
}
97-
98-
private static long test_dividingWithPowersOf2() {
99-
100-
long startTime, stopTime, elapsedTime;
101-
startTime = System.currentTimeMillis();
102-
103-
int total = 0;
104-
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
105-
total += NumberOfDigits.dividingWithPowersOf2(i);
106-
}
107-
108-
stopTime = System.currentTimeMillis();
109-
elapsedTime = stopTime - startTime;
110-
111-
return elapsedTime;
39+
40+
@Benchmark
41+
@BenchmarkMode(Mode.AverageTime)
42+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
43+
public void repeatedMultiplication(ExecutionPlan plan) {
44+
plan.length = plan.numberOfDigits.repeatedMultiplication(plan.number);
11245
}
113-
114-
private static long test_divideAndConquer() {
115-
116-
long startTime, stopTime, elapsedTime;
117-
startTime = System.currentTimeMillis();
118-
119-
int total = 0;
120-
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++) {
121-
total += NumberOfDigits.divideAndConquer(i);
122-
}
123-
124-
stopTime = System.currentTimeMillis();
125-
elapsedTime = stopTime - startTime;
126-
127-
return elapsedTime;
46+
47+
@Benchmark
48+
@BenchmarkMode(Mode.AverageTime)
49+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
50+
public void shiftOperators(ExecutionPlan plan) {
51+
plan.length = plan.numberOfDigits.shiftOperators(plan.number);
12852
}
12953

54+
@Benchmark
55+
@BenchmarkMode(Mode.AverageTime)
56+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
57+
public void dividingWithPowersOf2(ExecutionPlan plan) {
58+
plan.length = plan.numberOfDigits.dividingWithPowersOf2(plan.number);
59+
}
13060

61+
@Benchmark
62+
@BenchmarkMode(Mode.AverageTime)
63+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
64+
public void divideAndConquer(ExecutionPlan plan) {
65+
plan.length = plan.numberOfDigits.divideAndConquer(plan.number);
66+
}
13167
}

‎core-java/src/main/java/com/baeldung/numberofdigits/NumberOfDigits.java‎

100644100755
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package com.baeldung.numberofdigits;
22

33
public class NumberOfDigits {
4-
public static int stringBasedSolution(int number) {
4+
public int stringBasedSolution(int number) {
55
int length = String.valueOf(number).length();
66
return length;
77
}
88

9-
public static int logarithmicApproach(int number) {
9+
public int logarithmicApproach(int number) {
1010
int length = (int) Math.log10(number) + 1;
1111
return length;
1212
}
1313

14-
public static int repeatedMultiplication(int number) {
14+
public int repeatedMultiplication(int number) {
1515
int length = 0;
1616
long temp = 1;
1717
while(temp <= number) {
@@ -21,7 +21,7 @@ public static int repeatedMultiplication(int number) {
2121
return length;
2222
}
2323

24-
public static int shiftOperators(int number) {
24+
public int shiftOperators(int number) {
2525
int length = 0;
2626
long temp = 1;
2727
while(temp <= number) {
@@ -31,7 +31,7 @@ public static int shiftOperators(int number) {
3131
return length;
3232
}
3333

34-
public static int dividingWithPowersOf2(int number) {
34+
public int dividingWithPowersOf2(int number) {
3535
int length = 1;
3636
if (number >= 100000000) {
3737
length += 8;
@@ -51,7 +51,7 @@ public static int dividingWithPowersOf2(int number) {
5151
return length;
5252
}
5353

54-
public static int divideAndConquer(int number) {
54+
public int divideAndConquer(int number) {
5555
if (number < 100000){
5656
// 5 digits or less
5757
if (number < 100){
Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
1-
package com.baeldung.numberofdigits;
2-
3-
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
4-
5-
public class NumberOfDigitsDriver {
6-
public static void main(String[] args) {
7-
LOG.info("Testing all methods...");
8-
9-
long length = NumberOfDigits.stringBasedSolution(602);
10-
LOG.info("String Based Solution : " + length);
11-
12-
length = NumberOfDigits.logarithmicApproach(602);
13-
LOG.info("Logarithmic Approach : " + length);
14-
15-
length = NumberOfDigits.repeatedMultiplication(602);
16-
LOG.info("Repeated Multiplication : " + length);
17-
18-
length = NumberOfDigits.shiftOperators(602);
19-
LOG.info("Shift Operators : " + length);
20-
21-
length = NumberOfDigits.dividingWithPowersOf2(602);
22-
LOG.info("Dividing with Powers of 2 : " + length);
23-
24-
length = NumberOfDigits.divideAndConquer(602);
25-
LOG.info("Divide And Conquer : " + length);
26-
}
27-
}
1+
package com.baeldung.numberofdigits;
2+
3+
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
4+
5+
public class NumberOfDigitsDriver {
6+
private static NumberOfDigits numberOfDigits;
7+
8+
static {
9+
numberOfDigits = new NumberOfDigits();
10+
}
11+
12+
public static void main(String[] args) {
13+
LOG.info("Testing all methods...");
14+
15+
long length = numberOfDigits.stringBasedSolution(602);
16+
LOG.info("String Based Solution : " + length);
17+
18+
length = numberOfDigits.logarithmicApproach(602);
19+
LOG.info("Logarithmic Approach : " + length);
20+
21+
length = numberOfDigits.repeatedMultiplication(602);
22+
LOG.info("Repeated Multiplication : " + length);
23+
24+
length = numberOfDigits.shiftOperators(602);
25+
LOG.info("Shift Operators : " + length);
26+
27+
length = numberOfDigits.dividingWithPowersOf2(602);
28+
LOG.info("Dividing with Powers of 2 : " + length);
29+
30+
length = numberOfDigits.divideAndConquer(602);
31+
LOG.info("Divide And Conquer : " + length);
32+
}
33+
}

‎core-java/src/main/resources/META-INF/BenchmarkList‎

Whitespace-only changes.

‎core-java/src/test/java/com/baeldung/numberofdigits/NumberOfDigitsIntegrationTest.java‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99

1010
@RunWith(Theories.class)
1111
public class NumberOfDigitsIntegrationTest {
12-
12+
13+
private static NumberOfDigits numberOfDigits;
14+
15+
static {
16+
numberOfDigits = new NumberOfDigits();
17+
}
18+
1319
@DataPoints
1420
public static int[][] lowestIntegers()
1521
{
@@ -64,37 +70,37 @@ public static int[][] randomIntegers()
6470
@Theory
6571
public void givenDataPoints_whenStringBasedSolutionInvoked_thenAllPointsMatch(final int[] entry) {
6672
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
67-
Assert.assertEquals(entry[0], NumberOfDigits.stringBasedSolution(entry[1]));
73+
Assert.assertEquals(entry[0], numberOfDigits.stringBasedSolution(entry[1]));
6874
}
6975

7076
@Theory
7177
public void givenDataPoints_whenLogarithmicApproachInvoked_thenAllPointsMatch(final int[] entry) {
7278
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
73-
Assert.assertEquals(entry[0], NumberOfDigits.logarithmicApproach(entry[1]));
79+
Assert.assertEquals(entry[0], numberOfDigits.logarithmicApproach(entry[1]));
7480
}
7581

7682
@Theory
7783
public void givenDataPoints_whenRepeatedMultiplicationInvoked_thenAllPointsMatch(final int[] entry) {
7884
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
79-
Assert.assertEquals(entry[0], NumberOfDigits.repeatedMultiplication(entry[1]));
85+
Assert.assertEquals(entry[0], numberOfDigits.repeatedMultiplication(entry[1]));
8086
}
8187

8288
@Theory
8389
public void givenDataPoints_whenShiftOperatorsInvoked_thenAllPointsMatch(final int[] entry) {
8490
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
85-
Assert.assertEquals(entry[0], NumberOfDigits.shiftOperators(entry[1]));
91+
Assert.assertEquals(entry[0], numberOfDigits.shiftOperators(entry[1]));
8692
}
8793

8894
@Theory
8995
public void givenDataPoints_whenDividingWithPowersOf2Invoked_thenAllPointsMatch(final int[] entry) {
9096
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
91-
Assert.assertEquals(entry[0], NumberOfDigits.dividingWithPowersOf2(entry[1]));
97+
Assert.assertEquals(entry[0], numberOfDigits.dividingWithPowersOf2(entry[1]));
9298
}
9399

94100
@Theory
95101
public void givenDataPoints_whenDivideAndConquerInvoked_thenAllPointsMatch(final int[] entry) {
96102
Assume.assumeTrue(entry[0] > 0 && entry[1] > 0);
97-
Assert.assertEquals(entry[0], NumberOfDigits.divideAndConquer(entry[1]));
103+
Assert.assertEquals(entry[0], numberOfDigits.divideAndConquer(entry[1]));
98104
}
99105

100106
}

0 commit comments

Comments
 (0)