1
0
mirror of synced 2026-04-27 02:38:39 +08:00

!6 D语言移植

* Porting to D
* The pthread needed.
This commit is contained in:
BitWorld
2021-04-08 21:04:43 +08:00
committed by yitter
parent b3a77a2824
commit a82d9cb063
18 changed files with 768 additions and 3 deletions

38
D/test/source/GenTest.d Normal file
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!("msecs").to!string() ~ " ms");
}
}

52
D/test/source/app.d Normal file
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 = 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());
}
}