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

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