1
0
mirror of synced 2026-02-16 01:47:55 +08:00
Files
SnowFlake-IdGenerator/D/test/source/app.d
BitWorld a82d9cb063 !6 D语言移植
* Porting to D
* The pthread needed.
2021-04-08 21:04:43 +08:00

53 lines
1.3 KiB
D
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 = SysTime(DateTime(2020, 2, 20, 21, 51, 33));
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.to!string() ~ " 生成的 Id" ~ newId.to!string());
// 然后循环测试一下,看看并发请求时的耗时情况
try {
while (true) {
genTest.GenStart();
// Thread.sleep(200.msecs); // 每隔1秒执行一次GenStart
// writeln("Hello World! D");
}
} catch (Exception e) {
writeln(e.toString());
}
}