aboutsummaryrefslogtreecommitdiff
path: root/src/ProtoBench/Program.cs
diff options
context:
space:
mode:
authorJon Skeet <skeet@pobox.com>2009-01-16 10:57:40 +0000
committerJon Skeet <skeet@pobox.com>2009-01-16 10:57:40 +0000
commit8f8186a30b9b8d865c6211eb91a4df8aae1f40d5 (patch)
tree8448e4c52607cd39f2035a06196cfe4247942f06 /src/ProtoBench/Program.cs
parent0c89aa1fdba0158d8dee3aef9cfe815afe4cc494 (diff)
downloadprotobuf-8f8186a30b9b8d865c6211eb91a4df8aae1f40d5.tar.gz
protobuf-8f8186a30b9b8d865c6211eb91a4df8aae1f40d5.tar.bz2
protobuf-8f8186a30b9b8d865c6211eb91a4df8aae1f40d5.zip
Benchmarking, dumping and munging
Diffstat (limited to 'src/ProtoBench/Program.cs')
-rw-r--r--src/ProtoBench/Program.cs90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/ProtoBench/Program.cs b/src/ProtoBench/Program.cs
new file mode 100644
index 00000000..9b65c0b1
--- /dev/null
+++ b/src/ProtoBench/Program.cs
@@ -0,0 +1,90 @@
+using System;
+using System.Diagnostics;
+using System.IO;
+using Google.ProtocolBuffers;
+
+namespace ProtoBench {
+ /// <summary>
+ /// Simple benchmarking of arbitrary messages.
+ /// </summary>
+ public sealed class Program {
+
+ private static readonly TimeSpan MinSampleTime = TimeSpan.FromSeconds(2);
+ private static readonly TimeSpan TargetTime = TimeSpan.FromSeconds(30);
+
+ // Avoid a .NET 3.5 dependency
+ delegate void Action();
+
+ public static int Main(string[] args) {
+ if (args.Length != 2) {
+ Console.Error.WriteLine("Usage: ProtoBecnh <descriptor type name> <input data>");
+ Console.Error.WriteLine("The descriptor type name is the fully-qualified message name,");
+ Console.Error.WriteLine("including assembly - e.g. Google.ProtocolBuffers.BenchmarkProtos.Message1,ProtoBench");
+ return 1;
+ }
+ IMessage defaultMessage;
+ try {
+ defaultMessage = MessageUtil.GetDefaultMessage(args[0]);
+ } catch (ArgumentException e) {
+ Console.Error.WriteLine(e.Message);
+ return 1;
+ }
+ try {
+ IBuilder builder = defaultMessage.WeakCreateBuilderForType();
+ byte[] inputData = File.ReadAllBytes(args[1]);
+ ByteString inputString = ByteString.CopyFrom(inputData);
+ IMessage sampleMessage = defaultMessage.WeakCreateBuilderForType().WeakMergeFrom(inputString).WeakBuild();
+ sampleMessage.ToByteString();
+ Console.WriteLine("Benchmarking {0} with file {1}", sampleMessage.GetType().Name, args[1]);
+ Benchmark("Serialize to byte string", inputData.Length, () => sampleMessage.ToByteString());
+ Benchmark("Serialize to byte array", inputData.Length, () => sampleMessage.ToByteArray());
+ Benchmark("Serialize to memory stream", inputData.Length, () => sampleMessage.WriteTo(new MemoryStream()));
+ Benchmark("Deserialize from byte array", inputData.Length, () =>
+ defaultMessage.WeakCreateBuilderForType()
+ .WeakMergeFrom(CodedInputStream.CreateInstance(inputData))
+ .WeakBuild()
+ );
+ Benchmark("Deserialize from byte array", inputData.Length, () =>
+ defaultMessage.WeakCreateBuilderForType()
+ .WeakMergeFrom(inputString)
+ .WeakBuild()
+ );
+ return 0;
+ } catch (Exception e) {
+ Console.Error.WriteLine("Error: {0}", e.Message);
+ Console.Error.WriteLine();
+ Console.Error.WriteLine("Detailed exception information: {0}", e);
+ return 1;
+ }
+ }
+
+ private static void Benchmark(string name, int dataSize, Action action) {
+ // Make sure it's JITted
+ action();
+ // Run it progressively more times until we've got a reasonable sample
+
+ int iterations = 1;
+ TimeSpan elapsed = TimeAction(action, iterations);
+ while (elapsed < MinSampleTime) {
+ iterations *= 2;
+ elapsed = TimeAction(action, iterations);
+ }
+ // Upscale the sample to the target time. Do this in floating point arithmetic
+ // to avoid overflow issues.
+ iterations = (int) ((TargetTime.Ticks / (double)elapsed.Ticks) * iterations);
+ elapsed = TimeAction(action, iterations);
+ Console.WriteLine("{0}: {1} iterations in {2:f3}s; {3:f3}MB/s",
+ name, iterations, elapsed.TotalSeconds,
+ (iterations * dataSize) / (elapsed.TotalSeconds * 1024 * 1024));
+ }
+
+ private static TimeSpan TimeAction(Action action, int iterations) {
+ Stopwatch sw = Stopwatch.StartNew();
+ for (int i = 0; i < iterations; i++) {
+ action();
+ }
+ sw.Stop();
+ return sw.Elapsed;
+ }
+ }
+}