|
| 1 | +// <copyright file="DatadogDiagnoser.cs" company="Datadog"> |
| 2 | +// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. |
| 4 | +// </copyright> |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using BenchmarkDotNet.Analysers; |
| 9 | +using BenchmarkDotNet.Columns; |
| 10 | +using BenchmarkDotNet.Diagnosers; |
| 11 | +using BenchmarkDotNet.Engines; |
| 12 | +using BenchmarkDotNet.Exporters; |
| 13 | +using BenchmarkDotNet.Loggers; |
| 14 | +using BenchmarkDotNet.Reports; |
| 15 | +using BenchmarkDotNet.Running; |
| 16 | +using BenchmarkDotNet.Validators; |
| 17 | + |
| 18 | +namespace Datadog.Trace.BenchmarkDotNet; |
| 19 | + |
| 20 | +/// <summary> |
| 21 | +/// Datadog BenchmarkDotNet Diagnoser |
| 22 | +/// </summary> |
| 23 | +public class DatadogDiagnoser : IDiagnoser |
| 24 | +{ |
| 25 | + /// <summary> |
| 26 | + /// Default DatadogDiagnoser instance |
| 27 | + /// </summary> |
| 28 | + public static readonly IDiagnoser Default = new DatadogDiagnoser(); |
| 29 | + |
| 30 | + private long? _startDateTimeOffset; |
| 31 | + private long? _endDateTimeOffset; |
| 32 | + |
| 33 | + /// <inheritdoc /> |
| 34 | + public IEnumerable<string> Ids { get; } = new[] { "Datadog" }; |
| 35 | + |
| 36 | + /// <inheritdoc /> |
| 37 | + public IEnumerable<IExporter> Exporters { get; } = new[] { DatadogExporter.Default }; |
| 38 | + |
| 39 | + /// <inheritdoc /> |
| 40 | + public IEnumerable<IAnalyser> Analysers { get; } = Array.Empty<IAnalyser>(); |
| 41 | + |
| 42 | + /// <inheritdoc /> |
| 43 | + public RunMode GetRunMode(BenchmarkCase benchmarkCase) |
| 44 | + { |
| 45 | + return RunMode.NoOverhead; |
| 46 | + } |
| 47 | + |
| 48 | + /// <inheritdoc /> |
| 49 | + public bool RequiresBlockingAcknowledgments(BenchmarkCase benchmarkCase) |
| 50 | + { |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + /// <inheritdoc /> |
| 55 | + public void Handle(HostSignal signal, DiagnoserActionParameters parameters) |
| 56 | + { |
| 57 | + if (signal == HostSignal.BeforeProcessStart) |
| 58 | + { |
| 59 | + _startDateTimeOffset = DateTime.UtcNow.Ticks; |
| 60 | + } |
| 61 | + else if (signal == HostSignal.AfterProcessExit) |
| 62 | + { |
| 63 | + _endDateTimeOffset = DateTime.UtcNow.Ticks; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /// <inheritdoc /> |
| 68 | + public IEnumerable<Metric> ProcessResults(DiagnoserResults results) |
| 69 | + { |
| 70 | + yield return new Metric(new DateTimeOffSetMetricDescriptor("StartDate"), _startDateTimeOffset ?? 0d); |
| 71 | + yield return new Metric(new DateTimeOffSetMetricDescriptor("EndDate"), _endDateTimeOffset ?? 0d); |
| 72 | + } |
| 73 | + |
| 74 | + /// <inheritdoc /> |
| 75 | + public void DisplayResults(ILogger logger) |
| 76 | + { |
| 77 | + } |
| 78 | + |
| 79 | + /// <inheritdoc /> |
| 80 | + public IEnumerable<ValidationError> Validate(ValidationParameters validationParameters) |
| 81 | + { |
| 82 | + yield break; |
| 83 | + } |
| 84 | + |
| 85 | + private class DateTimeOffSetMetricDescriptor : IMetricDescriptor |
| 86 | + { |
| 87 | + public DateTimeOffSetMetricDescriptor(string id) => Id = id; |
| 88 | + |
| 89 | + public string Id { get; } |
| 90 | + |
| 91 | + public string DisplayName => Id; |
| 92 | + |
| 93 | + public string Legend => Id; |
| 94 | + |
| 95 | + public string NumberFormat => "0"; |
| 96 | + |
| 97 | + public UnitType UnitType => UnitType.Time; |
| 98 | + |
| 99 | + public string Unit => "ticks"; |
| 100 | + |
| 101 | + public bool TheGreaterTheBetter => false; |
| 102 | + |
| 103 | + public int PriorityInCategory => 0; |
| 104 | + } |
| 105 | +} |
0 commit comments