unit FormTest; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, //the following units are not used in Snappy, them are provided for a base benchmark; //you can delete them into your Snappy code SynLZ, SynCommons, SynZip; //from Synopse mORMot Framework type TTest = class(TForm) Compress: TButton; Uncompress: TButton; Memo1: TMemo; cbbAlgo: TComboBox; procedure CompressClick(Sender: TObject); procedure UncompressClick(Sender: TObject); procedure FormShow(Sender: TObject); private { Private declarations } public { Public declarations } end; var Test: TTest; implementation {$R *.dfm} uses Snappy; const COUNT = 1; //FILE_TEST = 'ecommerce.json'; //FILE_TEST = 'partials.json'; //FILE_TEST = 'transactions.json'; FILE_TEST = 'DRAGONFLY1-bhshdd001ServAppStarterApi.log'; type TAlgo = (alUndefined, alSnappy, alSynLZ, alZip1, alZip6); const ALGO_NAME: array[TAlgo] of string = ('','Snappy','SynLZ', 'SynZip1', 'SynZip6'); procedure TTest.CompressClick(Sender: TObject); // I did a test with 1GB vhd file, works md5 100% var env: psnappy_env; StreamIN, StreamOUT: TMemoryStream; inlen,outlen: NativeUInt; timer: TPrecisionTimer; s: string; i: integer; algo: TAlgo; begin algo := TAlgo(cbbAlgo.ItemIndex + 1); s := ALGO_NAME[algo]; StreamIN := TMemoryStream.Create; StreamOUT := TMemoryStream.Create; StreamIN.LoadFromFile(FILE_TEST); inlen := StreamIN.Size; if Memo1.Lines.Text = '' then Memo1.Lines.Text := format({$IFDEF WIN64}'Win64'{$else}'Win32'{$endif}+ ' Processing %s = %s for %d times',[FILE_TEST,KB(inlen),COUNT]); StreamOUT.Size := inlen; try outlen := 0; sleep(100); timer.Start; for i := 1 to COUNT do case Algo of alSynLZ: outlen := SynLZCompress1(StreamIN.Memory, StreamIN.Size, StreamOUT.Memory); alSnappy: begin New(env); {$IFDEF WIN64} snappy_init_env(env); snappy_compress(env, StreamIN.Memory, StreamIN.Size, StreamOUT.Memory, @outlen); snappy_free_env(env); {$ENDIF} {$IFDEF WIN32} _snappy_init_env(env); _snappy_compress(env, StreamIN.Memory, StreamIN.Size, StreamOUT.Memory, @outlen); _snappy_free_env(env); {$ENDIF} Dispose(env); end; alZip1: outlen := CompressMem(StreamIN.Memory, StreamOut.Memory, StreamIN.Size, StreamOut.Size, 1); alZip6: outlen := CompressMem(StreamIN.Memory, StreamOut.Memory, StreamIN.Size, StreamOut.Size, 6); else exit; end; Memo1.Lines.Add(format(' %s compress in %s, ratio=%d%%, %s/s', [s, timer.Stop, 100-((100*outlen)div inlen), KB(timer.PerSec(inlen*COUNT))])); StreamOut.Size := outlen; StreamOUT.SaveToFile(FILE_TEST + '.' + s); finally StreamIN.Free; StreamOUT.Free; end; end; procedure TTest.FormShow(Sender: TObject); var algo: TAlgo; begin for algo := succ(alUndefined) to High(algo) do cbbAlgo.Items.Add(string(TrimLeftLowerCaseShort(GetEnumName(TypeInfo(TAlgo),ord(algo))))); cbbAlgo.ItemIndex := 0; end; procedure TTest.UncompressClick(Sender: TObject); var env: psnappy_env; StreamIN, StreamOUT: TMemoryStream; outlen: NativeUInt; timer: TPrecisionTimer; s: string; i: integer; algo: TAlgo; begin algo := TAlgo(cbbAlgo.ItemIndex + 1); s := ALGO_NAME[algo]; StreamIN := TMemoryStream.Create; StreamOUT := TMemoryStream.Create; StreamIN.LoadFromFile(FILE_TEST + '.' + s); case algo of alSnappy: {$IFDEF WIN64} snappy_uncompressed_length(StreamIN.Memory, StreamIN.Size, @outlen); {$ENDIF} {$IFDEF WIN32} _snappy_uncompressed_length(StreamIN.Memory, StreamIN.Size, @outlen); {$ENDIF} alSynLZ: outlen := SynLZdecompressdestlen(StreamIN.Memory); alZip1, alZip6: outlen := FileSize(FILE_TEST); else exit; end; StreamOUT.Size := outlen; sleep(100); try timer.Start; for i := 1 to COUNT do case algo of alSynLZ: outlen := SynLZDecompress1(StreamIN.Memory, StreamIN.Size, StreamOUT.Memory); alSnappy: begin New(env); {$IFDEF WIN64} snappy_init_env(env); snappy_uncompress(StreamIN.Memory, StreamIN.Size, StreamOUT.Memory); snappy_free_env(env); {$ENDIF} {$IFDEF WIN32} _snappy_init_env(env); _snappy_uncompress(StreamIN.Memory, StreamIN.Size, StreamOUT.Memory); _snappy_free_env(env); {$ENDIF} Dispose(env); end; alZip1, alZip6: UnCompressMem(StreamIN.Memory, Streamout.Memory, StreamIn.Size, StreamOut.Size); end; Memo1.Lines.Add(format(' %s uncompress in %s, %s/s', [s, timer.Stop, KB(timer.PerSec(outlen * COUNT))])); finally StreamIN.Free; StreamOUT.SaveToFile(FILE_TEST + '2'); StreamOUT.Free; end; end; end.