summaryrefslogtreecommitdiffstats
path: root/CS/7zip/Compress/LzmaAlone/LzmaBench.cs
blob: 6a1ffe24602086dd94adf7cf85122d5a1ba8305c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// LzmaBench.cs

using System;
using System.IO;

namespace SevenZip
{
	/// <summary>
	/// LZMA Benchmark
	/// </summary>
	internal abstract class LzmaBench
	{
		const UInt32 kAdditionalSize = (6 << 20);
		const UInt32 kCompressedAdditionalSize = (1 << 10);
		const UInt32 kMaxLzmaPropSize = 10;

		class CRandomGenerator
		{
			UInt32 A1;
			UInt32 A2;
			public CRandomGenerator() { Init(); }
			public void Init() { A1 = 362436069; A2 = 521288629; }
			public UInt32 GetRnd()
			{
				return
					((A1 = 36969 * (A1 & 0xffff) + (A1 >> 16)) << 16) ^
					((A2 = 18000 * (A2 & 0xffff) + (A2 >> 16)));
			}
		};

		class CBitRandomGenerator
		{
			CRandomGenerator RG = new CRandomGenerator();
			UInt32 Value;
			int NumBits;
			public void Init()
			{
				Value = 0;
				NumBits = 0;
			}
			public UInt32 GetRnd(int numBits)
			{
				UInt32 result;
				if (NumBits > numBits)
				{
					result = Value & (((UInt32)1 << numBits) - 1);
					Value >>= numBits;
					NumBits -= numBits;
					return result;
				}
				numBits -= NumBits;
				result = (Value << numBits);
				Value = RG.GetRnd();
				result |= Value & (((UInt32)1 << numBits) - 1);
				Value >>= numBits;
				NumBits = 32 - numBits;
				return result;
			}
		};

		class CBenchRandomGenerator
		{
			CBitRandomGenerator RG = new CBitRandomGenerator();
			UInt32 Pos;
			UInt32 Rep0;
			
			public UInt32 BufferSize;
			public Byte[] Buffer = null;

			public CBenchRandomGenerator() { }

			public void Set(UInt32 bufferSize)
			{
				Buffer = new Byte[bufferSize];
				Pos = 0;
				BufferSize = bufferSize;
			}
			UInt32 GetRndBit() { return RG.GetRnd(1); }
			UInt32 GetLogRandBits(int numBits)
			{
				UInt32 len = RG.GetRnd(numBits);
				return RG.GetRnd((int)len);
			}
			UInt32 GetOffset()
			{
				if (GetRndBit() == 0)
					return GetLogRandBits(4);
				return (GetLogRandBits(4) << 10) | RG.GetRnd(10);
			}
			UInt32 GetLen1() { return RG.GetRnd(1 + (int)RG.GetRnd(2)); }
			UInt32 GetLen2() { return RG.GetRnd(2 + (int)RG.GetRnd(2)); }
			public void Generate()
			{
				RG.Init();
				Rep0 = 1;
				while (Pos < BufferSize)
				{
					if (GetRndBit() == 0 || Pos < 1)
						Buffer[Pos++] = (Byte)RG.GetRnd(8);
					else
					{
						UInt32 len;
						if (RG.GetRnd(3) == 0)
							len = 1 + GetLen1();
						else
						{
							do
								Rep0 = GetOffset();
							while (Rep0 >= Pos);
							Rep0++;
							len = 2 + GetLen2();
						}
						for (UInt32 i = 0; i < len && Pos < BufferSize; i++, Pos++)
							Buffer[Pos] = Buffer[Pos - Rep0];
					}
				}
			}
		};

		class CrcOutStream : System.IO.Stream
		{
			public CRC CRC = new CRC();
			public void Init() { CRC.Init(); }
			public UInt32 GetDigest() { return CRC.GetDigest(); }

			public override bool CanRead { get { return false; } }
			public override bool CanSeek { get { return false; } }
			public override bool CanWrite { get { return true; } }
			public override Int64 Length { get { return 0; } }
			public override Int64 Position { get { return 0; } set { } }
			public override void Flush() { }
			public override long Seek(long offset, SeekOrigin origin) { return 0; }
			public override void SetLength(long value) { }
			public override int Read(byte[] buffer, int offset, int count) { return 0; }

			public override void WriteByte(byte b)
			{
				CRC.UpdateByte(b);
			}
			public override void Write(byte[] buffer, int offset, int count)
			{
				CRC.Update(buffer, (uint)offset, (uint)count);
			}
		};

		class CProgressInfo : ICodeProgress
		{
			public Int64 ApprovedStart;
			public Int64 InSize;
			public System.DateTime Time;
			public void Init() { InSize = 0; }
			public void SetProgress(Int64 inSize, Int64 outSize)
			{
				if (inSize >= ApprovedStart && InSize == 0)
				{
					Time = DateTime.UtcNow;
					InSize = inSize;
				}
			}
		}
		const int kSubBits = 8;

		static UInt32 GetLogSize(UInt32 size)
		{
			for (int i = kSubBits; i < 32; i++)
				for (UInt32 j = 0; j < (1 << kSubBits); j++)
					if (size <= (((UInt32)1) << i) + (j << (i - kSubBits)))
						return (UInt32)(i << kSubBits) + j;
			return (32 << kSubBits);
		}

		static UInt64 MyMultDiv64(UInt64 value, UInt64 elapsedTime)
		{
			UInt64 freq = TimeSpan.TicksPerSecond;
			UInt64 elTime = elapsedTime;
			while (freq > 1000000)
			{
				freq >>= 1;
				elTime >>= 1;
			}
			if (elTime == 0)
				elTime = 1;
			return value * freq / elTime;
		}

		static UInt64 GetCompressRating(UInt32 dictionarySize, UInt64 elapsedTime, UInt64 size)
		{
			UInt64 t = GetLogSize(dictionarySize) - (18 << kSubBits);
			UInt64 numCommandsForOne = 1060 + ((t * t * 10) >> (2 * kSubBits));
			UInt64 numCommands = (UInt64)(size) * numCommandsForOne;
			return MyMultDiv64(numCommands, elapsedTime);
		}

		static UInt64 GetDecompressRating(UInt64 elapsedTime, UInt64 outSize, UInt64 inSize)
		{
			UInt64 numCommands = inSize * 220 + outSize * 20;
			return MyMultDiv64(numCommands, elapsedTime);
		}

		static UInt64 GetTotalRating(
			UInt32 dictionarySize,
			UInt64 elapsedTimeEn, UInt64 sizeEn,
			UInt64 elapsedTimeDe,
			UInt64 inSizeDe, UInt64 outSizeDe)
		{
			return (GetCompressRating(dictionarySize, elapsedTimeEn, sizeEn) +
				GetDecompressRating(elapsedTimeDe, inSizeDe, outSizeDe)) / 2;
		}

		static void PrintValue(UInt64 v)
		{
			string s = v.ToString();
			for (int i = 0; i + s.Length < 6; i++)
				System.Console.Write(" ");
			System.Console.Write(s);
		}

		static void PrintRating(UInt64 rating)
		{
			PrintValue(rating / 1000000);
			System.Console.Write(" MIPS");
		}

		static void PrintResults(
			UInt32 dictionarySize,
			UInt64 elapsedTime,
			UInt64 size,
			bool decompressMode, UInt64 secondSize)
		{
			UInt64 speed = MyMultDiv64(size, elapsedTime);
			PrintValue(speed / 1024);
			System.Console.Write(" KB/s  ");
			UInt64 rating;
			if (decompressMode)
				rating = GetDecompressRating(elapsedTime, size, secondSize);
			else
				rating = GetCompressRating(dictionarySize, elapsedTime, size);
			PrintRating(rating);
		}

		static public int LzmaBenchmark(Int32 numIterations, UInt32 dictionarySize)
		{
			if (numIterations <= 0)
				return 0;
			if (dictionarySize < (1 << 18))
			{
				System.Console.WriteLine("\nError: dictionary size for benchmark must be >= 19 (512 KB)");
				return 1;
			}
			System.Console.Write("\n       Compressing                Decompressing\n\n");

			Compression.LZMA.Encoder encoder = new Compression.LZMA.Encoder();
			Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder();


			CoderPropID[] propIDs = 
			{ 
				CoderPropID.DictionarySize,
			};
			object[] properties = 
			{
				(Int32)(dictionarySize),
			};

			UInt32 kBufferSize = dictionarySize + kAdditionalSize;
			UInt32 kCompressedBufferSize = (kBufferSize / 2) + kCompressedAdditionalSize;

			encoder.SetCoderProperties(propIDs, properties);
			System.IO.MemoryStream propStream = new System.IO.MemoryStream();
			encoder.WriteCoderProperties(propStream);
			byte[] propArray = propStream.ToArray();

			CBenchRandomGenerator rg = new CBenchRandomGenerator();

			rg.Set(kBufferSize);
			rg.Generate();
			CRC crc = new CRC();
			crc.Init();
			crc.Update(rg.Buffer, 0, rg.BufferSize);

			CProgressInfo progressInfo = new CProgressInfo();
			progressInfo.ApprovedStart = dictionarySize;

			UInt64 totalBenchSize = 0;
			UInt64 totalEncodeTime = 0;
			UInt64 totalDecodeTime = 0;
			UInt64 totalCompressedSize = 0;

			MemoryStream inStream = new MemoryStream(rg.Buffer, 0, (int)rg.BufferSize);
			MemoryStream compressedStream = new MemoryStream((int)kCompressedBufferSize);
			CrcOutStream crcOutStream = new CrcOutStream();
			for (Int32 i = 0; i < numIterations; i++)
			{
				progressInfo.Init();
				inStream.Seek(0, SeekOrigin.Begin);
				compressedStream.Seek(0, SeekOrigin.Begin);
				encoder.Code(inStream, compressedStream, -1, -1, progressInfo);
				TimeSpan sp2 = DateTime.UtcNow - progressInfo.Time;
				UInt64 encodeTime = (UInt64)sp2.Ticks;

				long compressedSize = compressedStream.Position;
				if (progressInfo.InSize == 0)
					throw (new Exception("Internal ERROR 1282"));

				UInt64 decodeTime = 0;
				for (int j = 0; j < 2; j++)
				{
					compressedStream.Seek(0, SeekOrigin.Begin);
					crcOutStream.Init();

					decoder.SetDecoderProperties(propArray);
					UInt64 outSize = kBufferSize;
					System.DateTime startTime = DateTime.UtcNow;
					decoder.Code(compressedStream, crcOutStream, 0, (Int64)outSize, null);
					TimeSpan sp = (DateTime.UtcNow - startTime);
					decodeTime = (ulong)sp.Ticks;
					if (crcOutStream.GetDigest() != crc.GetDigest())
						throw (new Exception("CRC Error"));
				}
				UInt64 benchSize = kBufferSize - (UInt64)progressInfo.InSize;
				PrintResults(dictionarySize, encodeTime, benchSize, false, 0);
				System.Console.Write("     ");
				PrintResults(dictionarySize, decodeTime, kBufferSize, true, (ulong)compressedSize);
				System.Console.WriteLine();

				totalBenchSize += benchSize;
				totalEncodeTime += encodeTime;
				totalDecodeTime += decodeTime;
				totalCompressedSize += (ulong)compressedSize;
			}
			System.Console.WriteLine("---------------------------------------------------");
			PrintResults(dictionarySize, totalEncodeTime, totalBenchSize, false, 0);
			System.Console.Write("     ");
			PrintResults(dictionarySize, totalDecodeTime,
					kBufferSize * (UInt64)numIterations, true, totalCompressedSize);
			System.Console.WriteLine("    Average");
			return 0;
		}
	}
}