Skip to content

Commit

Permalink
Merge pull request #1 from xp-vit/add-big-int-performance-test
Browse files Browse the repository at this point in the history
BigDecimal perf tests
  • Loading branch information
xp-vit authored Oct 29, 2024
2 parents e42e441 + d0eb396 commit e8c6add
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 8 deletions.
15 changes: 8 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
plugins {
id 'java'
id 'idea'
id "me.champeau.jmh" version "0.6.8"
id "me.champeau.jmh" version "0.7.2"
id("io.freefair.lombok") version "8.10.2"
}

group = 'com.patotski'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
targetCompatibility = '11'
sourceCompatibility = '21'
targetCompatibility = '21'

repositories {
mavenCentral()
Expand All @@ -17,11 +18,11 @@ dependencies {
jmhImplementation 'org.apache.logging.log4j:log4j-core:2.19.0'
jmh 'org.apache.logging.log4j:log4j-core:2.19.0'

jmh 'org.openjdk.jmh:jmh-core:1.35'
jmh 'org.openjdk.jmh:jmh-generator-annprocess:1.35'
jmh 'org.openjdk.jmh:jmh-core:1.37'
jmh 'org.openjdk.jmh:jmh-generator-annprocess:1.37'

// this is the line that solves the missing /META-INF/BenchmarkList error
jmhAnnotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess:1.35'
jmhAnnotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess:1.37'
}


Expand All @@ -31,4 +32,4 @@ jmh {
fork = 1
benchmarkMode = ['Throughput']
resultFormat = 'JSON'
}
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.patotski.performance.bigdec;

import org.openjdk.jmh.annotations.*;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

import static com.patotski.performance.utils.BenchmarkUtils.runBenchmark;

@BenchmarkMode({Mode.AverageTime})
@Warmup(iterations = 2, time = 3, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 10, timeUnit = TimeUnit.SECONDS)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
public class BigDecConstructor {
public static final BigDecimal BIG_DECIMAL = BigDecimal.valueOf(12.3456);

@Benchmark
public BigDecimal string() {
return new BigDecimal("12.3456");

}

@Benchmark
public BigDecimal doubleCreation() {
return new BigDecimal(12.3456);
}

public static void main(String[] args) throws Exception {
runBenchmark(BigDecConstructor.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.patotski.performance.bigdec;

import org.openjdk.jmh.annotations.*;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

import static com.patotski.performance.utils.BenchmarkUtils.runBenchmark;

@BenchmarkMode({Mode.AverageTime})
@Warmup(iterations = 2, time = 2, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
public class BigDecDivideVsMove {
public static final BigDecimal bigDecimal = BigDecimal.valueOf(12.3456);

@Benchmark
public BigDecimal divide() {
return bigDecimal.divide(BigDecimal.valueOf(100));
}

@Benchmark
public BigDecimal move2Left() {
return bigDecimal.movePointLeft(2);
}

@Benchmark
public BigDecimal scaleByPowerOfTen() {
return bigDecimal.scaleByPowerOfTen(-2);
}

public static void main(String[] args) throws Exception {
runBenchmark(BigDecDivideVsMove.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.patotski.performance.bigdec;

import org.openjdk.jmh.annotations.*;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import static com.patotski.performance.utils.BenchmarkUtils.runBenchmark;

@BenchmarkMode({Mode.AverageTime})
@Warmup(iterations = 2, time = 5, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
public class BigDecMultiplyVsMove {
public static final BigDecimal BIG_DECIMAL = BigDecimal.valueOf(12.3456);

@Benchmark
public BigDecimal multiply() {
return BIG_DECIMAL.multiply(BigDecimal.valueOf(100));
}

@Benchmark
public BigDecimal move2Right() {
return BIG_DECIMAL.movePointRight(2);
}

@Benchmark
public BigDecimal scaleByPowerOfTen() {
return BIG_DECIMAL.scaleByPowerOfTen(2);
}

public static void main(String[] args) throws Exception {
runBenchmark(BigDecMultiplyVsMove.class);
}
}
35 changes: 35 additions & 0 deletions src/jmh/java/com/patotski/performance/lombok/BigDecimalTuple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.patotski.performance.lombok;

import java.math.BigDecimal;

public record BigDecimalTuple(BigDecimal first, BigDecimal second) {
public static BigDecimalTupleBuilder builder() {
return new BigDecimalTupleBuilder();
}

public static class BigDecimalTupleBuilder {
private BigDecimal first;
private BigDecimal second;

BigDecimalTupleBuilder() {
}

public BigDecimalTupleBuilder first(BigDecimal first) {
this.first = first;
return this;
}

public BigDecimalTupleBuilder second(BigDecimal second) {
this.second = second;
return this;
}

public BigDecimalTuple build() {
return new BigDecimalTuple(this.first, this.second);
}

public String toString() {
return "BigDecimalTuple.BigDecimalTupleBuilder(first=" + this.first + ", second=" + this.second + ")";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.patotski.performance.lombok;

import org.openjdk.jmh.annotations.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.patotski.performance.utils.BenchmarkUtils.runBenchmark;


@BenchmarkMode({Mode.AverageTime})
@Warmup(iterations = 2, time = 2, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 10, time = 10, timeUnit = TimeUnit.SECONDS)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
public class BuilderVsConstructor {
public static final BigDecimal ONE_HUNDRED = new BigDecimal(100);

@Benchmark
public BigDecimalTuple builder() {
return BigDecimalTuple.builder()
.first(BigDecimal.ZERO)
.second(BigDecimal.ONE)
.build();
}

@Benchmark
public BigDecimalTuple constructor() {
return new BigDecimalTuple(BigDecimal.ZERO, BigDecimal.ONE);
}

public static void main(String[] args) throws Exception {
runBenchmark(BuilderVsConstructor.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.io.File;

public class BenchmarkUtils {

public static void runBenchmark(Class<?> clazz) throws Exception {
new File("reports").mkdirs();
Options opt = new OptionsBuilder()
.include(clazz.getSimpleName())
.resultFormat(ResultFormatType.JSON)
Expand Down

0 comments on commit e8c6add

Please sign in to comment.