File size: 4,937 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Exporters;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Reports;
using Reporting;
using System.Linq;

namespace BenchmarkDotNet.Extensions
{
    internal class PerfLabExporter : ExporterBase
    {
        protected override string FileExtension => "json";
        protected override string FileCaption => "perf-lab-report";

        public PerfLabExporter()
        {
        }

        public override void ExportToLog(Summary summary, ILogger logger)
        {
            var reporter = Reporter.CreateReporter();

            DisassemblyDiagnoser disassemblyDiagnoser = summary.Reports
                .FirstOrDefault()? // disassembler was either enabled for all or none of them (so we use the first one)
                .BenchmarkCase.Config.GetDiagnosers().OfType<DisassemblyDiagnoser>().FirstOrDefault();

            foreach (var report in summary.Reports)
            {
                var test = new Test();
                test.Name = FullNameProvider.GetBenchmarkName(report.BenchmarkCase);
                test.Categories = report.BenchmarkCase.Descriptor.Categories;

                var results = from result in report.AllMeasurements
                              where result.IterationMode == Engines.IterationMode.Workload && result.IterationStage == Engines.IterationStage.Result
                              orderby result.LaunchIndex, result.IterationIndex
                              select new { result.Nanoseconds, result.Operations};

                var overheadResults = from result in report.AllMeasurements
                                      where result.IsOverhead() && result.IterationStage != Engines.IterationStage.Jitting
                                      orderby result.LaunchIndex, result.IterationIndex
                                      select new { result.Nanoseconds, result.Operations };

                test.Counters.Add(new Counter
                {
                    Name = "Duration of single invocation",
                    TopCounter = true,
                    DefaultCounter = true,
                    HigherIsBetter = false,
                    MetricName = "ns",
                    Results = (from result in results
                               select result.Nanoseconds / result.Operations).ToList()
                });
                test.Counters.Add(new Counter
                {
                    Name = "Overhead invocation",
                    TopCounter = false,
                    DefaultCounter = false,
                    HigherIsBetter = false,
                    MetricName = "ns",
                    Results = (from result in overheadResults
                               select result.Nanoseconds / result.Operations).ToList()
                });
                test.Counters.Add(new Counter
                {
                    Name = "Duration",
                    TopCounter = false,
                    DefaultCounter = false,
                    HigherIsBetter = false,
                    MetricName = "ms",
                    Results = (from result in results
                               select result.Nanoseconds).ToList()
                });

                test.Counters.Add(new Counter
                {
                    Name = "Operations",
                    TopCounter = false,
                    DefaultCounter = false,
                    HigherIsBetter = true,
                    MetricName = "Count",
                    Results = (from result in results
                               select  (double)result.Operations).ToList()
                });

                foreach (var metric in report.Metrics.Keys)
                {
                    var m = report.Metrics[metric];
                    test.Counters.Add(new Counter
                    {
                        Name = m.Descriptor.DisplayName,
                        TopCounter = false,
                        DefaultCounter = false,
                        HigherIsBetter = m.Descriptor.TheGreaterTheBetter,
                        MetricName = m.Descriptor.Unit,
                        Results = new[] { m.Value }
                    });
                }

                if (disassemblyDiagnoser != null && disassemblyDiagnoser.Results.TryGetValue(report.BenchmarkCase, out var disassemblyResult))
                {
                    string disassembly = DiffableDisassemblyExporter.BuildDisassemblyString(disassemblyResult, disassemblyDiagnoser.Config);
                    test.AdditionalData["disasm"] = disassembly;
                }

                reporter.AddTest(test);
            }

            logger.WriteLine(reporter.GetJson());
        }
    }
}