Advertisement
Guest User

Untitled

a guest
Aug 14th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.05 KB | None | 0 0
  1. unit FormTest;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
  8.  
  9.   //the following units are not used in Snappy, them are provided for a base benchmark;
  10.   //you can delete them into your Snappy code
  11.   SynLZ, SynCommons, SynZip; //from Synopse mORMot Framework
  12.  
  13. type
  14.   TTest = class(TForm)
  15.     Compress: TButton;
  16.     Uncompress: TButton;
  17.     Memo1: TMemo;
  18.     cbbAlgo: TComboBox;
  19.     procedure CompressClick(Sender: TObject);
  20.     procedure UncompressClick(Sender: TObject);
  21.     procedure FormShow(Sender: TObject);
  22.   private
  23.     { Private declarations }
  24.   public
  25.     { Public declarations }
  26.   end;
  27.  
  28. var
  29.   Test: TTest;
  30.  
  31. implementation
  32.  
  33. {$R *.dfm}
  34.  
  35. uses Snappy;
  36.  
  37. const
  38.   COUNT = 1;
  39.   //FILE_TEST = 'ecommerce.json';
  40.   //FILE_TEST = 'partials.json';
  41.   //FILE_TEST = 'transactions.json';
  42.   FILE_TEST = 'DRAGONFLY1-bhshdd001ServAppStarterApi.log';
  43.  
  44. type
  45.   TAlgo = (alUndefined, alSnappy, alSynLZ, alZip1, alZip6);
  46. const
  47.   ALGO_NAME: array[TAlgo] of string = ('','Snappy','SynLZ', 'SynZip1', 'SynZip6');
  48.  
  49. procedure TTest.CompressClick(Sender: TObject); // I did a test with 1GB vhd file, works md5 100%
  50. var
  51.   env: psnappy_env;
  52.   StreamIN, StreamOUT: TMemoryStream;
  53.   inlen,outlen: NativeUInt;
  54.   timer: TPrecisionTimer;
  55.   s: string;
  56.   i: integer;
  57.   algo: TAlgo;
  58. begin
  59.   algo := TAlgo(cbbAlgo.ItemIndex + 1);
  60.   s := ALGO_NAME[algo];
  61.   StreamIN := TMemoryStream.Create;
  62.   StreamOUT := TMemoryStream.Create;
  63.   StreamIN.LoadFromFile(FILE_TEST);
  64.   inlen := StreamIN.Size;
  65.   if Memo1.Lines.Text = '' then
  66.     Memo1.Lines.Text := format({$IFDEF WIN64}'Win64'{$else}'Win32'{$endif}+
  67.       ' Processing %s = %s for %d times',[FILE_TEST,KB(inlen),COUNT]);
  68.   StreamOUT.Size := inlen;
  69.   try
  70.     outlen := 0;
  71.     sleep(100);
  72.     timer.Start;
  73.     for i := 1 to COUNT do
  74.       case Algo of
  75.        alSynLZ:
  76.          outlen := SynLZCompress1(StreamIN.Memory, StreamIN.Size, StreamOUT.Memory);
  77.        alSnappy: begin
  78.         New(env);
  79.       {$IFDEF WIN64}
  80.         snappy_init_env(env);
  81.         snappy_compress(env, StreamIN.Memory, StreamIN.Size, StreamOUT.Memory, @outlen);
  82.         snappy_free_env(env);
  83.       {$ENDIF}
  84.       {$IFDEF WIN32}
  85.         _snappy_init_env(env);
  86.         _snappy_compress(env, StreamIN.Memory, StreamIN.Size, StreamOUT.Memory, @outlen);
  87.         _snappy_free_env(env);
  88.       {$ENDIF}
  89.         Dispose(env);
  90.       end;
  91.       alZip1:
  92.         outlen := CompressMem(StreamIN.Memory, StreamOut.Memory, StreamIN.Size, StreamOut.Size, 1);
  93.       alZip6:
  94.         outlen := CompressMem(StreamIN.Memory, StreamOut.Memory, StreamIN.Size, StreamOut.Size, 6);
  95.       else
  96.         exit;
  97.       end;
  98.   Memo1.Lines.Add(format(' %s compress in %s, ratio=%d%%, %s/s',
  99.      [s, timer.Stop, 100-((100*outlen)div inlen), KB(timer.PerSec(inlen*COUNT))]));
  100.   StreamOut.Size := outlen;
  101.   StreamOUT.SaveToFile(FILE_TEST + '.' + s);
  102. finally
  103.   StreamIN.Free;
  104.   StreamOUT.Free;
  105. end;
  106. end;
  107.  
  108. procedure TTest.FormShow(Sender: TObject);
  109. var
  110.   algo: TAlgo;
  111. begin
  112.   for algo := succ(alUndefined) to High(algo) do
  113.     cbbAlgo.Items.Add(string(TrimLeftLowerCaseShort(GetEnumName(TypeInfo(TAlgo),ord(algo)))));
  114.   cbbAlgo.ItemIndex := 0;
  115. end;
  116.  
  117. procedure TTest.UncompressClick(Sender: TObject);
  118. var
  119.   env: psnappy_env;
  120.   StreamIN, StreamOUT: TMemoryStream;
  121.   outlen: NativeUInt;
  122.   timer: TPrecisionTimer;
  123.   s: string;
  124.   i: integer;
  125.   algo: TAlgo;
  126. begin
  127.   algo := TAlgo(cbbAlgo.ItemIndex + 1);
  128.   s := ALGO_NAME[algo];
  129.   StreamIN := TMemoryStream.Create;
  130.   StreamOUT := TMemoryStream.Create;
  131.   StreamIN.LoadFromFile(FILE_TEST + '.' + s);
  132.   case algo of
  133.     alSnappy:
  134.     {$IFDEF WIN64}
  135.       snappy_uncompressed_length(StreamIN.Memory, StreamIN.Size, @outlen);
  136.     {$ENDIF}
  137.     {$IFDEF WIN32}
  138.       _snappy_uncompressed_length(StreamIN.Memory, StreamIN.Size, @outlen);
  139.     {$ENDIF}
  140.     alSynLZ:
  141.       outlen := SynLZdecompressdestlen(StreamIN.Memory);
  142.     alZip1, alZip6:
  143.       outlen := FileSize(FILE_TEST);
  144.     else
  145.       exit;
  146.   end;
  147.   StreamOUT.Size := outlen;
  148.   sleep(100);
  149.   try
  150.     timer.Start;
  151.     for i := 1 to COUNT do
  152.     case algo of
  153.       alSynLZ:
  154.         outlen := SynLZDecompress1(StreamIN.Memory, StreamIN.Size, StreamOUT.Memory);
  155.       alSnappy: begin
  156.         New(env);
  157.         {$IFDEF WIN64}
  158.         snappy_init_env(env);
  159.         snappy_uncompress(StreamIN.Memory, StreamIN.Size, StreamOUT.Memory);
  160.         snappy_free_env(env);
  161.         {$ENDIF}
  162.         {$IFDEF WIN32}
  163.         _snappy_init_env(env);
  164.         _snappy_uncompress(StreamIN.Memory, StreamIN.Size, StreamOUT.Memory);
  165.         _snappy_free_env(env);
  166.         {$ENDIF}
  167.         Dispose(env);
  168.       end;
  169.       alZip1, alZip6:
  170.         UnCompressMem(StreamIN.Memory, Streamout.Memory, StreamIn.Size, StreamOut.Size);
  171.     end;
  172.     Memo1.Lines.Add(format(' %s uncompress in %s, %s/s', [s, timer.Stop, KB(timer.PerSec(outlen * COUNT))]));
  173.   finally
  174.     StreamIN.Free;
  175.     StreamOUT.SaveToFile(FILE_TEST + '2');
  176.     StreamOUT.Free;
  177.   end;
  178. end;
  179.  
  180. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement