-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathProgram.cs
More file actions
342 lines (299 loc) · 14.3 KB
/
Copy pathProgram.cs
File metadata and controls
342 lines (299 loc) · 14.3 KB
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
341
342
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using CommandLine;
using Snappy.Sharp;
namespace Snappy.Performance
{
class Program
{
private class CompressionResult
{
private static readonly long NanosecPerTick = 1000000000 / Stopwatch.Frequency;
public CompressionDirection Direction { get; set; }
public string FileName { get; set; }
public long FileBytes { get; set; }
public TimeSpan ElapsedTime { get; set; }
public double StandardDeviation { get; set; }
public int Iterations { get; set; }
public long CompressedSize { get; set; }
public double CompresionPercentage
{
get { return ((double)CompressedSize/FileBytes); }
}
public double Throughput
{
get
{
double ticksPerIter = (double)ElapsedTime.Ticks / Iterations;
double sec = (ticksPerIter * NanosecPerTick) / 1e9;
var mb = ((double)FileBytes / (1024 * 1024));
return mb/sec;
}
}
public override string ToString()
{
return string.Format("{0,-20}\t{1,10}\t{2}\t{3:F2}\t{4:P}\t{5:F2}",
Path.GetFileName(FileName),
ElapsedTime.Ticks * NanosecPerTick,
Iterations,
Throughput,
CompresionPercentage,
StandardDeviation);
}
public static string HeaderString = string.Format("{0,-20}\t{1,10}\t{2}\t{3}\t{4}", "File", "Time (ns)", "Iter", "MB/s", "Compression");
public XElement ToXml()
{
var x = new XElement("Result",
new XElement("FileName", Path.GetFileName(FileName)),
new XElement("FileBytes", FileBytes),
new XElement("CompressedSize", CompressedSize),
new XElement("Ticks", ElapsedTime.Ticks),
new XElement("Iterations", Iterations)
);
x.SetAttributeValue("direction", Direction);
return x;
}
public static CompressionResult FromXml(XElement xml)
{
return new CompressionResult
{
FileName = xml.Element("FileName").Value,
FileBytes = long.Parse(xml.Element("FileBytes").Value),
CompressedSize= long.Parse(xml.Element("CompressedSize").Value),
ElapsedTime = new TimeSpan(long.Parse(xml.Element("Ticks").Value)),
Iterations = int.Parse(xml.Element("Iterations").Value),
};
}
}
static double StdDev(IEnumerable<long> values)
{
double ret = 0;
if (values.Any())
{
double avg = values.Average();
double sum = values.Sum(val => Math.Pow(val - avg, 2));
ret = Math.Sqrt((sum) / (values.Count() - 1));
}
return ret;
}
static CompressionResult RunCompression(string fileName, int iterations)
{
int size = 0;
long[] ticks = new long[iterations];
byte[] uncompressed = File.ReadAllBytes(fileName);
var target = new SnappyCompressor();
var s = new Stopwatch();
for (int i = 0; i < iterations; i++)
{
var result = new byte[target.MaxCompressedLength(uncompressed.Length)];
s.Start();
size = target.Compress(uncompressed, 0, uncompressed.Length, result);
s.Stop();
ticks[i] = s.ElapsedTicks;
s.Reset();
}
return new CompressionResult
{
Direction = CompressionDirection.Compress,
FileName = FileMap[Path.GetFileName(fileName)],
CompressedSize = size,
FileBytes = uncompressed.Length,
ElapsedTime = new TimeSpan(ticks.Sum()),
StandardDeviation = StdDev(ticks),
Iterations = iterations
};
}
private static CompressionResult RunDecompression(string fileName, int iterations)
{
long[] ticks = new long[iterations];
byte[] uncompressed = File.ReadAllBytes(fileName);
var compressed = Sharp.Snappy.Compress(uncompressed);
int size = compressed.Length;
var target = new SnappyDecompressor();
var s = new Stopwatch();
for (int i = 0; i < iterations; i++)
{
s.Start();
var result = target.Decompress(compressed, 0, compressed.Length);
s.Stop();
ticks[i] = s.ElapsedTicks;
s.Reset();
}
return new CompressionResult
{
Direction = CompressionDirection.Decompress,
FileName = FileMap[Path.GetFileName(fileName)],
CompressedSize = size,
FileBytes = uncompressed.Length,
ElapsedTime = new TimeSpan(ticks.Sum()),
StandardDeviation = StdDev(ticks),
Iterations = iterations
};
}
private class Options
{
[Option('p', "pref", Default = false, HelpText = "Run performance.")]
public bool Performance { get; set; }
[Option('i', "iter", Default = 100, HelpText = "Number of iterations to run.")]
public int Iterations { get; set; }
[Option('c', "compare", Default = false, HelpText = "Compare to previous results.")]
public bool Compare { get; set; }
[Option('v', "verify", Default = false, HelpText = "Run readfile => compress => decompress => compare results")]
public bool Verify { get; set; }
[Option('x', "outputxml", Default = false, HelpText = "Save results to xml file.")]
public bool WriteXml { get; set; }
[Option('o', "xmldirectory", HelpText = "Directory to load/store xml results.", Default = @"..\..\..\..\perfdata")] // TODO Path.combine
public string XmlDirectory { get; set; }
[Option('d', "datadirectory", HelpText = "Directory to load test files.", Default = @"..\..\..\..\testdata")]
public string TestDataDirectory { get; set; }
// [HelpOption]
// public string GetUsage()
// {
// var usage = new StringBuilder();
// usage.AppendLine("Snappy.Performance.exe");
// usage.AppendLine("\t-c or --compare to indicate comparision to previous results");
// usage.AppendLine("\t-v or --verify to indicate data verficiation of round trip");
// usage.AppendLine("\t-x or --outputxml to indicate save output to xml");
// usage.AppendLine("\t-d<directory> or --datadirectory<directory> specifies source directory for files to test");
// usage.AppendLine("\t-o<directory> or --xmldirectory<directory> specifies directory to save xml results");
// return usage.ToString();
// }
}
static string xmlPath = @"snappyoutput";
static void Main(string[] args)
{
Parser.Default.ParseArguments<Options>(args)
.WithParsed(options =>
{
int iters = options.Iterations;
if (Directory.Exists(options.XmlDirectory))
xmlPath = options.XmlDirectory;
else
{
throw new DirectoryNotFoundException("Could not find specified xml directory.");
}
List<CompressionResult> results = new List<CompressionResult>();
foreach (string fileName in FileMap.Keys.Select(file => Path.Combine(options.TestDataDirectory,
file)))
{
if (options.Verify)
{
VerifyRoundTrip(fileName);
Console.WriteLine("Verified {0}", Path.GetFileName(fileName));
}
if (options.Performance)
{
results.Add(RunCompression(fileName, iters));
results.Add(RunDecompression(fileName, iters));
}
}
if (options.Compare)
{
Console.WriteLine(CompressionResult.HeaderString);
CompareResults(results.Where(r => r.Direction == CompressionDirection.Decompress));
Console.WriteLine();
CompareResults(results.Where(r => r.Direction == CompressionDirection.Compress));
}
else
{
foreach (var result in results.Where(r => r.Direction == CompressionDirection.Decompress))
Console.WriteLine(result);
foreach (var result in results.Where(r => r.Direction == CompressionDirection.Compress))
Console.WriteLine(result);
}
if (options.WriteXml)
{
WriteResultsAsXml(results);
}
});
}
private static void VerifyRoundTrip(string fileName)
{
int size = 0;
byte[] uncompressed = File.ReadAllBytes(fileName);
var compressor = new SnappyCompressor();
var result = new byte[compressor.MaxCompressedLength(uncompressed.Length)];
size = compressor.Compress(uncompressed, 0, uncompressed.Length, result);
Array.Resize(ref result, size);
var decompressor = new SnappyDecompressor();
var decompressed = decompressor.Decompress(result, 0, size);
byte[] source = File.ReadAllBytes(fileName);
if (source.Length != decompressed.Length)
throw new Exception(string.Format("Decompressed length {0} does not match original {1}", decompressed.Length, source.Length));
for (int i = 0; i < uncompressed.Length; i++)
if (source[i] != decompressed[i])
throw new Exception(string.Format("Decompressed data did not match original at index {0}", i));
}
private static void WriteResultsAsXml(IEnumerable<CompressionResult> results)
{
XDocument xd = new XDocument();
xd.Add(new XElement("results", results.Select(r => r.ToXml())));
using (var file = new FileStream(Path.Combine(xmlPath, string.Format("{0:MMddyyy-hhmmss}.xml", DateTime.Now)), FileMode.CreateNew, FileAccess.Write))
using (var writer = XmlWriter.Create(file))
{
xd.WriteTo(writer);
}
}
private static void CompareResults(IEnumerable<CompressionResult> results)
{
var lastResult = Directory.GetFiles(xmlPath, "*.xml", SearchOption.TopDirectoryOnly).Select(f => new {FilePath = f, Creation = File.GetCreationTime(f)}).OrderByDescending(f => f.Creation).FirstOrDefault();
if (lastResult != null)
{
XDocument oldResults;
using (var file = new FileStream(lastResult.FilePath, FileMode.Open, FileAccess.Read))
{
oldResults = XDocument.Load(file);
}
foreach (var r in results)
{
var match = CompressionResult.FromXml(oldResults.Descendants("Result").FirstOrDefault(x => x.Attribute("direction").Value == r.Direction.ToString() && x.Element("FileName").Value == r.FileName));
if (match != null)
{
Console.Write(r.ToString());
var currentColor = Console.ForegroundColor;
double speedup = CalculateSpeedup(r.Throughput, match.Throughput);
if (speedup > 1)
Console.ForegroundColor = r.Throughput < match.Throughput ? ConsoleColor.Red : ConsoleColor.Green;
Console.WriteLine(" [{0:F2}%]", speedup);
Console.ForegroundColor = currentColor;
}
}
}
}
private static double CalculateSpeedup(double throughput, double d)
{
return Math.Abs(100 - (100 * throughput / d));
}
static readonly Dictionary<string,string> FileMap = new Dictionary<string,string> {
{ "html", "html" },
{ "urls.10K", "urls" },
{ "house.jpg", "jpg" },
{ "mapreduce-osdi-1.pdf","pdf" },
{ "html_x_4", "html4" },
{ "cp.html", "cp" },
{ "fields.c", "c" },
{ "grammar.lsp", "lsp" },
{ "kennedy.xls", "xls" },
{ "alice29.txt", "txt1" },
{ "asyoulik.txt", "txt2" },
{ "lcet10.txt", "txt3" },
{ "plrabn12.txt", "txt4" },
{ "ptt5", "bin"},
{ "sum", "sum" },
{ "xargs.1", "man" },
{ "geo.protodata","pb" },
{ "kppkn.gtb","gaviota" },
};
}
enum CompressionDirection
{
Compress,
Decompress
}
}