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.

uTest.pas 5.1 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. unit uTest;
  2. interface
  3. uses
  4. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, System.DateUtils,
  5. Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls, Vcl.ExtCtrls, uIdGeneratorOptions, uIIdGenerator,
  6. uDefaultIdGenerator, uYitIdHelper;
  7. type
  8. TfTest = class(TForm)
  9. mmo1: TMemo;
  10. Panel1: TPanel;
  11. Button1: TButton;
  12. btnTimeStamp: TButton;
  13. Button2: TButton;
  14. rgSelect: TRadioGroup;
  15. procedure Button1Click(Sender: TObject);
  16. procedure btnTimeStampClick(Sender: TObject);
  17. procedure Button2Click(Sender: TObject);
  18. procedure FormCreate(Sender: TObject);
  19. procedure FormClose(Sender: TObject; var Action: TCloseAction);
  20. private
  21. { Private declarations }
  22. IdGeneratorOption: TIdGeneratorOptions;
  23. YitIdHelper: TYitIdHelper;
  24. public
  25. { Public declarations }
  26. end;
  27. var
  28. fTest: TfTest;
  29. implementation
  30. {$R *.dfm}
  31. function GetMillisecondTimeStamp(): Int64;
  32. var
  33. ST: TDateTime;
  34. begin
  35. ST := EncodeDateTime(1970, 1, 1, 0, 0, 0, 0);
  36. Result := MilliSecondsBetween(Now(), ST) - 28800000; // 8*60*60*1000;
  37. end;
  38. procedure TfTest.btnTimeStampClick(Sender: TObject);
  39. const
  40. T1 = 28800000;
  41. var
  42. SysTime: TSystemTime;
  43. Time1, Time2: TDateTime;
  44. T2, T3: Int64;
  45. begin
  46. // 뼶
  47. // mmo1.Lines.Add(IntToStr(DateTimeToUnix(EncodeDateTime(2020, 2, 20, 2, 20, 2, 20), False)));
  48. // mmo1.Lines.Add(IntToStr(DateTimeToUnix(EncodeDateTime(2020, 2, 20, 2, 20, 2, 20), True)));
  49. // mmo1.Lines.Add(FormatDateTime('yyyy-MM-dd HH:mm:ss.zzz',
  50. // UnixToDateTime(DateTimeToUnix(EncodeDateTime(2020, 2, 20, 2, 20, 2, 20), False), False)));
  51. mmo1.Lines.Add('ʱתΪUTCʱ䣺' + IntToStr(DateTimeToUnix(Now(), False)));
  52. mmo1.Lines.Add('ֱȡUTCʱ䣺' + IntToStr(DateTimeToUnix(Now(), True)));
  53. // mmo1.Lines.Add(FormatDateTime('yyyy-MM-dd HH:mm:ss.zzz', UnixToDateTime(DateTimeToUnix(Now(), False), False)));
  54. // mmo1.Lines.Add(FormatDateTime('yyyy-MM-dd HH:mm:ss.zzz', UnixToDateTime(DateTimeToUnix(Now(), True), True)));
  55. // 뼶
  56. Time2 := EncodeDateTime(1970, 1, 1, 0, 0, 0, 0);
  57. // δʱ
  58. GetLocalTime(SysTime);
  59. Time1 := SystemTimeToDateTime(SysTime);
  60. T2 := MilliSecondsBetween(Time1, Time2);
  61. T3 := T2 - T1;
  62. mmo1.Lines.Add('LocalTime' + IntToStr(T3));
  63. // ʱ
  64. GetSystemTime(SysTime);
  65. Time1 := SystemTimeToDateTime(SysTime);
  66. T2 := MilliSecondsBetween(Time1, Time2);
  67. T3 := T2 - T1;
  68. mmo1.Lines.Add('SystemTime' + IntToStr(T3));
  69. // δʱ
  70. Time1 := Now();
  71. T2 := MilliSecondsBetween(Time1, Time2);
  72. T3 := T2 - T1;
  73. mmo1.Lines.Add('Now' + IntToStr(T3));
  74. end;
  75. procedure TfTest.Button1Click(Sender: TObject);
  76. var
  77. options: TIdGeneratorOptions;
  78. IdGen: IIdGenerator;
  79. i, j: Integer;
  80. begin
  81. // ʹ÷ʽһֱʹ
  82. options := TIdGeneratorOptions.Create;
  83. try
  84. with options do
  85. begin
  86. Method := rgSelect.ItemIndex + 1;
  87. // BaseTime := DateTime.Now.AddYears(-10);
  88. WorkerId := 2;
  89. WorkerIdBitLength := 6;
  90. SeqBitLength := 6;
  91. MaxSeqNumber := 0;
  92. MinSeqNumber := 5;
  93. TopOverCostCount := 2000;
  94. DataCenterId := 0;
  95. DataCenterIdBitLength := 0;
  96. TimestampType := 0;
  97. end;
  98. IdGen := TDefaultIdGenerator.Create(options);
  99. j := 100;
  100. Screen.Cursor := crHourGlass;
  101. mmo1.Lines.BeginUpdate();
  102. try
  103. for i := 1 to j do
  104. begin
  105. mmo1.Lines.Add(FormatFloat('000', i) + '' + IntToStr(IdGen.NewLong()));
  106. end;
  107. mmo1.Perform(WM_VSCROLL, SB_BOTTOM, 0)
  108. finally
  109. mmo1.Lines.EndUpdate();
  110. Screen.Cursor := crDefault;
  111. end;
  112. finally
  113. options.Free;
  114. end;
  115. end;
  116. procedure TfTest.Button2Click(Sender: TObject);
  117. const
  118. Total = 5000000;
  119. var
  120. i: Integer;
  121. Id: Int64;
  122. ST, ET, Elapse: Cardinal;
  123. begin
  124. Screen.Cursor := crHourGlass;
  125. try
  126. ST := GetTickCount();
  127. for i := 1 to Total do
  128. begin
  129. Id := YitIdHelper.NextId();
  130. end;
  131. ET := GetTickCount();
  132. finally
  133. Screen.Cursor := crDefault;
  134. end;
  135. Elapse := ET - ST;
  136. mmo1.Lines.Add(Format('500ʱ䣺%d룬ƽÿ%d', [Elapse, Trunc((Total / Elapse) * 1000)]));
  137. end;
  138. procedure TfTest.FormClose(Sender: TObject; var Action: TCloseAction);
  139. begin
  140. YitIdHelper.Free;
  141. IdGeneratorOption.Free;
  142. end;
  143. procedure TfTest.FormCreate(Sender: TObject);
  144. begin
  145. IdGeneratorOption := TIdGeneratorOptions.Create;
  146. // Ĭϲ
  147. with IdGeneratorOption do
  148. begin
  149. Method := rgSelect.ItemIndex + 1;
  150. // BaseTime := DateTime.Now.AddYears(-10);
  151. WorkerId := 2;
  152. WorkerIdBitLength := 6;
  153. SeqBitLength := 6;
  154. MaxSeqNumber := 0;
  155. MinSeqNumber := 5;
  156. TopOverCostCount := 2000;
  157. DataCenterId := 0;
  158. DataCenterIdBitLength := 0;
  159. TimestampType := 0;
  160. end;
  161. // ʹ÷ʽװʹ
  162. YitIdHelper := TYitIdHelper.Create;
  163. YitIdHelper.SetIdGenerator(IdGeneratorOption);
  164. end;
  165. end.