Skip to content

Commit

Permalink
✨ 升级,添加更多压测
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunMengLu committed Aug 17, 2022
1 parent 6da5743 commit cf5c237
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 20 deletions.
11 changes: 4 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
apply plugin: "java"

ext {
jmhVersion = "1.33"
micaVersion = "2.5.5"
lombokVersion = "1.18.20"
jmhVersion = "1.35"
micaVersion = "2.7.2"
lombokVersion = "1.18.24"
}

repositories {
mavenLocal()
maven { url "https://repo.spring.io/libs-release" }
maven { url "https://repo.spring.io/milestone" }
maven { url "http://maven.aliyun.com/nexus/content/groups/public" }
// 添加 snapshots 库地址
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
Expand All @@ -21,8 +19,7 @@ dependencies {
compile "net.dreamlu:mica-core:${micaVersion}"
compile "net.dreamlu:mica-http:${micaVersion}"
compile "com.github.yangtu222:BeanUtils:1.0.11"
compile "cn.hutool:hutool-core:5.6.3"
compile "org.apache.commons:commons-lang:3.12.0"
compile "cn.hutool:hutool-all:5.8.5"
compile "org.openjdk.jmh:jmh-core:${jmhVersion}"
compile "org.mapstruct:mapstruct:1.4.2.Final"
compile "fr.xebia.extras:selma:1.0"
Expand Down
16 changes: 8 additions & 8 deletions src/jmh/benchmarks/date/DateBenchmarkDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public Date localDate() {
return Date.from(instant);
}

@Benchmark
public Date localTime() {
LocalTime lt = LocalTime.now();
Instant instant = lt.atDate(LocalDate.EPOCH)
.atZone(ZoneOffset.UTC)
.toInstant();
return Date.from(instant);
}
// @Benchmark
// public Date localTime() {
// LocalTime lt = LocalTime.now();
// Instant instant = lt.atDate(LocalDate.EPOCH)
// .atZone(ZoneOffset.UTC)
// .toInstant();
// return Date.from(instant);
// }

public static void main(String[] args) throws RunnerException {
Options opts = new OptionsBuilder()
Expand Down
2 changes: 1 addition & 1 deletion src/jmh/benchmarks/date/DateBenchmarkTest1.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package jmh.benchmarks.date;

import cn.hutool.core.date.format.FastDateFormat;
import net.dreamlu.mica.core.utils.DateUtil;
import org.apache.commons.lang3.time.FastDateFormat;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
Expand Down
2 changes: 1 addition & 1 deletion src/jmh/benchmarks/date/DateBenchmarkTest2.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package jmh.benchmarks.date;

import org.apache.commons.lang3.time.FastDateFormat;
import cn.hutool.core.date.format.FastDateFormat;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
Expand Down
51 changes: 51 additions & 0 deletions src/jmh/benchmarks/hex/HexBenchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package jmh.benchmarks.hex;

import cn.hutool.crypto.SecureUtil;
import net.dreamlu.mica.core.utils.DigestUtil;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

/**
* hex 测试,1 毫秒的吞吐量
*
* @author L.cm
*/
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Thread)
public class HexBenchmark {

@Benchmark
public String md5HexUtils() {
return HexUtils.md5Hex("Miss程_高精准IP地址库 评论了你的文章 mica 2.0.1 发布新增最好用的 ip2region boot stater", StandardCharsets.UTF_8);
}

@Benchmark
public String md5HexMica() {
return DigestUtil.md5Hex("Miss程_高精准IP地址库 评论了你的文章 mica 2.0.1 发布新增最好用的 ip2region boot stater");
}

@Benchmark
public String md5HexHutool() {
return SecureUtil.md5().digestHex("Miss程_高精准IP地址库 评论了你的文章 mica 2.0.1 发布新增最好用的 ip2region boot stater");
}

public static void main(String[] args) throws RunnerException {
Options opts = new OptionsBuilder()
.include(HexBenchmark.class.getSimpleName())
.warmupIterations(3)
.measurementIterations(3)
.jvmArgs("-server")
.forks(1)
.resultFormat(ResultFormatType.TEXT)
.build();
new Runner(opts).run();
}
}
43 changes: 43 additions & 0 deletions src/jmh/benchmarks/hex/HexUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package jmh.benchmarks.hex;

import java.nio.charset.Charset;
import java.security.MessageDigest;

public class HexUtils {

private static final String[] HEX_DIGITS = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

private static String byteArrayToHexString(byte[] b) {
StringBuilder resultSb = new StringBuilder();
for (byte value : b) {
resultSb.append(byteToHexString(value));
}
return resultSb.toString();
}

private static String byteToHexString(byte b) {
int n = b;
if (n < 0){
n += 256;
}
int d1 = n / 16;
int d2 = n % 16;
return HEX_DIGITS[d1] + HEX_DIGITS[d2];
}

public static String md5Hex(String origin, Charset charsetName) {
String resultString = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
if (charsetName == null) {
resultString = byteArrayToHexString(md.digest(origin.getBytes()));
} else {
resultString = byteArrayToHexString(md.digest(origin.getBytes(charsetName)));
}
} catch (Exception ex) {

}
return resultString;
}

}
17 changes: 14 additions & 3 deletions src/jmh/benchmarks/uuid/UUIDBenchmark.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jmh.benchmarks.uuid;

import cn.hutool.core.lang.id.NanoId;
import cn.hutool.core.util.IdUtil;
import net.dreamlu.mica.core.utils.StringUtil;
import org.openjdk.jmh.annotations.*;
Expand All @@ -19,7 +20,7 @@
* @author L.cm
*/
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Thread)
public class UUIDBenchmark {

Expand All @@ -28,6 +29,11 @@ public String micaUUId() {
return StringUtil.getUUID();
}

@Benchmark
public String randomUUID() {
return NanoId.randomNanoId();
}

@Benchmark
public String hutoolFastSimpleUUID() {
return IdUtil.fastSimpleUUID();
Expand All @@ -38,6 +44,11 @@ public String jdk8UUId() {
return UUID.randomUUID().toString();
}

@Benchmark
public String micaNanoId() {
return StringUtil.getNanoId();
}

@Benchmark
public String jdk8ThreadLocalRandomUUId() {
ThreadLocalRandom random = ThreadLocalRandom.current();
Expand All @@ -48,8 +59,8 @@ public String jdk8ThreadLocalRandomUUId() {
public static void main(String[] args) throws RunnerException {
Options opts = new OptionsBuilder()
.include(UUIDBenchmark.class.getSimpleName())
.warmupIterations(5)
.measurementIterations(5)
.warmupIterations(3)
.measurementIterations(3)
.jvmArgs("-server")
.forks(1)
.resultFormat(ResultFormatType.TEXT)
Expand Down

0 comments on commit cf5c237

Please sign in to comment.