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

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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 = 2;//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. DataCenterIdBitLength = 10,
  32. TopOverCostCount = 2000,
  33. //TimestampType = 1,
  34. // MinSeqNumber = 1,
  35. // MaxSeqNumber = 200,
  36. // BaseTime = DateTime.Now.AddYears(-10),
  37. };
  38. IdGen = new DefaultIdGenerator(options);
  39. GenTest genTest = new GenTest(IdGen, genIdCount, options.WorkerId);
  40. // 首先测试一下 IdHelper 方法,获取单个Id
  41. YitIdHelper.SetIdGenerator(options);
  42. long newId = YitIdHelper.NextId();
  43. Console.WriteLine("=====================================");
  44. Console.WriteLine("这是用方法 " + method + " 生成的 Id:" + newId);
  45. while (true)
  46. {
  47. RunSingle();
  48. //CallDll();
  49. //Go(options);
  50. Thread.Sleep(1000); // 每隔1秒执行一次Go
  51. }
  52. }
  53. private static void RunSingle()
  54. {
  55. DateTime start = DateTime.Now;
  56. for (int i = 0; i < genIdCount; i++)
  57. {
  58. var id = IdGen.NewLong();
  59. }
  60. DateTime end = DateTime.Now;
  61. Console.WriteLine($"++++++++++++++++++++++++++++++++++++++++, total: {(end - start).TotalMilliseconds} ms");
  62. Interlocked.Increment(ref Program.Count);
  63. }
  64. private static void Go(IdGeneratorOptions options)
  65. {
  66. Count = 0;
  67. testList = new List<GenTest>();
  68. // ++++++++++++++++++++++++++++++++
  69. if (single)
  70. {
  71. if (IdGen == null)
  72. {
  73. IdGen = new DefaultIdGenerator(options);
  74. }
  75. if (outputLog)
  76. {
  77. IdGen.GenIdActionAsync = (arg =>
  78. {
  79. if (arg.ActionType == 1)
  80. {
  81. Console.WriteLine($">>>> {arg.WorkerId}:开始:{DateTime.Now.ToString("mm:ss:fff")}, 周期次序:{arg.TermIndex}");
  82. }
  83. else if (arg.ActionType == 2)
  84. {
  85. Console.WriteLine($"<<<< {arg.WorkerId}:结束:{DateTime.Now.ToString("mm:ss:fff")},漂移 {arg.OverCostCountInOneTerm} 次,产生 {arg.GenCountInOneTerm} 个, 周期次序:{arg.TermIndex}");
  86. }
  87. if (arg.ActionType == 8)
  88. {
  89. Console.WriteLine($"---- {arg.WorkerId}:AA结束:{DateTime.Now.ToString("mm:ss:fff")},时间回拨");
  90. }
  91. });
  92. }
  93. for (int i = 1; i < workerCount + 1; i++)
  94. {
  95. Console.WriteLine("Gen:" + i);
  96. var test = new GenTest(IdGen, genIdCount, i);
  97. testList.Add(test);
  98. // test.GenId();
  99. test.GenStart();
  100. }
  101. }
  102. else
  103. {
  104. for (int i = 1; i < workerCount + 1; i++)
  105. {
  106. IdGeneratorOptions newOptions = new IdGeneratorOptions()
  107. {
  108. WorkerId = (ushort)i, // workerId 不能设置为0
  109. WorkerIdBitLength = options.WorkerIdBitLength,
  110. SeqBitLength = options.SeqBitLength,
  111. MinSeqNumber = options.MinSeqNumber,
  112. MaxSeqNumber = options.MaxSeqNumber,
  113. Method = options.Method,
  114. //TimestampType = 1,
  115. };
  116. Console.WriteLine("Gen:" + i);
  117. var idGen2 = new DefaultIdGenerator(newOptions);
  118. var test = new GenTest(idGen2, genIdCount, i);
  119. if (outputLog)
  120. {
  121. idGen2.GenIdActionAsync = (arg =>
  122. {
  123. Console.WriteLine($"{DateTime.Now.ToString("mm:ss:fff")} {arg.WorkerId} 漂移了 {arg.OverCostCountInOneTerm}, 顺序:{arg.TermIndex}");
  124. });
  125. }
  126. testList.Add(test);
  127. // test.GenId();
  128. test.GenStart();
  129. }
  130. }
  131. while (Count < workerCount)
  132. {
  133. //Console.WriteLine("Count:" + Count);
  134. Thread.Sleep(1000);
  135. }
  136. //Console.WriteLine("Count:" + Count);
  137. if (!checkResult)
  138. {
  139. return;
  140. }
  141. var dupCount = 0;
  142. foreach (var id in testList[0].idList)
  143. {
  144. if (id == 0)
  145. {
  146. Console.WriteLine("############### 错误的ID:" + id + "###########");
  147. }
  148. for (int j = 1; j < testList.Count; j++)
  149. {
  150. if (testList[j].idList.Contains(id))
  151. {
  152. dupCount++;
  153. Console.WriteLine("xxxxxxxxxx 重复的ID:" + id);
  154. }
  155. }
  156. }
  157. if (dupCount > 0)
  158. {
  159. Console.WriteLine($"重复数量:{dupCount}");
  160. }
  161. }
  162. }
  163. }