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

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