You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Program.cs 6.1 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using Yitter.IdGenerator;
  6. namespace Yitter.OrgSystem.TestA
  7. {
  8. class Program
  9. {
  10. // 测试参数(默认配置下,最佳性能是10W/s)
  11. static int genIdCount = 50000; // 计算ID数量(如果要验证50W效率,请将TopOverCostCount设置为20000或适当增加SeqBitLength)
  12. static short method = 1; // 1-漂移算法,2-传统算法
  13. static bool single = true;
  14. static bool outputLog = true;
  15. static IIdGenerator IdGen = null;
  16. static IList<GenTest> testList = new List<GenTest>();
  17. static bool checkResult = false;
  18. public static int Count = 0;
  19. static int workerCount = 1;
  20. static void Main(string[] args)
  21. {
  22. var options = new IdGeneratorOptions()
  23. {
  24. Method = method,
  25. WorkerId = 1,
  26. //TopOverCostCount = 2000,
  27. //WorkerIdBitLength = 1,
  28. //SeqBitLength = 2,
  29. //MinSeqNumber = 1,
  30. // MaxSeqNumber = 200,
  31. BaseTime = DateTime.Now.AddYears(-10),
  32. };
  33. IIdGenerator IdGen = new DefaultIdGenerator(options);
  34. GenTest genTest = new GenTest(IdGen, genIdCount, options.WorkerId);
  35. // 首先测试一下 IdHelper 方法,获取单个Id
  36. YitIdHelper.SetIdGenerator(options);
  37. long newId = YitIdHelper.NextId();
  38. Console.WriteLine("=====================================");
  39. Console.WriteLine("这是用方法 " + method + " 生成的 Id:" + newId);
  40. while (true)
  41. {
  42. Go(options);
  43. Thread.Sleep(1000); // 每隔3秒执行一次Go
  44. Console.WriteLine("Hello World!");
  45. }
  46. }
  47. private static void Go(IdGeneratorOptions options)
  48. {
  49. Count = 0;
  50. testList = new List<GenTest>();
  51. // ++++++++++++++++++++++++++++++++
  52. if (single)
  53. {
  54. if (IdGen == null)
  55. {
  56. IdGen = new DefaultIdGenerator(options);
  57. }
  58. if (outputLog)
  59. {
  60. IdGen.GenIdActionAsync = (arg =>
  61. {
  62. if (arg.ActionType == 1)
  63. {
  64. Console.WriteLine($">>>> {arg.WorkerId}:开始:{DateTime.Now.ToString("mm:ss:fff")}, 周期次序:{arg.TermIndex}");
  65. }
  66. else if (arg.ActionType == 2)
  67. {
  68. Console.WriteLine($"<<<< {arg.WorkerId}:结束:{DateTime.Now.ToString("mm:ss:fff")},漂移 {arg.OverCostCountInOneTerm} 次,产生 {arg.GenCountInOneTerm} 个, 周期次序:{arg.TermIndex}");
  69. }
  70. if (arg.ActionType == 8)
  71. {
  72. Console.WriteLine($"---- {arg.WorkerId}:AA结束:{DateTime.Now.ToString("mm:ss:fff")},时间回拨");
  73. }
  74. });
  75. }
  76. for (int i = 1; i < workerCount + 1; i++)
  77. {
  78. Console.WriteLine("Gen:" + i);
  79. var test = new GenTest(IdGen, genIdCount, i);
  80. testList.Add(test);
  81. // test.GenId();
  82. test.GenStart();
  83. }
  84. }
  85. else
  86. {
  87. for (int i = 1; i < workerCount + 1; i++)
  88. {
  89. IdGeneratorOptions newOptions = new IdGeneratorOptions()
  90. {
  91. WorkerId = (ushort)i, // workerId 不能设置为0
  92. WorkerIdBitLength = options.WorkerIdBitLength,
  93. SeqBitLength = options.SeqBitLength,
  94. MinSeqNumber = options.MinSeqNumber,
  95. MaxSeqNumber = options.MaxSeqNumber,
  96. Method = options.Method,
  97. };
  98. Console.WriteLine("Gen:" + i);
  99. var idGen2 = new DefaultIdGenerator(newOptions);
  100. var test = new GenTest(idGen2, genIdCount, i);
  101. if (outputLog)
  102. {
  103. idGen2.GenIdActionAsync = (arg =>
  104. {
  105. Console.WriteLine($"{DateTime.Now.ToString("mm:ss:fff")} {arg.WorkerId} 漂移了 {arg.OverCostCountInOneTerm}, 顺序:{arg.TermIndex}");
  106. });
  107. }
  108. testList.Add(test);
  109. // test.GenId();
  110. test.GenStart();
  111. }
  112. }
  113. while (Count < workerCount)
  114. {
  115. //Console.WriteLine("Count:" + Count);
  116. Thread.Sleep(1000);
  117. }
  118. //Console.WriteLine("Count:" + Count);
  119. if (!checkResult)
  120. {
  121. return;
  122. }
  123. var dupCount = 0;
  124. foreach (var id in testList[0].idList)
  125. {
  126. if (id == 0)
  127. {
  128. Console.WriteLine("############### 错误的ID:" + id + "###########");
  129. }
  130. for (int j = 1; j < testList.Count; j++)
  131. {
  132. if (testList[j].idList.Contains(id))
  133. {
  134. dupCount++;
  135. Console.WriteLine("xxxxxxxxxx 重复的ID:" + id);
  136. }
  137. }
  138. }
  139. if (dupCount > 0)
  140. {
  141. Console.WriteLine($"重复数量:{dupCount}");
  142. }
  143. }
  144. }
  145. }

雪花算法中非常好用的数字ID生成器