1
0
mirror of synced 2026-04-03 01:58:41 +08:00
This commit is contained in:
zhouzj
2021-03-19 22:14:50 +08:00
parent 7ade620f9c
commit ffb9978f07
28 changed files with 476 additions and 5 deletions

View File

@@ -1,49 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Yitter.IdGenerator;
namespace Yitter.OrgSystem.TestA
{
public class GenTest
{
private IIdGenerator IdGen;
private Hashtable ids = new Hashtable();
public IList<long> idList = new List<long>();
private int GenIdCount;
private int WorkerId;
public GenTest(IIdGenerator idGen, int genIdCount, int workerId)
{
GenIdCount = genIdCount;
IdGen = idGen;
WorkerId = workerId;
}
//public void GenId()
//{
// Thread t = new Thread(new ThreadStart(Gen1Start));
// t.Start();
//}
public void GenStart()
{
DateTime start = DateTime.Now;
for (int i = 0; i < GenIdCount; i++)
{
var id = IdGen.NewLong();
//ids.Add(id, i);
//idList.Add(id);
}
DateTime end = DateTime.Now;
Console.WriteLine($"++++++++++++++++++++++++++++++++++++++++WorkerId: {WorkerId}, total: {(end - start).TotalSeconds} s");
Interlocked.Increment(ref Program.Count);
}
}
}

View File

@@ -1,172 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using Yitter.IdGenerator;
namespace Yitter.OrgSystem.TestA
{
class Program
{
// 测试参数默认配置下最佳性能是10W/s
static int genIdCount = 50000; // 计算ID数量如果要验证50W效率请将TopOverCostCount设置为20000或适当增加SeqBitLength
static short method = 1; // 1-漂移算法2-传统算法
static bool single = true;
static bool outputLog = true;
static IIdGenerator IdGen = null;
static IList<GenTest> testList = new List<GenTest>();
static bool checkResult = false;
public static int Count = 0;
static int workerCount = 1;
static void Main(string[] args)
{
var options = new IdGeneratorOptions()
{
Method = method,
WorkerId = 1,
//TopOverCostCount = 2000,
//WorkerIdBitLength = 1,
//SeqBitLength = 2,
//MinSeqNumber = 1,
// MaxSeqNumber = 200,
BaseTime = DateTime.Now.AddYears(-10),
};
IIdGenerator IdGen = new DefaultIdGenerator(options);
GenTest genTest = new GenTest(IdGen, genIdCount, options.WorkerId);
// 首先测试一下 IdHelper 方法获取单个Id
YitIdHelper.SetIdGenerator(options);
long newId = YitIdHelper.NextId();
Console.WriteLine("=====================================");
Console.WriteLine("这是用方法 " + method + " 生成的 Id" + newId);
while (true)
{
Go(options);
Thread.Sleep(1000); // 每隔3秒执行一次Go
Console.WriteLine("Hello World!");
}
}
private static void Go(IdGeneratorOptions options)
{
Count = 0;
testList = new List<GenTest>();
// ++++++++++++++++++++++++++++++++
if (single)
{
if (IdGen == null)
{
IdGen = new DefaultIdGenerator(options);
}
if (outputLog)
{
IdGen.GenIdActionAsync = (arg =>
{
if (arg.ActionType == 1)
{
Console.WriteLine($">>>> {arg.WorkerId}:开始:{DateTime.Now.ToString("mm:ss:fff")}, 周期次序:{arg.TermIndex}");
}
else if (arg.ActionType == 2)
{
Console.WriteLine($"<<<< {arg.WorkerId}:结束:{DateTime.Now.ToString("mm:ss:fff")},漂移 {arg.OverCostCountInOneTerm} 次,产生 {arg.GenCountInOneTerm} 个, 周期次序:{arg.TermIndex}");
}
if (arg.ActionType == 8)
{
Console.WriteLine($"---- {arg.WorkerId}AA结束{DateTime.Now.ToString("mm:ss:fff")},时间回拨");
}
});
}
for (int i = 1; i < workerCount + 1; i++)
{
Console.WriteLine("Gen" + i);
var test = new GenTest(IdGen, genIdCount, i);
testList.Add(test);
// test.GenId();
test.GenStart();
}
}
else
{
for (int i = 1; i < workerCount + 1; i++)
{
IdGeneratorOptions newOptions = new IdGeneratorOptions()
{
WorkerId = (ushort)i, // workerId 不能设置为0
WorkerIdBitLength = options.WorkerIdBitLength,
SeqBitLength = options.SeqBitLength,
MinSeqNumber = options.MinSeqNumber,
MaxSeqNumber = options.MaxSeqNumber,
Method = options.Method,
};
Console.WriteLine("Gen" + i);
var idGen2 = new DefaultIdGenerator(newOptions);
var test = new GenTest(idGen2, genIdCount, i);
if (outputLog)
{
idGen2.GenIdActionAsync = (arg =>
{
Console.WriteLine($"{DateTime.Now.ToString("mm:ss:fff")} {arg.WorkerId} 漂移了 {arg.OverCostCountInOneTerm}, 顺序:{arg.TermIndex}");
});
}
testList.Add(test);
// test.GenId();
test.GenStart();
}
}
while (Count < workerCount)
{
//Console.WriteLine("Count" + Count);
Thread.Sleep(1000);
}
//Console.WriteLine("Count" + Count);
if (!checkResult)
{
return;
}
var dupCount = 0;
foreach (var id in testList[0].idList)
{
if (id == 0)
{
Console.WriteLine("############### 错误的ID" + id + "###########");
}
for (int j = 1; j < testList.Count; j++)
{
if (testList[j].idList.Contains(id))
{
dupCount++;
Console.WriteLine("xxxxxxxxxx 重复的ID" + id);
}
}
}
if (dupCount > 0)
{
Console.WriteLine($"重复数量:{dupCount}");
}
}
}
}

View File

@@ -1,19 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Version>1.0.2</Version>
<PackageProjectUrl>https://gitee.com/yitter/idgenerator</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Yitter.IdGenerator\Yitter.IdGenerator.csproj" />
</ItemGroup>
</Project>