33using System . IO ;
44using System . IO . Compression ;
55using System . Linq ;
6+ using System . Text ;
67
8+ using Force . Blazer . Algorithms ;
79using Force . Blazer . Algorithms . Crc32C ;
10+ using Force . Blazer . Benchmark . MessagesDto ;
811using Force . Blazer . Native ;
912
1013using ICSharpCode . SharpZipLib . BZip2 ;
1114using ICSharpCode . SharpZipLib . GZip ;
15+ using ICSharpCode . SharpZipLib . Zip . Compression ;
1216
1317using LZ4 ;
1418
@@ -20,9 +24,10 @@ public class Program
2024 {
2125 public static void Main ( )
2226 {
23- BenchCrc32C ( ) ;
27+ BenchPatternedCompression ( ) ;
28+ // BenchCrc32C();
2429 //BenchNoCompression();
25- BenchFile ( "Selesia Total" , @"..\..\..\TestFiles\Silesia\ztotal.tar" ) ;
30+ // BenchFile("Selesia Total", @"..\..\..\TestFiles\Silesia\ztotal.tar");
2631 // BenchFile("Log", @"..\..\..\TestFiles\Service.2016-05-01.log");
2732 // BenchFile("AdventureWorks (high compressible db)", @"..\..\..\TestFiles\AdventureWorks2012_Data.mdf");
2833 // BenchFile("enwiki8 (big text document)", @"..\..\..\TestFiles\enwik8");
@@ -108,9 +113,9 @@ private static void BenchData(string title, byte[] array)
108113 DoBench ( "Snappy " , array , x => new SnappyStream ( x , CompressionMode . Compress ) , x => new SnappyStream ( x , CompressionMode . Decompress ) ) ;
109114 DoBench ( "StdGZip " , array , x => new GZipStream ( x , CompressionMode . Compress ) , x => new GZipStream ( x , CompressionMode . Decompress ) ) ;
110115 // very slow for usual running
111- DoBench ( "BZip2 " , array , x => new BZip2OutputStream ( x ) , x => new BZip2InputStream ( x ) ) ;
112- DoBenchQuickLZ ( "QuickLZ/1" , 1 , array ) ;
113- DoBenchQuickLZ ( "QuickLZ/3" , 3 , array ) ;
116+ // DoBench("BZip2 ", array, x => new BZip2OutputStream(x), x => new BZip2InputStream(x));
117+ // DoBenchQuickLZ("QuickLZ/1", 1, array);
118+ // DoBenchQuickLZ("QuickLZ/3", 3, array);
114119 }
115120
116121 private static void DoBench ( string title , byte [ ] data , Func < Stream , Stream > createCompressionStream , Func < Stream , Stream > createDecompressionStream )
@@ -222,5 +227,52 @@ private static void DoBenchBlock(
222227 100.0 * comprArray . Length / data . Length ,
223228 1.0 * data . Length / comprArray . Length ) ;
224229 }
230+
231+ private static void BenchPatternedCompression ( )
232+ {
233+ var data = LogMessage . Generate ( 10000 ) ;
234+ var totalSize = data . Sum ( x => x . Length ) ;
235+
236+ var gzipSize = data . Sum ( x =>
237+ {
238+ var deflater = new Deflater ( ) ;
239+ deflater . SetInput ( x ) ;
240+ deflater . Finish ( ) ;
241+ var cnt = 0 ;
242+ while ( ! deflater . IsNeedingInput )
243+ cnt += deflater . Deflate ( new byte [ x . Length ] ) ;
244+ return cnt ;
245+ } ) ;
246+
247+ var quickLzSize = data . Sum ( x => QuickLZ . QuickLZ . compress ( x , 1 ) . Length ) ;
248+
249+ var blStreamIndependent = data . Sum ( x => StreamEncoder . CompressData ( x ) . Length ) ;
250+
251+ var ps = BlazerPatternedHelper . CreateStream ( ) ;
252+ ps . PreparePattern ( data [ 0 ] ) ;
253+
254+ var psbest = BlazerPatternedHelper . CreateStream ( ) ;
255+ psbest . PreparePattern ( LogMessage . GenerateBestPattern ( ) ) ;
256+ // var psh = BlazerPatternedHelper.CreateStreamHigh();
257+ // psh.PreparePattern(data[0]);
258+ // var pb = BlazerPatternedHelper.CreateBlock();
259+ // pb.PreparePattern(data[0]);
260+
261+ var blSPatterned = data . Sum ( x => ps . EncodeWithPattern ( x ) . Length ) ;
262+ var blSBestPatterned = data . Sum ( x => psbest . EncodeWithPattern ( x ) . Length ) ;
263+ // var blSHPatterned = data.Sum(x => psh.EncodeWithPattern(x).Length);
264+ // var blBPatterned = data.Sum(x => pb.EncodeWithPattern(x).Length);
265+
266+ Console . WriteLine ( Encoding . UTF8 . GetString ( data [ 0 ] ) ) ;
267+ Console . WriteLine ( ) ;
268+ Console . WriteLine ( "Total: {0}" , totalSize ) ;
269+ Console . WriteLine ( "GZip (Deflate): {0}\t {1:0.000}" , gzipSize , 100.0 * gzipSize / totalSize ) ;
270+ Console . WriteLine ( "QuickLZ: {0}\t {1:0.000}" , quickLzSize , 100.0 * quickLzSize / totalSize ) ;
271+ Console . WriteLine ( "Blazer Independent: {0}\t {1:0.000}" , blStreamIndependent , 100.0 * blStreamIndependent / totalSize ) ;
272+ Console . WriteLine ( "Blazer Pattern: {0}\t {1:0.000}" , blSPatterned , 100.0 * blSPatterned / totalSize ) ;
273+ Console . WriteLine ( "Blazer Pattern Best: {0}\t {1:0.000}" , blSBestPatterned , 100.0 * blSBestPatterned / totalSize ) ;
274+ // Console.WriteLine("Blazer Pattern SH: {0}\t{1:0.000}", blSHPatterned, 100.0 * blSHPatterned / totalSize);
275+ // Console.WriteLine("Blazer Pattern B: {0}\t{1:0.000}", blBPatterned, 100.0 * blBPatterned / totalSize);
276+ }
225277 }
226278}
0 commit comments