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

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