Debian Codegraph @

plugins/wcx/zip/src/lzma/ULZMABench.pas

raw
1 unit ULZMABench;
2
3 {$IFDEF FPC}
4 {$MODE Delphi}
5 {$ENDIF}
6
7 interface
8
9 uses Classes,UCRC,ULZMACommon,windows,ULZMAEncoder,ULZMADecoder;
10
11 type TLZMABench=class
12 public
13 function GetLogSize(const size:integer):integer;
14 function MyMultDiv64(const value, elapsedTime:int64):int64;
15 function GetCompressRating(const dictionarySize:integer;const elapsedTime,size:int64):int64;
16 function GetDecompressRating(const elapsedTime:int64;const outSize,inSize:int64):int64;
17 function GetTotalRating(const dictionarySize:int64;const elapsedTimeEn, sizeEn, elapsedTimeDe, inSizeDe, outSizeDe:int64):int64;
18 procedure PrintValue(const v:int64);
19 procedure PrintRating(const rating:int64);
20 procedure PrintResults(const dictionarySize:integer;const elapsedTime, size:int64;const decompressMode:boolean;const secondSize:int64);
21 function LzmaBenchmark(const numIterations, dictionarySize:integer):integer;
22 end;
23
24 TLZMARandomGenerator=class
25 public
26 A1:integer;
27 A2:integer;
28 constructor Create;
29 procedure Init;
30 function GetRnd:integer;
31 end;
32
33 TLZMABenchBitRandomGenerator=class
34 public
35 RG:TLZMARandomGenerator;
36 Value:integer;
37 NumBits:integer;
38 constructor Create;
39 destructor Destroy;override;
40 procedure Init;
41 function GetRnd(numBits:integer):integer;
42 end;
43
44 TLZMABenchRandomGenerator=class
45 public
46 RG:TLZMABenchBitRandomGenerator;
47 Pos:integer;
48 Rep0:integer;
49 Buffer:array of byte;
50 BufferSize:integer;
51 constructor Create;
52 destructor Destroy;override;
53 procedure _Set(const bufferSize:integer);
54 function GetRndBit:integer;
55 function GetLogRandBits(const numBits:integer):integer;
56 function GetOffset:integer;
57 function GetLen1:integer;
58 function GetLen2:integer;
59 procedure Generate;
60 end;
61
62 TCRCStream=class(TStream)
63 public
64 CRC:TCRC;
65 constructor Create;
66 destructor Destroy;override;
67 procedure Init;
68 function GetDigest:integer;
69 function Write(const Buffer; Count: Longint): Longint;override;
70 end;
71
72 TByteArray=array of byte;
73 PByteArray=^TByteArray;
74
75 TMyOutputStream=class(TStream)
76 public
77 _buffer:PByteArray;
78 _size:integer;
79 _pos:integer;
80 constructor Create(const buffer:PByteArray);
81 procedure Reset;
82 function Write(const Buffer; Count: Longint): Longint;override;
83 function Size:integer;
84 end;
85
86 TMyInputStream=class(TStream)
87 public
88 _buffer:PByteArray;
89 _size:integer;
90 _pos:integer;
91 constructor Create(const buffer:PByteArray;const size:integer);
92 procedure Reset;
93 function Read(var Buffer; Count: Longint): Longint;override;
94 end;
95
96 TLZMAProgressInfo=class
97 public
98 ApprovedStart:int64;
99 InSize:int64;
100 Time:cardinal;
101 procedure Init;
102 procedure OnProgress(const Action:TLZMAProgressAction;const Value:int64);
103 end;
104
105
106 implementation
107
108 uses SysUtils;
109
110 const kAdditionalSize = (1 shl 21);
111 kCompressedAdditionalSize = (1 shl 10);
112 kSubBits = 8;
113
114 constructor TLZMARandomGenerator.Create;
115 begin
116 Init;
117 end;
118
119 procedure TLZMARandomGenerator.Init;
120 begin
121 A1 := 362436069;
122 A2 := 521288629;
123 end;
124
125 function TLZMARandomGenerator.GetRnd:integer;
126 begin
127 A1 := 36969 * (A1 and $ffff) + (A1 shr 16);
128 A2 := 18000 * (A2 and $ffff) + (A2 shr 16);
129 result:=(A1 shl 16) xor (A2);
130 end;
131
132 constructor TLZMABenchBitRandomGenerator.Create;
133 begin
134 RG:=TLZMARandomGenerator.Create;
135 end;
136
137 destructor TLZMABenchBitRandomGenerator.Destroy;
138 begin
139 RG.Free;
140 end;
141
142 procedure TLZMABenchBitRandomGenerator.Init;
143 begin
144 Value := 0;
145 NumBits := 0;
146 end;
147
148 function TLZMABenchBitRandomGenerator.GetRnd(numBits:integer):integer;
149 begin
150 if self.NumBits > numBits then begin
151 result := Value and ((1 shl numBits) - 1);
152 Value := Value shr numBits;
153 self.NumBits := self.NumBits - numBits;
154 exit;
155 end;
156 numBits := numBits - self.NumBits;
157 result := (Value shl numBits);
158 Value := RG.GetRnd;
159 result := result or (Value and ((1 shl numBits) - 1));
160 Value := value shr numBits;
161 self.NumBits := 32 - numBits;
162 end;
163
164 constructor TLZMABenchRandomGenerator.Create;
165 begin
166 RG:=TLZMABenchBitRandomGenerator.Create;
167 end;
168
169 destructor TLZMABenchRandomGenerator.Destroy;
170 begin
171 RG.free;
172 end;
173
174 procedure TLZMABenchRandomGenerator._Set(const bufferSize:integer);
175 begin
176 setlength(Buffer,bufferSize);
177 Pos := 0;
178 self.BufferSize := bufferSize;
179 end;
180
181 function TLZMABenchRandomGenerator.GetLogRandBits(const numBits:integer):integer;
182 var len:integer;
183 begin
184 len := RG.GetRnd(numBits);
185 result:=RG.GetRnd(len);
186 end;
187
188 function TLZMABenchRandomGenerator.GetRndBit:integer;
189 begin
190 result:=RG.GetRnd(1);
191 end;
192
193 function TLZMABenchRandomGenerator.GetOffset:integer;
194 begin
195 if GetRndBit = 0 then
196 result:=GetLogRandBits(4)
197 else result:=(GetLogRandBits(4) shl 10) or RG.GetRnd(10);
198 end;
199
200 function TLZMABenchRandomGenerator.GetLen1:integer;
201 begin
202 result:=RG.GetRnd(1 + RG.GetRnd(2));
203 end;
204
205 function TLZMABenchRandomGenerator.GetLen2:integer;
206 begin
207 result:=RG.GetRnd(2 + RG.GetRnd(2));
208 end;
209
210 procedure TLZMABenchRandomGenerator.Generate;
211 var len,i:integer;
212 begin
213 RG.Init;
214 Rep0 := 1;
215 while Pos < BufferSize do begin
216 if (GetRndBit = 0) or (Pos < 1) then begin
217 Buffer[Pos] := RG.GetRnd(8);
218 inc(pos);
219 end else begin
220 if RG.GetRnd(3) = 0 then
221 len := 1 + GetLen1
222 else begin
223 repeat
224 Rep0 := GetOffset;
225 until not (Rep0 >= Pos);
226 inc(Rep0);
227 len := 2 + GetLen2;
228 end;
229 i:=0;
230 while (i < len) and (Pos < BufferSize) do begin
231 Buffer[Pos] := Buffer[Pos - Rep0];
232 inc(i);
233 inc(pos);
234 end;
235 end;
236 end;
237 end;
238
239 constructor TCRCStream.Create;
240 begin
241 CRC:=TCRC.Create;
242 end;
243
244 destructor TCRCStream.Destroy;
245 begin
246 CRC.Free;
247 end;
248
249 procedure TCRCStream.Init;
250 begin
251 CRC.Init;
252 end;
253
254 function TCRCStream.GetDigest:integer;
255 begin
256 result:=CRC.GetDigest;
257 end;
258
259 function TCRCStream.Write(const Buffer; Count: Longint): Longint;
260 var p:^byte;
261 i:integer;
262 begin
263 p:=@buffer;
264 for i:=0 to count -1 do begin
265 CRC.UpdateByte(p^);
266 inc(p);
267 end;
268 result:=count;
269 end;
270
271 constructor TMyOutputStream.Create(const buffer:PByteArray);
272 begin
273 _buffer:=buffer;
274 _size:=length(buffer^);
275 end;
276
277 procedure TMyOutputStream.Reset;
278 begin
279 _pos:=0;
280 end;
281
282 function TMyOutputStream.Write(const Buffer; Count: Longint): Longint;
283 begin
284 if _pos+count>=_size then
285 raise Exception.Create('Error');
286 move(buffer,_buffer^[_pos],count);
287 _pos:=_pos+count;
288 result:=count;
289 end;
290
291 function TMyOutputStream.Size:integer;
292 begin
293 result:=_pos;
294 end;
295
296 constructor TMyInputStream.Create(const buffer:PByteArray;const size:integer);
297 begin
298 _buffer:=buffer;
299 _size:=size;
300 end;
301
302 procedure TMyInputStream.Reset;
303 begin
304 _pos:=0;
305 end;
306
307 function TMyInputStream.Read(var Buffer; Count: Longint): Longint;
308 var b:int64;
309 begin
310 try
311 b:=_size-_pos;
312 if b>count then b:=count;
313 result:=b;
314 move(_buffer^[_pos],buffer,b);
315 _pos:=_pos+b;
316 except writeln('inread error');
317 end;
318 end;
319
320 procedure TLZMAProgressInfo.Init;
321 begin
322 InSize:=0;
323 end;
324
325 procedure TLZMAProgressInfo.OnProgress(const Action:TLZMAProgressAction;const Value:int64);
326 begin
327 if Action=LPAMax then exit;
328 if (value >= ApprovedStart) and (InSize = 0) then begin
329 Time := GetTickCount;
330 InSize := value;
331 end;
332 end;
333
334 function TLZMABench.GetLogSize(const size:integer):integer;
335 var i,j:integer;
336 begin
337 for i := kSubBits to 31 do
338 for j := 0 to 1 shl kSubBits -1 do
339 if (size <= (1 shl i) + (j shl (i - kSubBits))) then begin
340 result:=(i shl kSubBits) + j;
341 exit;
342 end;
343 result:=32 shl kSubBits;
344 end;
345
346 function TLZMABench.MyMultDiv64(const value, elapsedTime:int64):int64;
347 var freq,elTime:int64;
348 begin
349 freq := 1000; // ms
350 elTime := elapsedTime;
351 while freq > 1000000 do begin
352 freq := freq shr 1;
353 elTime :=elTime shr 1;
354 end;
355 if elTime = 0 then
356 elTime := 1;
357 result:=value * freq div elTime;
358 end;
359
360 function TLZMABench.GetCompressRating(const dictionarySize:integer;const elapsedTime,size:int64):int64;
361 var t,numCommandsForOne,numCommands:int64;
362 begin
363 t := GetLogSize(dictionarySize) - (18 shl kSubBits);
364 numCommandsForOne := 1060 + ((t * t * 10) shr (2 * kSubBits));
365 numCommands := size * numCommandsForOne;
366 result:=MyMultDiv64(numCommands, elapsedTime);
367 end;
368
369 function TLZMABench.GetDecompressRating(const elapsedTime:int64;const outSize,inSize:int64):int64;
370 var numCommands:int64;
371 begin
372 numCommands := inSize * 220 + outSize * 20;
373 result:=MyMultDiv64(numCommands, elapsedTime);
374 end;
375
376 function TLZMABench.GetTotalRating(const dictionarySize:int64;const elapsedTimeEn, sizeEn, elapsedTimeDe, inSizeDe, outSizeDe:int64):int64;
377 begin
378 result:=(GetCompressRating(dictionarySize, elapsedTimeEn, sizeEn) +
379 GetDecompressRating(elapsedTimeDe, inSizeDe, outSizeDe)) div 2;
380 end;
381
382 procedure TLZMABench.PrintValue(const v:int64);
383 var s:string;
384 i:integer;
385 begin
386 s:=inttostr(v);
387 i:=0;
388 while i+length(s)<6 do begin
389 write(' ');
390 inc(i);
391 end;
392 write(s);
393 end;
394
395 procedure TLZMABench.PrintRating(const rating:int64);
396 begin
397 PrintValue(rating div 1000000);
398 write(' MIPS');
399 end;
400
401 procedure TLZMABench.PrintResults(const dictionarySize:integer;const elapsedTime, size:int64;const decompressMode:boolean;const secondSize:int64);
402 var speed:int64;
403 rating:int64;
404 begin
405 speed := MyMultDiv64(size, elapsedTime);
406 PrintValue(speed div 1024);
407 write(' KB/s ');
408 if decompressMode then
409 rating := GetDecompressRating(elapsedTime, size, secondSize)
410 else rating := GetCompressRating(dictionarySize, elapsedTime, size);
411 PrintRating(rating);
412 end;
413
414 function TLZMABench.LzmaBenchmark(const numIterations, dictionarySize:integer):integer;
415 var encoder:TLZMAEncoder;
416 decoder:TLZMADecoder;
417 kBufferSize,kCompressedBufferSize:integer;
418 propStream:TMemoryStream;
419 proparray:array of byte;
420 rg:TLZMABenchRandomGenerator;
421 crc:TCRC;
422 progressInfo:TLZMAProgressInfo;
423 totalBenchSize,totalEncodeTime,totalDecodeTime,totalCompressedSize:int64;
424 inStream:TMyInputStream;
425 compressedBuffer:array of byte;
426 compressedStream:TMyOutputStream;
427 CrcOutStream:TCRCStream;
428 inputCompressedStream:TMyInputStream;
429 compressedSize,i,j:integer;
430 encodeTime,decodeTime:cardinal;
431 outSize,startTime,benchSize:int64;
432 begin
433 if numIterations <= 0 then begin
434 result:=0;
435 exit;
436 end;
437 if dictionarySize < (1 shl 18) then begin
438 writeln(#10'Error: dictionary size for benchmark must be >= 18 (256 KB)');
439 result:=1;
440 exit;
441 end;
442 write(#10' Compressing Decompressing'#10#10);
443
444 encoder := TLZMAEncoder.Create;
445 decoder := TLZMADecoder.Create;
446
447 if not encoder.SetDictionarySize(dictionarySize) then
448 raise Exception.Create('Incorrect dictionary size');
449
450 kBufferSize := dictionarySize + kAdditionalSize;
451 kCompressedBufferSize := (kBufferSize div 2) + kCompressedAdditionalSize;
452
453 propstream:=TMemoryStream.Create;
454 encoder.WriteCoderProperties(propStream);
455 setlength(proparray,propstream.size);
456 propstream.Position:=0;
457 propstream.Read(propArray[0],propstream.Size);
458 decoder.SetDecoderProperties(propArray);
459
460 rg := TLZMABenchRandomGenerator.Create;
461
462 rg._Set(kBufferSize);
463 rg.Generate;
464 crc := TCRC.Create;
465 crc.Init;
466 crc.Update(rg.Buffer[0], 0, rg.BufferSize);
467
468 progressInfo := TLZMAProgressInfo.Create;
469 progressInfo.ApprovedStart := dictionarySize;
470
471 totalBenchSize := 0;
472 totalEncodeTime := 0;
473 totalDecodeTime := 0;
474 totalCompressedSize := 0;
475
476 inStream := TMyInputStream.Create(@(rg.Buffer), rg.BufferSize);
477
478 setlength(compressedBuffer,kCompressedBufferSize);
479 compressedStream := TMyOutputStream.Create(@compressedBuffer);
480 crcOutStream :=TCRCStream.Create;
481 inputCompressedStream := nil;
482 compressedSize := 0;
483 for i := 0 to numIterations -1 do begin
484 progressInfo.Init;
485 inStream.reset;
486 compressedStream.reset;
487 encoder.OnProgress:=progressInfo.OnProgress;
488 encoder.Code(inStream, compressedStream, rg.BufferSize, -1);
489 encodeTime := GetTickCount - progressInfo.Time;
490
491 if i = 0 then begin
492 compressedSize := compressedStream.size;
493 inputCompressedStream := TMyInputStream.Create(@compressedBuffer, compressedSize);
494 end else if compressedSize <> compressedStream.size then
495 raise Exception.Create('Encoding error');
496
497 if progressInfo.InSize = 0 then
498 raise Exception.Create('Internal ERROR 1282');
499
500 decodeTime := 0;
501 for j := 0 to 1 do begin
502 inputCompressedStream.reset;
503 crcOutStream.Init;
504
505 outSize := kBufferSize;
506 startTime := GetTickCount;
507 if not decoder.Code(inputCompressedStream, crcOutStream, outSize) then
508 raise Exception.Create('Decoding Error');
509 decodeTime := GetTickCount - startTime;
510 if crcOutStream.GetDigest <> crc.GetDigest then
511 raise Exception.Create('CRC Error');
512 end;
513 benchSize := kBufferSize - progressInfo.InSize;
514 PrintResults(dictionarySize, encodeTime, benchSize, false, 0);
515 write(' ');
516 PrintResults(dictionarySize, decodeTime, kBufferSize, true, compressedSize);
517 writeln('');
518
519 totalBenchSize := totalBenchSize + benchSize;
520 totalEncodeTime := totalEncodeTime + encodeTime;
521 totalDecodeTime := totalDecodeTime + decodeTime;
522 totalCompressedSize := totalCompressedSize + compressedSize;
523 end;
524 writeln('---------------------------------------------------');
525 PrintResults(dictionarySize, totalEncodeTime, totalBenchSize, false, 0);
526 write(' ');
527 PrintResults(dictionarySize, totalDecodeTime, kBufferSize * numIterations, true, totalCompressedSize);
528 writeln(' Average');
529 result:=0;
530 end;
531
532 end.