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.2 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. // RustDll();
  47. Thread.Sleep(1000); // 每隔1秒执行一次Go
  48. }
  49. }
  50. [DllImport("yitidgen.dll", CallingConvention = CallingConvention.StdCall)]
  51. public static extern long NextId();
  52. private static void RustDll()
  53. {
  54. int i = 0;
  55. long id = 0;
  56. DateTime start = DateTime.Now;
  57. while (i < 50000)
  58. {
  59. id = NextId();
  60. i++;
  61. }
  62. DateTime end = DateTime.Now;
  63. Console.WriteLine("id:" + id);
  64. Console.WriteLine($"+++++++++++C# call rust dll, gen 5W, total: {(end - start).TotalMilliseconds} ms");
  65. }
  66. private static void RunSingle()
  67. {
  68. DateTime start = DateTime.Now;
  69. for (int i = 0; i < genIdCount; i++)
  70. {
  71. var id = IdGen.NewLong();
  72. }
  73. DateTime end = DateTime.Now;
  74. Console.WriteLine($"++++++++++++++++++++++++++++++++++++++++, total: {(end - start).TotalMilliseconds} ms");
  75. Interlocked.Increment(ref Program.Count);
  76. }
  77. private static void Go(IdGeneratorOptions options)
  78. {
  79. Count = 0;
  80. testList = new List<GenTest>();
  81. // ++++++++++++++++++++++++++++++++
  82. if (single)
  83. {
  84. if (IdGen == null)
  85. {
  86. IdGen = new DefaultIdGenerator(options);
  87. }
  88. if (outputLog)
  89. {
  90. IdGen.GenIdActionAsync = (arg =>
  91. {
  92. if (arg.ActionType == 1)
  93. {
  94. Console.WriteLine($">>>> {arg.WorkerId}:开始:{DateTime.Now.ToString("mm:ss:fff")}, 周期次序:{arg.TermIndex}");
  95. }
  96. else if (arg.ActionType == 2)
  97. {
  98. Console.WriteLine($"<<<< {arg.WorkerId}:结束:{DateTime.Now.ToString("mm:ss:fff")},漂移 {arg.OverCostCountInOneTerm} 次,产生 {arg.GenCountInOneTerm} 个, 周期次序:{arg.TermIndex}");
  99. }
  100. if (arg.ActionType == 8)
  101. {
  102. Console.WriteLine($"---- {arg.WorkerId}:AA结束:{DateTime.Now.ToString("mm:ss:fff")},时间回拨");
  103. }
  104. });
  105. }
  106. for (int i = 1; i < workerCount + 1; i++)
  107. {
  108. Console.WriteLine("Gen:" + i);
  109. var test = new GenTest(IdGen, genIdCount, i);
  110. testList.Add(test);
  111. // test.GenId();
  112. test.GenStart();
  113. }
  114. }
  115. else
  116. {
  117. for (int i = 1; i < workerCount + 1; i++)
  118. {
  119. IdGeneratorOptions newOptions = new IdGeneratorOptions()
  120. {
  121. WorkerId = (ushort)i, // workerId 不能设置为0
  122. WorkerIdBitLength = options.WorkerIdBitLength,
  123. SeqBitLength = options.SeqBitLength,
  124. MinSeqNumber = options.MinSeqNumber,
  125. MaxSeqNumber = options.MaxSeqNumber,
  126. Method = options.Method,
  127. };
  128. Console.WriteLine("Gen:" + i);
  129. var idGen2 = new DefaultIdGenerator(newOptions);
  130. var test = new GenTest(idGen2, genIdCount, i);
  131. if (outputLog)
  132. {
  133. idGen2.GenIdActionAsync = (arg =>
  134. {
  135. Console.WriteLine($"{DateTime.Now.ToString("mm:ss:fff")} {arg.WorkerId} 漂移了 {arg.OverCostCountInOneTerm}, 顺序:{arg.TermIndex}");
  136. });
  137. }
  138. testList.Add(test);
  139. // test.GenId();
  140. test.GenStart();
  141. }
  142. }
  143. while (Count < workerCount)
  144. {
  145. //Console.WriteLine("Count:" + Count);
  146. Thread.Sleep(1000);
  147. }
  148. //Console.WriteLine("Count:" + Count);
  149. if (!checkResult)
  150. {
  151. return;
  152. }
  153. var dupCount = 0;
  154. foreach (var id in testList[0].idList)
  155. {
  156. if (id == 0)
  157. {
  158. Console.WriteLine("############### 错误的ID:" + id + "###########");
  159. }
  160. for (int j = 1; j < testList.Count; j++)
  161. {
  162. if (testList[j].idList.Contains(id))
  163. {
  164. dupCount++;
  165. Console.WriteLine("xxxxxxxxxx 重复的ID:" + id);
  166. }
  167. }
  168. }
  169. if (dupCount > 0)
  170. {
  171. Console.WriteLine($"重复数量:{dupCount}");
  172. }
  173. }
  174. }
  175. }

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