|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- unit uTest;
-
- interface
-
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, System.DateUtils,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls, Vcl.ExtCtrls, uIdGeneratorOptions, uIIdGenerator,
- uDefaultIdGenerator, uYitIdHelper;
-
- type
- TfTest = class(TForm)
- mmo1: TMemo;
- Panel1: TPanel;
- Button1: TButton;
- btnTimeStamp: TButton;
- Button2: TButton;
- rgSelect: TRadioGroup;
- procedure Button1Click(Sender: TObject);
- procedure btnTimeStampClick(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- private
- { Private declarations }
- IdGeneratorOption: TIdGeneratorOptions;
- YitIdHelper: TYitIdHelper;
- public
- { Public declarations }
- end;
-
- var
- fTest: TfTest;
-
- implementation
-
- {$R *.dfm}
-
- function GetMillisecondTimeStamp(): Int64;
- var
- ST: TDateTime;
- begin
- ST := EncodeDateTime(1970, 1, 1, 0, 0, 0, 0);
- Result := MilliSecondsBetween(Now(), ST) - 28800000; // 8*60*60*1000;
- end;
-
- procedure TfTest.btnTimeStampClick(Sender: TObject);
- const
- T1 = 28800000;
- var
- SysTime: TSystemTime;
- Time1, Time2: TDateTime;
- T2, T3: Int64;
- begin
- // 뼶
- // mmo1.Lines.Add(IntToStr(DateTimeToUnix(EncodeDateTime(2020, 2, 20, 2, 20, 2, 20), False)));
- // mmo1.Lines.Add(IntToStr(DateTimeToUnix(EncodeDateTime(2020, 2, 20, 2, 20, 2, 20), True)));
- // mmo1.Lines.Add(FormatDateTime('yyyy-MM-dd HH:mm:ss.zzz',
- // UnixToDateTime(DateTimeToUnix(EncodeDateTime(2020, 2, 20, 2, 20, 2, 20), False), False)));
- mmo1.Lines.Add('ʱתΪUTCʱ䣺' + IntToStr(DateTimeToUnix(Now(), False)));
- mmo1.Lines.Add('ֱȡUTCʱ䣺' + IntToStr(DateTimeToUnix(Now(), True)));
- // mmo1.Lines.Add(FormatDateTime('yyyy-MM-dd HH:mm:ss.zzz', UnixToDateTime(DateTimeToUnix(Now(), False), False)));
- // mmo1.Lines.Add(FormatDateTime('yyyy-MM-dd HH:mm:ss.zzz', UnixToDateTime(DateTimeToUnix(Now(), True), True)));
-
- // 뼶
- Time2 := EncodeDateTime(1970, 1, 1, 0, 0, 0, 0);
-
- // δʱ
- GetLocalTime(SysTime);
- Time1 := SystemTimeToDateTime(SysTime);
- T2 := MilliSecondsBetween(Time1, Time2);
- T3 := T2 - T1;
- mmo1.Lines.Add('LocalTime' + IntToStr(T3));
-
- // ʱ
- GetSystemTime(SysTime);
- Time1 := SystemTimeToDateTime(SysTime);
- T2 := MilliSecondsBetween(Time1, Time2);
- T3 := T2 - T1;
- mmo1.Lines.Add('SystemTime' + IntToStr(T3));
-
- // δʱ
- Time1 := Now();
- T2 := MilliSecondsBetween(Time1, Time2);
- T3 := T2 - T1;
- mmo1.Lines.Add('Now' + IntToStr(T3));
- end;
-
- procedure TfTest.Button1Click(Sender: TObject);
- var
- options: TIdGeneratorOptions;
- IdGen: IIdGenerator;
- i, j: Integer;
- begin
- // ʹ÷ʽһֱʹ
- options := TIdGeneratorOptions.Create;
- try
- with options do
- begin
- Method := rgSelect.ItemIndex + 1;
- // BaseTime := DateTime.Now.AddYears(-10);
- WorkerId := 2;
-
- WorkerIdBitLength := 6;
- SeqBitLength := 6;
-
- MaxSeqNumber := 0;
- MinSeqNumber := 5;
-
- TopOverCostCount := 2000;
-
- DataCenterId := 0;
- DataCenterIdBitLength := 0;
-
- TimestampType := 0;
- end;
-
- IdGen := TDefaultIdGenerator.Create(options);
-
- j := 100;
- Screen.Cursor := crHourGlass;
- mmo1.Lines.BeginUpdate();
- try
- for i := 1 to j do
- begin
- mmo1.Lines.Add(FormatFloat('000', i) + '' + IntToStr(IdGen.NewLong()));
- end;
- mmo1.Perform(WM_VSCROLL, SB_BOTTOM, 0)
- finally
- mmo1.Lines.EndUpdate();
- Screen.Cursor := crDefault;
- end;
-
- finally
- options.Free;
- end;
- end;
-
- procedure TfTest.Button2Click(Sender: TObject);
- const
- Total = 5000000;
- var
- i: Integer;
- Id: Int64;
- ST, ET, Elapse: Cardinal;
- begin
- Screen.Cursor := crHourGlass;
- try
- ST := GetTickCount();
- for i := 1 to Total do
- begin
- Id := YitIdHelper.NextId();
- end;
- ET := GetTickCount();
- finally
- Screen.Cursor := crDefault;
- end;
- Elapse := ET - ST;
- mmo1.Lines.Add(Format('500ʱ䣺%d룬ƽÿ%d', [Elapse, Trunc((Total / Elapse) * 1000)]));
-
- end;
-
- procedure TfTest.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- YitIdHelper.Free;
- IdGeneratorOption.Free;
- end;
-
- procedure TfTest.FormCreate(Sender: TObject);
- begin
- IdGeneratorOption := TIdGeneratorOptions.Create;
- // Ĭϲ
- with IdGeneratorOption do
- begin
- Method := rgSelect.ItemIndex + 1;
- // BaseTime := DateTime.Now.AddYears(-10);
- WorkerId := 2;
-
- WorkerIdBitLength := 6;
- SeqBitLength := 6;
-
- MaxSeqNumber := 0;
- MinSeqNumber := 5;
-
- TopOverCostCount := 2000;
-
- DataCenterId := 0;
- DataCenterIdBitLength := 0;
-
- TimestampType := 0;
- end;
-
- // ʹ÷ʽװʹ
- YitIdHelper := TYitIdHelper.Create;
- YitIdHelper.SetIdGenerator(IdGeneratorOption);
- end;
-
- end.
|