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.8 kB

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