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 5.6 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
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. while (true)
  23. {
  24. Go();
  25. Thread.Sleep(1000); // 每隔3秒执行一次Go
  26. Console.WriteLine("Hello World!");
  27. }
  28. }
  29. private static void Go()
  30. {
  31. Count = 0;
  32. testList = new List<GenTest>();
  33. var newConfig = new IdGeneratorOptions()
  34. {
  35. Method = method,
  36. WorkerId = 1,
  37. TopOverCostCount = 10000,
  38. //WorkerIdBitLength = 6,
  39. //SeqBitLength = 9,
  40. //MinSeqNumber = 11,
  41. //MaxSeqNumber = 200,
  42. StartTime = DateTime.Now.AddYears(-10),
  43. };
  44. // ++++++++++++++++++++++++++++++++
  45. if (single)
  46. {
  47. if (IdGen == null)
  48. {
  49. IdGen = new DefaultIdGenerator(newConfig);
  50. }
  51. if (outputLog)
  52. {
  53. IdGen.GenIdActionAsync = (arg =>
  54. {
  55. if (arg.ActionType == 1)
  56. {
  57. Console.WriteLine($">>>> {arg.WorkerId}:开始:{DateTime.Now.ToString("mm:ss:fff")}, 周期次序:{arg.TermIndex}");
  58. }
  59. else if (arg.ActionType == 2)
  60. {
  61. Console.WriteLine($"<<<< {arg.WorkerId}:结束:{DateTime.Now.ToString("mm:ss:fff")},漂移 {arg.OverCostCountInOneTerm} 次,产生 {arg.GenCountInOneTerm} 个, 周期次序:{arg.TermIndex}");
  62. }
  63. if (arg.ActionType == 8)
  64. {
  65. Console.WriteLine($"---- {arg.WorkerId}:AA结束:{DateTime.Now.ToString("mm:ss:fff")},时间回拨");
  66. }
  67. });
  68. }
  69. for (int i = 1; i < workerCount + 1; i++)
  70. {
  71. Console.WriteLine("Gen:" + i);
  72. var test = new GenTest(IdGen, genIdCount, i);
  73. testList.Add(test);
  74. // test.GenId();
  75. test.GenStart();
  76. }
  77. }
  78. else
  79. {
  80. for (int i = 1; i < workerCount + 1; i++)
  81. {
  82. IdGeneratorOptions options =
  83. new IdGeneratorOptions()
  84. {
  85. WorkerId = (ushort)i, // workerId 不能设置为0
  86. WorkerIdBitLength = newConfig.WorkerIdBitLength,
  87. SeqBitLength = newConfig.SeqBitLength,
  88. MinSeqNumber = newConfig.MinSeqNumber,
  89. MaxSeqNumber = newConfig.MaxSeqNumber,
  90. Method = newConfig.Method,
  91. };
  92. Console.WriteLine("Gen:" + i);
  93. var idGen2 = new DefaultIdGenerator(options);
  94. var test = new GenTest(idGen2, genIdCount, i);
  95. if (outputLog)
  96. {
  97. idGen2.GenIdActionAsync = (arg =>
  98. {
  99. Console.WriteLine($"{DateTime.Now.ToString("mm:ss:fff")} {arg.WorkerId} 漂移了 {arg.OverCostCountInOneTerm}, 顺序:{arg.TermIndex}");
  100. });
  101. }
  102. testList.Add(test);
  103. // test.GenId();
  104. test.GenStart();
  105. }
  106. }
  107. while (Count < workerCount)
  108. {
  109. //Console.WriteLine("Count:" + Count);
  110. Thread.Sleep(1000);
  111. }
  112. //Console.WriteLine("Count:" + Count);
  113. if (!checkResult)
  114. {
  115. return;
  116. }
  117. var dupCount = 0;
  118. foreach (var id in testList[0].idList)
  119. {
  120. if (id == 0)
  121. {
  122. Console.WriteLine("############### 错误的ID:" + id + "###########");
  123. }
  124. for (int j = 1; j < testList.Count; j++)
  125. {
  126. if (testList[j].idList.Contains(id))
  127. {
  128. dupCount++;
  129. Console.WriteLine("xxxxxxxxxx 重复的ID:" + id);
  130. }
  131. }
  132. }
  133. if (dupCount > 0)
  134. {
  135. Console.WriteLine($"重复数量:{dupCount}");
  136. }
  137. }
  138. }
  139. }

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