-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathBenchmarks.cs
100 lines (86 loc) · 3.43 KB
/
Benchmarks.cs
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
//---------------------------------------------------------------------
// <copyright file="Benchmarks.cs" company="Microsoft">
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
// </copyright>
//---------------------------------------------------------------------
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
using BenchmarkDotNet.Diagnosers;
using ExperimentsLib;
using Microsoft.OData.Edm;
namespace JsonWriterBenchmarks
{
[MemoryDiagnoser]
[Orderer(SummaryOrderPolicy.Method, MethodOrderPolicy.Declared)]
public class Benchmarks
{
private readonly static WriterCollection<IEnumerable<Customer>> writerCollection = DefaultWriterCollection.Create();
private readonly IEnumerable<Customer> data;
private readonly IEnumerable<Customer> dataWithLargeValues;
private readonly IEdmModel model;
private Stream outputStream;
private string filePath;
private IPayloadWriter<IEnumerable<Customer>> writer;
[ParamsSource(nameof(WriterNames))]
public string WriterName;
public static IEnumerable<string> WriterNames() =>
DefaultWriterCollection.Create().GetWriterNames();
public Benchmarks()
{
// the written output will be about 1.45MB of JSON text
data = CustomerDataSet.GetCustomers(5000);
// contains fields with 1MB+ values each
dataWithLargeValues = CustomerDataSet.GetDataWithLargeFields(30);
model = DataModel.GetEdmModel();
}
[GlobalSetup]
public void Setup()
{
writer = writerCollection.GetWriter(WriterName);
filePath = Path.GetTempFileName();
outputStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, share: FileShare.ReadWrite);
}
[GlobalCleanup]
public void Cleanup()
{
outputStream.Dispose();
File.Delete(filePath);
}
[Benchmark]
[BenchmarkCategory("InMemory")]
public async Task WriteToMemoryAsync()
{
using var memoryStream = new MemoryStream();
await writer.WritePayloadAsync(data, memoryStream);
}
[Benchmark]
[BenchmarkCategory("ToFile")]
public async Task WriteToFileAsync()
{
// multiple writes to increase benchmark duration
await WritePayloadAsync(data);
await WritePayloadAsync(data);
}
[Benchmark]
[BenchmarkCategory("RawValues")]
public async Task WriteWithRawValues()
{
// multiple writes to increase benchmark duration
await WritePayloadAsync(data, includeRawValues: true);
await WritePayloadAsync(data, includeRawValues: true);
}
[Benchmark]
[BenchmarkCategory("ToFile")]
public async Task WriteToFileWithLargeValuesAsync()
{
await WritePayloadAsync(dataWithLargeValues);
}
private async Task WritePayloadAsync(IEnumerable<Customer> payload, bool includeRawValues = false)
{
await writer.WritePayloadAsync(payload, outputStream, includeRawValues);
}
}
}