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 9.3 kB

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

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