-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6da5743
commit cf5c237
Showing
7 changed files
with
122 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters