11 internal abstract class LzmaBench
13 const UInt32 kAdditionalSize = (6 << 20);
14 const UInt32 kCompressedAdditionalSize = (1 << 10);
15 const UInt32 kMaxLzmaPropSize = 10;
17 class CRandomGenerator
21 public CRandomGenerator() { Init(); }
22 public void Init() { A1 = 362436069; A2 = 521288629; }
23 public UInt32 GetRnd()
26 ((A1 = 36969 * (A1 & 0xffff) + (A1 >> 16)) << 16) ^
27 ((A2 = 18000 * (A2 & 0xffff) + (A2 >> 16)));
31 class CBitRandomGenerator
33 CRandomGenerator RG = new CRandomGenerator();
41 public UInt32 GetRnd(int numBits)
44 if (NumBits > numBits)
46 result = Value & (((UInt32)1 << numBits) - 1);
52 result = (Value << numBits);
54 result |= Value & (((UInt32)1 << numBits) - 1);
56 NumBits = 32 - numBits;
61 class CBenchRandomGenerator
63 CBitRandomGenerator RG = new CBitRandomGenerator();
67 public UInt32 BufferSize;
68 public Byte[] Buffer = null;
70 public CBenchRandomGenerator() { }
72 public void Set(UInt32 bufferSize)
74 Buffer = new Byte[bufferSize];
76 BufferSize = bufferSize;
78 UInt32 GetRndBit() { return RG.GetRnd(1); }
79 UInt32 GetLogRandBits(int numBits)
81 UInt32 len = RG.GetRnd(numBits);
82 return RG.GetRnd((int)len);
87 return GetLogRandBits(4);
88 return (GetLogRandBits(4) << 10) | RG.GetRnd(10);
90 UInt32 GetLen1() { return RG.GetRnd(1 + (int)RG.GetRnd(2)); }
91 UInt32 GetLen2() { return RG.GetRnd(2 + (int)RG.GetRnd(2)); }
92 public void Generate()
96 while (Pos < BufferSize)
98 if (GetRndBit() == 0 || Pos < 1)
99 Buffer[Pos++] = (Byte)RG.GetRnd(8);
103 if (RG.GetRnd(3) == 0)
113 for (UInt32 i = 0; i < len && Pos < BufferSize; i++, Pos++)
114 Buffer[Pos] = Buffer[Pos - Rep0];
120 class CrcOutStream : System.IO.Stream
122 public CRC CRC = new CRC();
123 public void Init() { CRC.Init(); }
124 public UInt32 GetDigest() { return CRC.GetDigest(); }
126 public override bool CanRead { get { return false; } }
127 public override bool CanSeek { get { return false; } }
128 public override bool CanWrite { get { return true; } }
129 public override Int64 Length { get { return 0; } }
130 public override Int64 Position { get { return 0; } set { } }
131 public override void Flush() { }
132 public override long Seek(long offset, SeekOrigin origin) { return 0; }
133 public override void SetLength(long value) { }
134 public override int Read(byte[] buffer, int offset, int count) { return 0; }
136 public override void WriteByte(byte b)
140 public override void Write(byte[] buffer, int offset, int count)
142 CRC.Update(buffer, (uint)offset, (uint)count);
146 class CProgressInfo : ICodeProgress
148 public Int64 ApprovedStart;
150 public System.DateTime Time;
151 public void Init() { InSize = 0; }
152 public void SetProgress(Int64 inSize, Int64 outSize)
154 if (inSize >= ApprovedStart && InSize == 0)
156 Time = DateTime.UtcNow;
161 const int kSubBits = 8;
163 static UInt32 GetLogSize(UInt32 size)
165 for (int i = kSubBits; i < 32; i++)
166 for (UInt32 j = 0; j < (1 << kSubBits); j++)
167 if (size <= (((UInt32)1) << i) + (j << (i - kSubBits)))
168 return (UInt32)(i << kSubBits) + j;
169 return (32 << kSubBits);
172 static UInt64 MyMultDiv64(UInt64 value, UInt64 elapsedTime)
174 UInt64 freq = TimeSpan.TicksPerSecond;
175 UInt64 elTime = elapsedTime;
176 while (freq > 1000000)
183 return value * freq / elTime;
186 static UInt64 GetCompressRating(UInt32 dictionarySize, UInt64 elapsedTime, UInt64 size)
188 UInt64 t = GetLogSize(dictionarySize) - (18 << kSubBits);
189 UInt64 numCommandsForOne = 1060 + ((t * t * 10) >> (2 * kSubBits));
190 UInt64 numCommands = (UInt64)(size) * numCommandsForOne;
191 return MyMultDiv64(numCommands, elapsedTime);
194 static UInt64 GetDecompressRating(UInt64 elapsedTime, UInt64 outSize, UInt64 inSize)
196 UInt64 numCommands = inSize * 220 + outSize * 20;
197 return MyMultDiv64(numCommands, elapsedTime);
200 static UInt64 GetTotalRating(
201 UInt32 dictionarySize,
202 UInt64 elapsedTimeEn, UInt64 sizeEn,
203 UInt64 elapsedTimeDe,
204 UInt64 inSizeDe, UInt64 outSizeDe)
206 return (GetCompressRating(dictionarySize, elapsedTimeEn, sizeEn) +
207 GetDecompressRating(elapsedTimeDe, inSizeDe, outSizeDe)) / 2;
210 static void PrintValue(UInt64 v)
212 string s = v.ToString();
213 for (int i = 0; i + s.Length < 6; i++)
214 System.Console.Write(" ");
215 System.Console.Write(s);
218 static void PrintRating(UInt64 rating)
220 PrintValue(rating / 1000000);
221 System.Console.Write(" MIPS");
224 static void PrintResults(
225 UInt32 dictionarySize,
228 bool decompressMode, UInt64 secondSize)
230 UInt64 speed = MyMultDiv64(size, elapsedTime);
231 PrintValue(speed / 1024);
232 System.Console.Write(" KB/s ");
235 rating = GetDecompressRating(elapsedTime, size, secondSize);
237 rating = GetCompressRating(dictionarySize, elapsedTime, size);
241 static public int LzmaBenchmark(Int32 numIterations, UInt32 dictionarySize)
243 if (numIterations <= 0)
245 if (dictionarySize < (1 << 18))
247 System.Console.WriteLine("\nError: dictionary size for benchmark must be >= 19 (512 KB)");
250 System.Console.Write("\n Compressing Decompressing\n\n");
252 Compression.LZMA.Encoder encoder = new Compression.LZMA.Encoder();
253 Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder();
256 CoderPropID[] propIDs =
258 CoderPropID.DictionarySize,
260 object[] properties =
262 (Int32)(dictionarySize),
265 UInt32 kBufferSize = dictionarySize + kAdditionalSize;
266 UInt32 kCompressedBufferSize = (kBufferSize / 2) + kCompressedAdditionalSize;
268 encoder.SetCoderProperties(propIDs, properties);
269 System.IO.MemoryStream propStream = new System.IO.MemoryStream();
270 encoder.WriteCoderProperties(propStream);
271 byte[] propArray = propStream.ToArray();
273 CBenchRandomGenerator rg = new CBenchRandomGenerator();
279 crc.Update(rg.Buffer, 0, rg.BufferSize);
281 CProgressInfo progressInfo = new CProgressInfo();
282 progressInfo.ApprovedStart = dictionarySize;
284 UInt64 totalBenchSize = 0;
285 UInt64 totalEncodeTime = 0;
286 UInt64 totalDecodeTime = 0;
287 UInt64 totalCompressedSize = 0;
289 MemoryStream inStream = new MemoryStream(rg.Buffer, 0, (int)rg.BufferSize);
290 MemoryStream compressedStream = new MemoryStream((int)kCompressedBufferSize);
291 CrcOutStream crcOutStream = new CrcOutStream();
292 for (Int32 i = 0; i < numIterations; i++)
295 inStream.Seek(0, SeekOrigin.Begin);
296 compressedStream.Seek(0, SeekOrigin.Begin);
297 encoder.Code(inStream, compressedStream, -1, -1, progressInfo);
298 TimeSpan sp2 = DateTime.UtcNow - progressInfo.Time;
299 UInt64 encodeTime = (UInt64)sp2.Ticks;
301 long compressedSize = compressedStream.Position;
302 if (progressInfo.InSize == 0)
303 throw (new Exception("Internal ERROR 1282"));
305 UInt64 decodeTime = 0;
306 for (int j = 0; j < 2; j++)
308 compressedStream.Seek(0, SeekOrigin.Begin);
311 decoder.SetDecoderProperties(propArray);
312 UInt64 outSize = kBufferSize;
313 System.DateTime startTime = DateTime.UtcNow;
314 decoder.Code(compressedStream, crcOutStream, 0, (Int64)outSize, null);
315 TimeSpan sp = (DateTime.UtcNow - startTime);
316 decodeTime = (ulong)sp.Ticks;
317 if (crcOutStream.GetDigest() != crc.GetDigest())
318 throw (new Exception("CRC Error"));
320 UInt64 benchSize = kBufferSize - (UInt64)progressInfo.InSize;
321 PrintResults(dictionarySize, encodeTime, benchSize, false, 0);
322 System.Console.Write(" ");
323 PrintResults(dictionarySize, decodeTime, kBufferSize, true, (ulong)compressedSize);
324 System.Console.WriteLine();
326 totalBenchSize += benchSize;
327 totalEncodeTime += encodeTime;
328 totalDecodeTime += decodeTime;
329 totalCompressedSize += (ulong)compressedSize;
331 System.Console.WriteLine("---------------------------------------------------");
332 PrintResults(dictionarySize, totalEncodeTime, totalBenchSize, false, 0);
333 System.Console.Write(" ");
334 PrintResults(dictionarySize, totalDecodeTime,
335 kBufferSize * (UInt64)numIterations, true, totalCompressedSize);
336 System.Console.WriteLine(" Average");