Sunday 28 February 2016

Benchmark - the correct tool

[BenchMark] is fantastic. It keeps the boilerplate code to capture the important process level metric abstracted away from you and lets you focus on writing useful application level flow tests that are important to you and business. And using it is as simple as adding an attribute on the function that you wish to benchmark and asking the benchmarkrunner to take care of rest of the stuff. I tried it out with some old code blocks.

First and most important thing is that you have to write less code :)

public class TypeA
    {
        public string Name { get; set; }
        public string Value { get; set; }
        [Benchmark]
        public static void MeasureJsonSerializationPerformance()
        {
            var obj = new TypeA() { Name = "Name", Value = "Value" };
            string[] jsonStrings = new string[1];
            string jsonString1 = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
            jsonStrings[0] = jsonString1;
        }
        [Benchmark]
        public static void MeasureBsonSerializationPerformance()
        {
            var obj = new TypeA() { Name = "Name", Value = "Value" };
            string[] bsonStrings = new string[1];
            MemoryStream ms = new MemoryStream();
            using (BsonWriter writer = new BsonWriter(ms))
            {
                JsonSerializer serializer = new JsonSerializer();
                serializer.Serialize(writer, obj);
            }
            string bsonString1 = Convert.ToBase64String(ms.ToArray());
            bsonStrings[0] = bsonString1;
        }
}

That is pretty much all I needed to write as my test bed. Now all I need to do is to call this:

BenchmarkRunner.Run();

There you have it. Benchmark takes care of many of the low level nuances that you would otherwise not be thinking of when writing your own performance testing console app. The performance results show up in a nice window:

// * Summary *
BenchmarkDotNet=v0.9.1.0
OS=Microsoft Windows NT 6.2.9200.0
Processor=Intel(R) Core(TM) i7-3740QM CPU @ 2.70GHz, ProcessorCount=8
Frequency=2628190 ticks, Resolution=380.4900 ns
HostCLR=MS.NET 4.0.30319.42000, Arch=32-bit RELEASE
Type=TypeA  Mode=Throughput
                              Method |        Median |     StdDev |
------------------------------------ |-------------- |----------- |
 MeasureBsonSerializationPerformance | 1,651.0726 ns | 11.7229 ns |
 MeasureJsonSerializationPerformance |   857.5405 ns | 12.1060 ns |
// ***** BenchmarkRunner: End *****

No comments:

Post a Comment