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

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