@@ -45,7 +45,20 @@
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>1.21</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>1.21</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
```
|
||||
|
||||
@@ -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 {
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user