Skip to content

Instantly share code, notes, and snippets.

@CarlosLanderas
Created December 26, 2019 17:13
Show Gist options
  • Save CarlosLanderas/2aaeecdaa1f36abe6929ca35d0d339bc to your computer and use it in GitHub Desktop.
Save CarlosLanderas/2aaeecdaa1f36abe6929ca35d0d339bc to your computer and use it in GitHub Desktop.
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<PerfBenchmark>();
}
}
[MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.NetCoreApp31)]
public class PerfBenchmark
{
const string response = "{\"status\":\"Healthy\",\"totalDuration\":\"00:00:00.0101838\",\"entries\":{\"random1\":{\"data\":{},\"duration\":\"00:00:00.0013400\",\"status\":\"Healthy\"},\"random2\":{\"data\":{},\"duration\":\"00:00:00.0011863\",\"status\":\"Healthy\"}}}";
UIHealthReport report = null;
public void Setup()
{
report = JsonConvert.DeserializeObject<UIHealthReport>(response);
}
[Benchmark]
public void sync_serialize()
{
var str = JsonConvert.SerializeObject(report);
}
[Benchmark]
public async Task async_serialize()
{
using var responseStream = new MemoryStream();
await System.Text.Json.JsonSerializer.SerializeAsync(responseStream, report);
var response = responseStream.ToArray();
}
}
public class UIHealthReport
{
public UIHealthStatus Status { get; set; }
public TimeSpan TotalDuration { get; set; }
public Dictionary<string, UIHealthReportEntry> Entries { get; }
public UIHealthReport(Dictionary<string, UIHealthReportEntry> entries, TimeSpan totalDuration)
{
Entries = entries;
TotalDuration = totalDuration;
}
}
public enum UIHealthStatus
{
Unhealthy = 0,
Degraded = 1,
Healthy = 2
}
public class UIHealthReportEntry
{
public IReadOnlyDictionary<string, object> Data { get; set; }
public string Description { get; set; }
public TimeSpan Duration { get; set; }
public string Exception { get; set; }
public UIHealthStatus Status { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment