From 458bfae171f727d981e6fbf3cf0b23d31a35012f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=8C=AF=E5=AE=87?= <858514558@qq.com> Date: Sat, 9 Oct 2021 02:14:34 +0000 Subject: [PATCH] =?UTF-8?q?!15=20=E5=A2=9E=E5=8A=A0java=E6=80=A7=E8=83=BD?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=A1=86=E6=9E=B6=20*=20=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E5=8F=82=E6=95=B0=20*=20=E5=A2=9E=E5=8A=A0JMH=E6=80=A7?= =?UTF-8?q?=E8=83=BD=E6=B5=8B=E8=AF=95=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Java/source/pom.xml | 15 ++++- .../java/com/github/yitter/test/GenTest.java | 2 - .../java/com/github/yitter/test/README.md | 7 +++ .../java/com/github/yitter/test/StartUp.java | 2 - .../com/github/yitter/test/StartUpJmh.java | 62 ++++++++++++++++++ .../com/github/yitter/test/StartUpJmh2.java | 63 +++++++++++++++++++ 6 files changed, 146 insertions(+), 5 deletions(-) rename Java/source/src/{main => test}/java/com/github/yitter/test/GenTest.java (90%) create mode 100644 Java/source/src/test/java/com/github/yitter/test/README.md rename Java/source/src/{main => test}/java/com/github/yitter/test/StartUp.java (90%) create mode 100644 Java/source/src/test/java/com/github/yitter/test/StartUpJmh.java create mode 100644 Java/source/src/test/java/com/github/yitter/test/StartUpJmh2.java diff --git a/Java/source/pom.xml b/Java/source/pom.xml index bb41e2a..0d8f601 100644 --- a/Java/source/pom.xml +++ b/Java/source/pom.xml @@ -45,7 +45,20 @@ 1.8 1.8 - + + + org.openjdk.jmh + jmh-core + 1.21 + provided + + + org.openjdk.jmh + jmh-generator-annprocess + 1.21 + provided + + diff --git a/Java/source/src/main/java/com/github/yitter/test/GenTest.java b/Java/source/src/test/java/com/github/yitter/test/GenTest.java similarity index 90% rename from Java/source/src/main/java/com/github/yitter/test/GenTest.java rename to Java/source/src/test/java/com/github/yitter/test/GenTest.java index 09430a4..036ec86 100644 --- a/Java/source/src/main/java/com/github/yitter/test/GenTest.java +++ b/Java/source/src/test/java/com/github/yitter/test/GenTest.java @@ -1,8 +1,6 @@ package com.github.yitter.test; -import com.github.yitter.contract.IIdGenerator; import com.github.yitter.idgen.YitIdHelper; - import java.util.HashSet; import java.util.Set; diff --git a/Java/source/src/test/java/com/github/yitter/test/README.md b/Java/source/src/test/java/com/github/yitter/test/README.md new file mode 100644 index 0000000..571f6c4 --- /dev/null +++ b/Java/source/src/test/java/com/github/yitter/test/README.md @@ -0,0 +1,7 @@ +JMH 基准测试 漂移算法 macbook pro 13(2020) i5 jdk1.8.0_301 +``` +Benchmark Mode Cnt Score Error Units +StartUpJmh.testGetPojo thrpt 40 58835.536 ± 535.605 ops/s +StartUpJmh.testGetPojo avgt 40 ≈ 10⁻⁵ s/op +``` + diff --git a/Java/source/src/main/java/com/github/yitter/test/StartUp.java b/Java/source/src/test/java/com/github/yitter/test/StartUp.java similarity index 90% rename from Java/source/src/main/java/com/github/yitter/test/StartUp.java rename to Java/source/src/test/java/com/github/yitter/test/StartUp.java index 359c88b..a2334d4 100644 --- a/Java/source/src/main/java/com/github/yitter/test/StartUp.java +++ b/Java/source/src/test/java/com/github/yitter/test/StartUp.java @@ -1,8 +1,6 @@ package com.github.yitter.test; -import com.github.yitter.contract.IIdGenerator; import com.github.yitter.contract.IdGeneratorOptions; -import com.github.yitter.idgen.DefaultIdGenerator; import com.github.yitter.idgen.YitIdHelper; public class StartUp { diff --git a/Java/source/src/test/java/com/github/yitter/test/StartUpJmh.java b/Java/source/src/test/java/com/github/yitter/test/StartUpJmh.java new file mode 100644 index 0000000..5ba25f7 --- /dev/null +++ b/Java/source/src/test/java/com/github/yitter/test/StartUpJmh.java @@ -0,0 +1,62 @@ +package com.github.yitter.test; + +import com.github.yitter.contract.IdGeneratorOptions; +import com.github.yitter.idgen.YitIdHelper; +import java.util.concurrent.TimeUnit; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Threads; +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; + +/** + * @author suzhenyu + * @date 2021/9/27 + */ +// 测试方法平均执行时间 +@BenchmarkMode({Mode.All}) +// 输出结果的时间粒度为微秒 +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@State(Scope.Thread) +//@Threads(2) +public class StartUpJmh { + + //1-漂移算法,2-传统算法 + final static short method = 1; + + public static void main(String[] args) throws RunnerException { + Options options = new OptionsBuilder().include(StartUpJmh.class.getSimpleName()) + .warmupIterations(1).measurementIterations(5).forks(1).build(); + new Runner(options).run(); + } + + /** + * setup初始化容器的时候只执行一次 + */ + @Setup(Level.Trial) + public void init() { + IdGeneratorOptions options = new IdGeneratorOptions(); + options.WorkerIdBitLength = 6; + options.SeqBitLength = 10; + options.BaseTime = System.currentTimeMillis(); + options.Method = method; + options.WorkerId = 1; + + // 首先测试一下 IdHelper 方法,获取单个Id + YitIdHelper.setIdGenerator(options); + YitIdHelper.nextId(); + } + + @Benchmark + public void testNextId() { + YitIdHelper.nextId(); + } +} diff --git a/Java/source/src/test/java/com/github/yitter/test/StartUpJmh2.java b/Java/source/src/test/java/com/github/yitter/test/StartUpJmh2.java new file mode 100644 index 0000000..07632b0 --- /dev/null +++ b/Java/source/src/test/java/com/github/yitter/test/StartUpJmh2.java @@ -0,0 +1,63 @@ +package com.github.yitter.test; + +import com.github.yitter.contract.IdGeneratorOptions; +import com.github.yitter.idgen.YitIdHelper; +import java.util.concurrent.TimeUnit; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Threads; +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; + +/** + * @author suzhenyu + * @date 2021/9/27 + */ +// 测试方法平均执行时间 +@BenchmarkMode({Mode.All}) +// 输出结果的时间粒度为微秒 +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@State(Scope.Thread) +//@Threads(2) +public class StartUpJmh2 { + + //1-漂移算法,2-传统算法 + final static short method = 2; + + public static void main(String[] args) throws RunnerException { + Options options = new OptionsBuilder().include(StartUpJmh2.class.getName() + ".*") + .warmupIterations(1).measurementIterations(5).forks(2).build(); + new Runner(options).run(); + } + + /** + * setup初始化容器的时候只执行一次 + */ + @Setup(Level.Trial) + public void init() { + IdGeneratorOptions options = new IdGeneratorOptions(); + options.WorkerIdBitLength = 6; + options.SeqBitLength = 10; + options.BaseTime = System.currentTimeMillis(); + options.Method = method; + options.WorkerId = 1; + + // 首先测试一下 IdHelper 方法,获取单个Id + YitIdHelper.setIdGenerator(options); + YitIdHelper.nextId(); + } + + @Benchmark + public void testGetPojo() { + + YitIdHelper.nextId(); + } +}