diff --git a/build.gradle b/build.gradle index 4609890..021dd4b 100644 --- a/build.gradle +++ b/build.gradle @@ -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" } @@ -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" diff --git a/src/jmh/benchmarks/date/DateBenchmarkDate.java b/src/jmh/benchmarks/date/DateBenchmarkDate.java index eff72d0..02f5469 100644 --- a/src/jmh/benchmarks/date/DateBenchmarkDate.java +++ b/src/jmh/benchmarks/date/DateBenchmarkDate.java @@ -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() diff --git a/src/jmh/benchmarks/date/DateBenchmarkTest1.java b/src/jmh/benchmarks/date/DateBenchmarkTest1.java index 8da15e8..32167ce 100644 --- a/src/jmh/benchmarks/date/DateBenchmarkTest1.java +++ b/src/jmh/benchmarks/date/DateBenchmarkTest1.java @@ -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; diff --git a/src/jmh/benchmarks/date/DateBenchmarkTest2.java b/src/jmh/benchmarks/date/DateBenchmarkTest2.java index 9921d77..9e85faa 100644 --- a/src/jmh/benchmarks/date/DateBenchmarkTest2.java +++ b/src/jmh/benchmarks/date/DateBenchmarkTest2.java @@ -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; diff --git a/src/jmh/benchmarks/hex/HexBenchmark.java b/src/jmh/benchmarks/hex/HexBenchmark.java new file mode 100644 index 0000000..65fb817 --- /dev/null +++ b/src/jmh/benchmarks/hex/HexBenchmark.java @@ -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(); + } +} diff --git a/src/jmh/benchmarks/hex/HexUtils.java b/src/jmh/benchmarks/hex/HexUtils.java new file mode 100644 index 0000000..0a52c40 --- /dev/null +++ b/src/jmh/benchmarks/hex/HexUtils.java @@ -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; + } + +} diff --git a/src/jmh/benchmarks/uuid/UUIDBenchmark.java b/src/jmh/benchmarks/uuid/UUIDBenchmark.java index a0ebccf..1d25270 100644 --- a/src/jmh/benchmarks/uuid/UUIDBenchmark.java +++ b/src/jmh/benchmarks/uuid/UUIDBenchmark.java @@ -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.*; @@ -19,7 +20,7 @@ * @author L.cm */ @BenchmarkMode(Mode.Throughput) -@OutputTimeUnit(TimeUnit.SECONDS) +@OutputTimeUnit(TimeUnit.MILLISECONDS) @State(Scope.Thread) public class UUIDBenchmark { @@ -28,6 +29,11 @@ public String micaUUId() { return StringUtil.getUUID(); } + @Benchmark + public String randomUUID() { + return NanoId.randomNanoId(); + } + @Benchmark public String hutoolFastSimpleUUID() { return IdUtil.fastSimpleUUID(); @@ -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(); @@ -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)