1
0
mirror of synced 2026-10-31 12:16:39 +08:00

auto commit

This commit is contained in:
yitter
2021-04-09 21:15:41 +08:00
parent d8c73f7cdc
commit c5dc18e938
29 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
module GenTest;
import yitter.contract.IIdGenerator;
import std.conv;
import std.datetime;
import std.stdio;
class GenTest {
private IIdGenerator IdGen;
private int GenIdCount;
private int WorkerId;
this(IIdGenerator idGen, int genIdCount, int workerId) {
GenIdCount = genIdCount;
IdGen = idGen;
WorkerId = workerId;
}
void GenStart() {
MonoTime start = MonoTime.currTime();
long id = 0;
for (int i = 0; i < GenIdCount; i++) {
id = IdGen.newLong();
}
MonoTime end = MonoTime.currTime();
Duration dur = end - start;
// writeln(id);
writeln("++++++++++++++++++++++++++++++++++++++++WorkerId: "
~ WorkerId.to!string() ~ ", total: " ~ dur.total!("usecs").to!string() ~ " us");
}
}

View File

@@ -0,0 +1,52 @@
import std.stdio;
import yitter;
import GenTest;
import core.thread;
import std.conv;
import std.datetime;
import std.stdio;
/**
* 测试结果:
* (1):1W并发,方法 1只要 1ms.而方法 2 要 180ms。
* (2):5W并发,方法 1只要 3ms.而方法 2 要 900ms。
* [不同CPU可能结果有差异,但相对大小不变]
* 默认配置下,最佳性能是5W/s-8W/s
*/
enum int genIdCount = 50000;
//1-漂移算法,2-传统算法
enum short method = 1;
void main()
{
IdGeneratorOptions options = new IdGeneratorOptions();
options.Method = method;
options.BaseTime = 1582206693000L;
options.WorkerId = 1;
IIdGenerator idGen = new DefaultIdGenerator(options);
GenTest.GenTest genTest = new GenTest.GenTest(idGen, genIdCount, options.WorkerId);
// 首先测试一下 IdHelper 方法,获取单个Id
YitIdHelper.setIdGenerator(options);
long newId = YitIdHelper.nextId();
writeln("=====================================");
writeln("Method " ~ method.to!string() ~ " used, the result Id:" ~ newId.to!string());
// 然后循环测试一下,看看并发请求时的耗时情况
try {
while (true) {
genTest.GenStart();
Thread.sleep(1000.msecs);
// writeln("Hello World! D");
}
} catch (Exception e) {
writeln(e.toString());
}
}