-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathContentSerializationPerformanceTests.cs
More file actions
272 lines (243 loc) · 12 KB
/
ContentSerializationPerformanceTests.cs
File metadata and controls
272 lines (243 loc) · 12 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Documents;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[Ignore]
[TestClass]
public class ContentSerializationPerformanceTests
{
private const string DiagnosticsDataFileName = "ContentSerializationPerformanceTestsDiagnosticsData.txt";
private readonly Dictionary<SupportedSerializationFormats, QueryStatisticsDatumVisitor> queryStatisticsDatumVisitorMap;
private readonly string endpoint;
private readonly string authKey;
private readonly string cosmosDatabaseId;
private readonly string containerId;
private readonly int insertDocumentCount;
private readonly IReadOnlyList<string> queries;
private readonly int iterationCount;
private readonly int warmupIterationCount;
private readonly int maxConcurrency;
private readonly int maxItemCount;
private readonly bool useStronglyTypedIterator;
private readonly string outputPath;
private readonly string outputDirectoryName;
public ContentSerializationPerformanceTests()
{
this.queryStatisticsDatumVisitorMap = new Dictionary<SupportedSerializationFormats, QueryStatisticsDatumVisitor>
{
{ SupportedSerializationFormats.JsonText, new QueryStatisticsDatumVisitor() },
{ SupportedSerializationFormats.CosmosBinary, new QueryStatisticsDatumVisitor() }
};
this.endpoint = Utils.ConfigurationManager.AppSettings["GatewayEndpoint"];
this.authKey = Utils.ConfigurationManager.AppSettings["MasterKey"];
this.cosmosDatabaseId = Utils.ConfigurationManager.AppSettings["QueryPerformanceTests.CosmosDatabaseId"];
this.containerId = Utils.ConfigurationManager.AppSettings["QueryPerformanceTests.ContainerId"];
this.insertDocumentCount = int.Parse(Utils.ConfigurationManager.AppSettings["QueryPerformanceTests.InsertDocumentCount"]);
this.queries = JsonSerializer.Deserialize<List<string>>(Utils.ConfigurationManager.AppSettings["QueryPerformanceTests.Queries"]);
this.iterationCount = int.Parse(Utils.ConfigurationManager.AppSettings["QueryPerformanceTests.NumberOfIterations"]);
this.warmupIterationCount = int.Parse(Utils.ConfigurationManager.AppSettings["QueryPerformanceTests.WarmupIterations"]);
this.maxConcurrency = int.Parse(Utils.ConfigurationManager.AppSettings["QueryPerformanceTests.MaxConcurrency"]);
this.maxItemCount = int.Parse(Utils.ConfigurationManager.AppSettings["QueryPerformanceTests.MaxItemCount"]);
this.useStronglyTypedIterator = bool.Parse(Utils.ConfigurationManager.AppSettings["QueryPerformanceTests.UseStronglyTypedIterator"]);
this.outputPath = Utils.ConfigurationManager.AppSettings["QueryPerformanceTests.OutputPath"];
this.outputDirectoryName = Utils.ConfigurationManager.AppSettings["QueryPerformanceTests.OutputDirectoryName"];
}
[TestMethod]
public async Task SetupBenchmark()
{
CosmosClient client = new CosmosClient(
this.endpoint,
this.authKey,
new CosmosClientOptions
{
ConnectionMode = ConnectionMode.Direct
});
Cosmos.Database database = await client.CreateDatabaseIfNotExistsAsync(this.cosmosDatabaseId);
FeedIterator<ContainerProperties> iterator = database.GetContainerQueryIterator<ContainerProperties>();
bool containerExists = false;
while (iterator.HasMoreResults)
{
FeedResponse<ContainerProperties> containers = await iterator.ReadNextAsync().ConfigureAwait(false);
if (containers.Any(c => c.Id == this.containerId))
{
containerExists = true;
break;
}
}
if (containerExists)
{
Container previousContainer = database.GetContainer(this.containerId);
await previousContainer.DeleteContainerAsync();
}
Container container = await database.CreateContainerIfNotExistsAsync(
id: this.containerId,
partitionKeyPath: "/myPartitionKey",
throughput: 400
);
await this.InsertDocuments(container);
}
[TestMethod]
public async Task RunBenchmark()
{
using (CosmosClient client = new CosmosClient(
this.endpoint,
this.authKey,
new CosmosClientOptions
{
ConnectionMode = ConnectionMode.Direct
}))
{
await this.RunAsync(client);
}
}
private async Task RunAsync(CosmosClient client)
{
Container container = client.GetContainer(this.cosmosDatabaseId, this.containerId);
string outputPath = Path.GetFullPath(!string.IsNullOrWhiteSpace(this.outputPath) ? this.outputPath : Directory.GetCurrentDirectory());
string fullOutputPath = Path.Combine(outputPath, this.outputDirectoryName);
if (!Directory.Exists(fullOutputPath))
{
Directory.CreateDirectory(fullOutputPath);
}
foreach (string query in this.queries)
{
foreach (SupportedSerializationFormats serializationFormat in new[] { SupportedSerializationFormats.JsonText, SupportedSerializationFormats.CosmosBinary })
{
for (int i = 0; i < this.warmupIterationCount; i++)
{
await this.RunQueryAsync(container, query, serializationFormat, isWarmup: true);
}
MetricsSerializer metricsSerializer = new();
for (int i = 0; i < this.iterationCount; i++)
{
await this.RunQueryAsync(container, query, serializationFormat, isWarmup: false);
}
metricsSerializer.Serialize(fullOutputPath, this.queryStatisticsDatumVisitorMap[serializationFormat], this.iterationCount, query, serializationFormat);
}
}
Console.WriteLine($"Output file path: {fullOutputPath}");
File.Delete(Path.GetFullPath(DiagnosticsDataFileName));
}
private async Task InsertDocuments(Container container)
{
string[] regions = { "Arizona", "California", "Florida", "Utah", "New York", "Oregon" };
for (int i = 0; i < this.insertDocumentCount; i++)
{
States state = new States
{
MyPartitionKey = $"partition-{i}",
Id = $"id-{i}",
Name = $"State-{i}",
City = $"City-{i % 1000}",
PostalCode = $"{(10000 + (i % 90000))}",
Region = regions[i % regions.Length],
UserDefinedID = i % 1000,
WordsArray = new List<string> { "alpha", "beta", "gamma", "delta" },
Tags = new Tags
{
Words = new List<string> { "sun", "moon", "stars", "comet" },
Numbers = $"{i % 100}-{(i / 2) % 100}-{(i / 3) % 100}"
},
RecipientList = new List<RecipientList>
{
new RecipientList
{
Name = $"Recipient-{i % 100}",
City = $"RecipientCity-{i % 1000}",
PostalCode = $"{(20000 + (i * 7) % 80000)}",
Region = regions[i % regions.Length],
GUID = $"guid-{i}",
Quantity = (i % 99) + 1
}
}
};
await container.CreateItemAsync(state, new Cosmos.PartitionKey(state.MyPartitionKey));
if(i % 1000 == 0)
{
Console.WriteLine($"Number of documents inserted: " + i);
}
}
}
private async Task RunQueryAsync(Container container, string query, SupportedSerializationFormats serializationFormat, bool isWarmup)
{
QueryRequestOptions requestOptions = new QueryRequestOptions()
{
MaxConcurrency = this.maxConcurrency,
MaxItemCount = this.maxItemCount,
SupportedSerializationFormats = serializationFormat
};
if (isWarmup)
{
using (FeedIterator<dynamic> iterator = container.GetItemQueryIterator<dynamic>(
queryText: query,
requestOptions: requestOptions))
{
while (iterator.HasMoreResults)
{
await iterator.ReadNextAsync();
}
}
}
else
{
if (this.useStronglyTypedIterator)
{
using (FeedIterator<States> iterator = container.GetItemQueryIterator<States>(
queryText: query,
requestOptions: requestOptions))
{
await this.GetIteratorResponse(iterator, serializationFormat);
}
}
else
{
using (FeedIterator<dynamic> iterator = container.GetItemQueryIterator<dynamic>(
queryText: query,
requestOptions: requestOptions))
{
await this.GetIteratorResponse(iterator, serializationFormat);
}
}
}
}
private async Task GetIteratorResponse<T>(FeedIterator<T> feedIterator, SupportedSerializationFormats serializationFormat)
{
string diagnosticDataPath = Path.GetFullPath(DiagnosticsDataFileName);
QueryStatisticsDatumVisitor visitor = this.queryStatisticsDatumVisitorMap[serializationFormat];
MetricsAccumulator metricsAccumulator = new MetricsAccumulator();
Documents.ValueStopwatch totalTime = new Documents.ValueStopwatch();
Documents.ValueStopwatch accumulateMetricsTime = new Documents.ValueStopwatch();
while (feedIterator.HasMoreResults)
{
totalTime.Start();
FeedResponse<T> response = await feedIterator.ReadNextAsync();
accumulateMetricsTime.Start();
if (response.RequestCharge != 0)
{
using (StreamWriter outputFile = new StreamWriter(path: diagnosticDataPath, append: true))
{
outputFile.WriteLine(response.Diagnostics.ToString());
}
metricsAccumulator.ReadFromTrace(response, visitor);
}
accumulateMetricsTime.Stop();
totalTime.Stop();
if (response.RequestCharge != 0)
{
visitor.AddEndToEndTime(totalTime.ElapsedMilliseconds - accumulateMetricsTime.ElapsedMilliseconds);
visitor.AddRequestCharge(response.RequestCharge);
visitor.PopulateMetrics();
}
totalTime.Reset();
accumulateMetricsTime.Reset();
}
}
}
}