1baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpackage SevenZip;
2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
3baa3858d3f5d128a5c8466b700098109edcad5f2repo syncimport java.io.ByteArrayOutputStream;
4baa3858d3f5d128a5c8466b700098109edcad5f2repo syncimport java.io.ByteArrayInputStream;
5baa3858d3f5d128a5c8466b700098109edcad5f2repo syncimport java.io.IOException;
6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
7baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpublic class LzmaBench
8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	static final int kAdditionalSize = (1 << 21);
10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	static final int kCompressedAdditionalSize = (1 << 10);
11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	static class CRandomGenerator
13baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
14baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int A1;
15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int A2;
16baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public CRandomGenerator() { Init(); }
17baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public void Init() { A1 = 362436069; A2 = 521288629; }
18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public int GetRnd()
19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			return
21baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				((A1 = 36969 * (A1 & 0xffff) + (A1 >>> 16)) << 16) ^
22baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				((A2 = 18000 * (A2 & 0xffff) + (A2 >>> 16)));
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	};
25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	static class CBitRandomGenerator
27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		CRandomGenerator RG = new CRandomGenerator();
29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int Value;
30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int NumBits;
31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public void Init()
32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			Value = 0;
34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			NumBits = 0;
35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
36baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public int GetRnd(int numBits)
37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
38baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			int result;
39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (NumBits > numBits)
40baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				result = Value & ((1 << numBits) - 1);
42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				Value >>>= numBits;
43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				NumBits -= numBits;
44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				return result;
45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			numBits -= NumBits;
47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			result = (Value << numBits);
48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			Value = RG.GetRnd();
49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			result |= Value & (((int)1 << numBits) - 1);
50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			Value >>>= numBits;
51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			NumBits = 32 - numBits;
52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			return result;
53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	};
55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
56baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	static class CBenchRandomGenerator
57baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		CBitRandomGenerator RG = new CBitRandomGenerator();
59baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int Pos;
60baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int Rep0;
61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public int BufferSize;
63baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public byte[] Buffer = null;
64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
65baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public CBenchRandomGenerator() { }
66baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public void Set(int bufferSize)
67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
68baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			Buffer = new byte[bufferSize];
69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			Pos = 0;
70baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			BufferSize = bufferSize;
71baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
72baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int GetRndBit() { return RG.GetRnd(1); }
73baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int GetLogRandBits(int numBits)
74baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
75baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			int len = RG.GetRnd(numBits);
76baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			return RG.GetRnd((int)len);
77baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
78baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int GetOffset()
79baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
80baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (GetRndBit() == 0)
81baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				return GetLogRandBits(4);
82baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			return (GetLogRandBits(4) << 10) | RG.GetRnd(10);
83baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
84baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int GetLen1() { return RG.GetRnd(1 + (int)RG.GetRnd(2)); }
85baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int GetLen2() { return RG.GetRnd(2 + (int)RG.GetRnd(2)); }
86baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public void Generate()
87baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
88baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			RG.Init();
89baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			Rep0 = 1;
90baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			while (Pos < BufferSize)
91baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
92baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (GetRndBit() == 0 || Pos < 1)
93baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					Buffer[Pos++] = (byte)(RG.GetRnd(8));
94baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				else
95baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				{
96baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					int len;
97baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					if (RG.GetRnd(3) == 0)
98baa3858d3f5d128a5c8466b700098109edcad5f2repo sync						len = 1 + GetLen1();
99baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					else
100baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					{
101baa3858d3f5d128a5c8466b700098109edcad5f2repo sync						do
102baa3858d3f5d128a5c8466b700098109edcad5f2repo sync							Rep0 = GetOffset();
103baa3858d3f5d128a5c8466b700098109edcad5f2repo sync						while (Rep0 >= Pos);
104baa3858d3f5d128a5c8466b700098109edcad5f2repo sync						Rep0++;
105baa3858d3f5d128a5c8466b700098109edcad5f2repo sync						len = 2 + GetLen2();
106baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					}
107baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					for (int i = 0; i < len && Pos < BufferSize; i++, Pos++)
108baa3858d3f5d128a5c8466b700098109edcad5f2repo sync						Buffer[Pos] = Buffer[Pos - Rep0];
109baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				}
110baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
111baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
112baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	};
113baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
114baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	static class CrcOutStream extends java.io.OutputStream
115baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
116baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public CRC CRC = new CRC();
117baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
118baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public void Init()
119baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
120baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			CRC.Init();
121baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
122baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public int GetDigest()
123baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
124baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			return CRC.GetDigest();
125baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
126baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public void write(byte[] b)
127baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
128baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			CRC.Update(b);
129baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
130baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public void write(byte[] b, int off, int len)
131baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
132baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			CRC.Update(b, off, len);
133baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
134baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public void write(int b)
135baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
136baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			CRC.UpdateByte(b);
137baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
138baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	};
139baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
140baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	static class MyOutputStream extends java.io.OutputStream
141baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
142baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		byte[] _buffer;
143baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int _size;
144baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int _pos;
145baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
146baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public MyOutputStream(byte[] buffer)
147baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
148baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			_buffer = buffer;
149baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			_size = _buffer.length;
150baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
151baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
152baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public void reset()
153baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
154baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			_pos = 0;
155baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
156baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
157baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public void write(int b) throws IOException
158baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
159baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (_pos >= _size)
160baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				throw new IOException("Error");
161baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			_buffer[_pos++] = (byte)b;
162baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
163baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
164baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public int size()
165baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
166baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			return _pos;
167baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
168baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	};
169baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
170baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	static class MyInputStream extends java.io.InputStream
171baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
172baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		byte[] _buffer;
173baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int _size;
174baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int _pos;
175baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
176baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public MyInputStream(byte[] buffer, int size)
177baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
178baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			_buffer = buffer;
179baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			_size = size;
180baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
181baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
182baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public void reset()
183baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
184baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			_pos = 0;
185baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
186baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
187baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public int read()
188baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
189baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (_pos >= _size)
190baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				return -1;
191baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			return _buffer[_pos++] & 0xFF;
192baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
193baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	};
194baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
195baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	static class CProgressInfo implements ICodeProgress
196baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
197baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public long ApprovedStart;
198baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public long InSize;
199baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public long Time;
200baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public void Init()
201baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{ InSize = 0; }
202baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		public void SetProgress(long inSize, long outSize)
203baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
204baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (inSize >= ApprovedStart && InSize == 0)
205baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
206baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				Time = System.currentTimeMillis();
207baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				InSize = inSize;
208baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
209baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
210baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	}
211baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	static final int kSubBits = 8;
212baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
213baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	static int GetLogSize(int size)
214baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
215baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		for (int i = kSubBits; i < 32; i++)
216baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			for (int j = 0; j < (1 << kSubBits); j++)
217baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (size <= ((1) << i) + (j << (i - kSubBits)))
218baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					return (i << kSubBits) + j;
219baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		return (32 << kSubBits);
220baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	}
221baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
222baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	static long MyMultDiv64(long value, long elapsedTime)
223baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
224baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		long freq = 1000; // ms
225baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		long elTime = elapsedTime;
226baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		while (freq > 1000000)
227baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
228baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			freq >>>= 1;
229baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			elTime >>>= 1;
230baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
231baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		if (elTime == 0)
232baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			elTime = 1;
233baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		return value * freq / elTime;
234baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	}
235baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
236baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	static long GetCompressRating(int dictionarySize, long elapsedTime, long size)
237baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
238baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		long t = GetLogSize(dictionarySize) - (18 << kSubBits);
239baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		long numCommandsForOne = 1060 + ((t * t * 10) >> (2 * kSubBits));
240baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		long numCommands = (long)(size) * numCommandsForOne;
241baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		return MyMultDiv64(numCommands, elapsedTime);
242baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	}
243baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
244baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	static long GetDecompressRating(long elapsedTime, long outSize, long inSize)
245baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
246baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		long numCommands = inSize * 220 + outSize * 20;
247baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		return MyMultDiv64(numCommands, elapsedTime);
248baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	}
249baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
250baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	static long GetTotalRating(
251baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			int dictionarySize,
252baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			long elapsedTimeEn, long sizeEn,
253baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			long elapsedTimeDe,
254baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			long inSizeDe, long outSizeDe)
255baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
256baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		return (GetCompressRating(dictionarySize, elapsedTimeEn, sizeEn) +
257baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				GetDecompressRating(elapsedTimeDe, inSizeDe, outSizeDe)) / 2;
258baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	}
259baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
260baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	static void PrintValue(long v)
261baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
262baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		String s = "";
263baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		s += v;
264baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		for (int i = 0; i + s.length() < 6; i++)
265baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			System.out.print(" ");
266baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		System.out.print(s);
267baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	}
268baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
269baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	static void PrintRating(long rating)
270baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
271baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		PrintValue(rating / 1000000);
272baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		System.out.print(" MIPS");
273baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	}
274baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
275baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	static void PrintResults(
276baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			int dictionarySize,
277baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			long elapsedTime,
278baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			long size,
279baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			boolean decompressMode, long secondSize)
280baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
281baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		long speed = MyMultDiv64(size, elapsedTime);
282baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		PrintValue(speed / 1024);
283baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		System.out.print(" KB/s  ");
284baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		long rating;
285baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		if (decompressMode)
286baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			rating = GetDecompressRating(elapsedTime, size, secondSize);
287baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		else
288baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			rating = GetCompressRating(dictionarySize, elapsedTime, size);
289baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		PrintRating(rating);
290baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	}
291baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
292baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	static public int LzmaBenchmark(int numIterations, int dictionarySize) throws Exception
293baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
294baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		if (numIterations <= 0)
295baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			return 0;
296baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		if (dictionarySize < (1 << 18))
297baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
298baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			System.out.println("\nError: dictionary size for benchmark must be >= 18 (256 KB)");
299baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			return 1;
300baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
301baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		System.out.print("\n       Compressing                Decompressing\n\n");
302baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
303baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
304baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
305baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
306baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		if (!encoder.SetDictionarySize(dictionarySize))
307baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			throw new Exception("Incorrect dictionary size");
308baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
309baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int kBufferSize = dictionarySize + kAdditionalSize;
310baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int kCompressedBufferSize = (kBufferSize / 2) + kCompressedAdditionalSize;
311baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
312baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		ByteArrayOutputStream propStream = new ByteArrayOutputStream();
313baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		encoder.WriteCoderProperties(propStream);
314baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		byte[] propArray = propStream.toByteArray();
315baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		decoder.SetDecoderProperties(propArray);
316baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
317baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		CBenchRandomGenerator rg = new CBenchRandomGenerator();
318baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
319baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		rg.Set(kBufferSize);
320baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		rg.Generate();
321baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		CRC crc = new CRC();
322baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		crc.Init();
323baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		crc.Update(rg.Buffer, 0, rg.BufferSize);
324baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
325baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		CProgressInfo progressInfo = new CProgressInfo();
326baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		progressInfo.ApprovedStart = dictionarySize;
327baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
328baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		long totalBenchSize = 0;
329baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		long totalEncodeTime = 0;
330baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		long totalDecodeTime = 0;
331baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		long totalCompressedSize = 0;
332baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
333baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		MyInputStream inStream = new MyInputStream(rg.Buffer, rg.BufferSize);
334baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
335baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		byte[] compressedBuffer = new byte[kCompressedBufferSize];
336baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		MyOutputStream compressedStream = new MyOutputStream(compressedBuffer);
337baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		CrcOutStream crcOutStream = new CrcOutStream();
338baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		MyInputStream inputCompressedStream = null;
339baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int compressedSize = 0;
340baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		for (int i = 0; i < numIterations; i++)
341baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
342baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			progressInfo.Init();
343baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			inStream.reset();
344baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			compressedStream.reset();
345baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			encoder.Code(inStream, compressedStream, -1, -1, progressInfo);
346baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			long encodeTime = System.currentTimeMillis() - progressInfo.Time;
347baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
348baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (i == 0)
349baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
350baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				compressedSize = compressedStream.size();
351baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				inputCompressedStream = new MyInputStream(compressedBuffer, compressedSize);
352baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
353baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			else if (compressedSize != compressedStream.size())
354baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				throw (new Exception("Encoding error"));
355baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
356baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			if (progressInfo.InSize == 0)
357baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				throw (new Exception("Internal ERROR 1282"));
358baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
359baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			long decodeTime = 0;
360baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			for (int j = 0; j < 2; j++)
361baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			{
362baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				inputCompressedStream.reset();
363baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				crcOutStream.Init();
364baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
365baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				long outSize = kBufferSize;
366baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				long startTime = System.currentTimeMillis();
367baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (!decoder.Code(inputCompressedStream, crcOutStream, outSize))
368baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					throw (new Exception("Decoding Error"));;
369baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				decodeTime = System.currentTimeMillis() - startTime;
370baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				if (crcOutStream.GetDigest() != crc.GetDigest())
371baa3858d3f5d128a5c8466b700098109edcad5f2repo sync					throw (new Exception("CRC Error"));
372baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			}
373baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			long benchSize = kBufferSize - (long)progressInfo.InSize;
374baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			PrintResults(dictionarySize, encodeTime, benchSize, false, 0);
375baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			System.out.print("     ");
376baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			PrintResults(dictionarySize, decodeTime, kBufferSize, true, compressedSize);
377baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			System.out.println();
378baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
379baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			totalBenchSize += benchSize;
380baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			totalEncodeTime += encodeTime;
381baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			totalDecodeTime += decodeTime;
382baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			totalCompressedSize += compressedSize;
383baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
384baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		System.out.println("---------------------------------------------------");
385baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		PrintResults(dictionarySize, totalEncodeTime, totalBenchSize, false, 0);
386baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		System.out.print("     ");
387baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		PrintResults(dictionarySize, totalDecodeTime,
388baa3858d3f5d128a5c8466b700098109edcad5f2repo sync				kBufferSize * (long)numIterations, true, totalCompressedSize);
389baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		System.out.println("    Average");
390baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		return 0;
391baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	}
392baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
393