-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathBenchmarkConfig.cs
More file actions
303 lines (240 loc) · 13.6 KB
/
BenchmarkConfig.cs
File metadata and controls
303 lines (240 loc) · 13.6 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace CosmosBenchmark
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime;
using CommandLine;
using Microsoft.Azure.Cosmos.Telemetry;
using Microsoft.Azure.Documents.Client;
using Newtonsoft.Json;
/// <summary>
/// Represents Benchmark Configuration
/// </summary>
public class BenchmarkConfig
{
private static readonly string UserAgentSuffix = "cosmosdbdotnetbenchmark";
[Option('w', Required = true, HelpText = "Workload type insert, read")]
public string WorkloadType { get; set; }
[Option('e', Required = true, HelpText = "Cosmos account end point")]
public string EndPoint { get; set; }
[Option('k', Required = true, HelpText = "Cosmos account master key")]
[JsonIgnore]
public string Key { get; set; }
[Option("isthinclientenabled", Required = false, HelpText = "ThinClient enabled")]
public string IsThinClientEnabledRaw { get; set; }
public bool IsThinClientEnabled => string.Equals(this.IsThinClientEnabledRaw, "true", StringComparison.OrdinalIgnoreCase);
[Option("isgatewaymodeenabled", Required = false, HelpText = "Gateway mode enabled")]
public string IsGatewayModeEnabledRaw { get; set; }
public bool IsGatewayModeEnabled => string.Equals(this.IsGatewayModeEnabledRaw, "true", StringComparison.OrdinalIgnoreCase);
[Option("isdirectmodeenabled", Required = false, HelpText = "Direct mode enabled")]
public string IsDirectModeEnabledRaw { get; set; }
public bool IsDirectModeEnabled => string.Equals(this.IsDirectModeEnabledRaw, "true", StringComparison.OrdinalIgnoreCase);
[Option(Required = false, HelpText = "Workload Name, it will override the workloadType value in published results")]
public string WorkloadName { get; set; }
[Option(Required = false, HelpText = "Database to use")]
public string Database { get; set; } = "db";
[Option(Required = false, HelpText = "Collection to use")]
public string Container { get; set; } = "data";
[Option('t', Required = false, HelpText = "Collection throughput use")]
public int Throughput { get; set; } = 100000;
[Option('n', Required = false, HelpText = "Number of documents to insert")]
public int ItemCount { get; set; } = 200000;
[Option(Required = false, HelpText = "Client consistency level to override")]
public string ConsistencyLevel { get; set; }
[Option(Required = false, HelpText = "Enable latency percentiles")]
public bool EnableLatencyPercentiles { get; set; }
[Option(Required = false, HelpText = "Start with new collection")]
public bool CleanupOnStart { get; set; } = false;
[Option(Required = false, HelpText = "Clean-up after run")]
public bool CleanupOnFinish { get; set; } = false;
[Option(Required = false, HelpText = "Container partition key path")]
public string PartitionKeyPath { get; set; } = "/partitionKey";
[Option("pl", Required = false, HelpText = "Degree of parallism")]
public int DegreeOfParallelism { get; set; } = -1;
[Option("tcp", Required = false, HelpText = "MaxRequestsPerTcpConnection")]
public int? MaxRequestsPerTcpConnection { get; set; } = null;
[Option(Required = false, HelpText = "MaxTcpConnectionsPerEndpoint")]
public int? MaxTcpConnectionsPerEndpoint { get; set; } = null;
[Option(Required = false, HelpText = "Item template")]
public string ItemTemplateFile { get; set; } = "Player.json";
[Option(Required = false, HelpText = "Min thread pool size")]
public int MinThreadPoolSize { get; set; } = 100;
[Option(Required = false, HelpText = "Write the task execution failure to console. Useful for debugging failures")]
public bool TraceFailures { get; set; }
[Option(Required = false, HelpText = "Publish run results")]
public bool PublishResults { get; set; }
[Option(Required = false, HelpText = "Run ID, only for publish")]
internal string RunId { get; set; }
[Option(Required = false, HelpText = "Commit ID, only for publish")]
public string CommitId { get; set; }
[Option(Required = false, HelpText = "Commit date, only for publish")]
public string CommitDate { get; set; }
[Option(Required = false, HelpText = "Commit time, only for publish")]
public string CommitTime { get; set; }
[Option(Required = false, HelpText = "Branch name, only for publish")]
public string BranchName { get; set; }
[Option(Required = false, HelpText = "Partitionkey, only for publish")]
public string ResultsPartitionKeyValue { get; set; }
[Option(Required = false, HelpText = "Disable core SDK logging")]
public bool DisableCoreSdkLogging { get; set; }
[Option(Required = false, HelpText = "Enable Distributed Tracing")]
public bool EnableDistributedTracing { get; set; } = false;
[Option(Required = false, HelpText = "Client Telemetry Schedule in Seconds")]
public int TelemetryScheduleInSec { get; set; }
[Option(Required = false, HelpText = "Endpoint to publish results to")]
public string ResultsEndpoint { get; set; }
[Option(Required = false, HelpText = "Key to publish results to")]
[JsonIgnore]
public string ResultsKey { get; set; }
[Option(Required = false, HelpText = "Database to publish results to")]
public string ResultsDatabase { get; set; }
[Option(Required = false, HelpText = "Container to publish results to")]
public string ResultsContainer { get; set; } = "runsummary";
[Option(Required = false, HelpText = "Request latency threshold for capturing diagnostic data")]
public int DiagnosticLatencyThresholdInMs { get; set; } = 100;
[Option(Required = false, HelpText = "Blob storage account connection string")]
[JsonIgnore]
public string DiagnosticsStorageConnectionString { get; set; }
[Option(Required = false, HelpText = "Blob storage container folder prefix")]
public string DiagnosticsStorageContainerPrefix { get; set; }
[Option(Required = false, HelpText = "Metrics reporting interval in seconds")]
public int MetricsReportingIntervalInSec { get; set; } = 5;
[Option(Required = false, HelpText = "Application Insights connection string")]
public string AppInsightsConnectionString { get; set; }
[Option(Required = false, HelpText = "Enable Client Telemetry Feature in SDK. Make sure you enable it from the portal also.")]
public bool EnableTelemetry { get; set; } = false;
[Option(Required = false, HelpText = "List of comma separated preferred regions.")]
public string ApplicationPreferredRegions { get; set; } = null;
internal int GetTaskCount(int containerThroughput)
{
int taskCount = this.DegreeOfParallelism;
if (taskCount == -1)
{
// set TaskCount = 10 for each 10k RUs, minimum 1, maximum { #processor * 50 }
taskCount = Math.Max(containerThroughput / 1000, 1);
taskCount = Math.Min(taskCount, Environment.ProcessorCount * 50);
}
return taskCount;
}
internal void Print()
{
using (ConsoleColorContext ct = new ConsoleColorContext(ConsoleColor.Green))
{
Utility.TeeTraceInformation($"{nameof(BenchmarkConfig)} arguments");
Utility.TeeTraceInformation($"IsServerGC: {GCSettings.IsServerGC}");
Utility.TeeTraceInformation("--------------------------------------------------------------------- ");
Utility.TeeTraceInformation(JsonHelper.ToString(
input: this,
capacity: 2048));
Utility.TeeTraceInformation("--------------------------------------------------------------------- ");
Utility.TeeTraceInformation(string.Empty);
}
}
internal static BenchmarkConfig From(string[] args)
{
BenchmarkConfig options = null;
Parser parser = new Parser((settings) =>
{
settings.CaseSensitive = false;
settings.HelpWriter = Console.Error;
settings.AutoHelp = true;
});
parser.ParseArguments<BenchmarkConfig>(args)
.WithParsed<BenchmarkConfig>(e => options = e)
.WithNotParsed<BenchmarkConfig>(e => BenchmarkConfig.HandleParseError(e));
if (options.PublishResults)
{
if (string.IsNullOrEmpty(options.ResultsContainer)
|| string.IsNullOrWhiteSpace(options.ResultsPartitionKeyValue)
|| string.IsNullOrWhiteSpace(options.CommitId)
|| string.IsNullOrWhiteSpace(options.CommitDate)
|| string.IsNullOrWhiteSpace(options.CommitTime))
{
throw new ArgumentException($"Missing either {nameof(options.ResultsContainer)} {nameof(options.ResultsPartitionKeyValue)} {nameof(options.CommitId)} {nameof(options.CommitDate)} {nameof(options.CommitTime)}");
}
}
return options;
}
/// <summary>
/// Give each workload a unique user agent string
/// so the backend logs can be filtered by the workload.
/// </summary>
private string GetUserAgentPrefix()
{
return this.WorkloadName ?? this.WorkloadType ?? BenchmarkConfig.UserAgentSuffix;
}
internal Microsoft.Azure.Cosmos.CosmosClient CreateCosmosClient()
{
// Overwrite the default timespan if configured
if(this.TelemetryScheduleInSec > 0)
{
ClientTelemetryOptions.DefaultIntervalForTelemetryJob = TimeSpan.FromSeconds(this.TelemetryScheduleInSec);
}
Microsoft.Azure.Cosmos.CosmosClientOptions clientOptions = new Microsoft.Azure.Cosmos.CosmosClientOptions()
{
ApplicationName = this.GetUserAgentPrefix(),
MaxRetryAttemptsOnRateLimitedRequests = 0,
MaxRequestsPerTcpConnection = this.MaxRequestsPerTcpConnection,
MaxTcpConnectionsPerEndpoint = this.MaxTcpConnectionsPerEndpoint,
ConnectionMode = (this.IsThinClientEnabled || this.IsGatewayModeEnabled) ? Microsoft.Azure.Cosmos.ConnectionMode.Gateway: Microsoft.Azure.Cosmos.ConnectionMode.Direct,
CosmosClientTelemetryOptions = new Microsoft.Azure.Cosmos.CosmosClientTelemetryOptions()
{
DisableSendingMetricsToService = !this.EnableTelemetry,
DisableDistributedTracing = !this.EnableDistributedTracing
},
};
if (!string.IsNullOrEmpty(this.ApplicationPreferredRegions))
{
clientOptions.ApplicationPreferredRegions = this.ApplicationPreferredRegions.Split(',')
.Select(region => region.Trim()).ToArray();
}
if (!string.IsNullOrWhiteSpace(this.ConsistencyLevel))
{
clientOptions.ConsistencyLevel = (Microsoft.Azure.Cosmos.ConsistencyLevel)Enum.Parse(typeof(Microsoft.Azure.Cosmos.ConsistencyLevel), this.ConsistencyLevel, ignoreCase: true);
}
return new Microsoft.Azure.Cosmos.CosmosClient(
this.EndPoint,
this.Key,
clientOptions);
}
internal DocumentClient CreateDocumentClient(string accountKey)
{
Microsoft.Azure.Documents.ConsistencyLevel? consistencyLevel = null;
if (!string.IsNullOrWhiteSpace(this.ConsistencyLevel))
{
consistencyLevel = (Microsoft.Azure.Documents.ConsistencyLevel)Enum.Parse(typeof(Microsoft.Azure.Documents.ConsistencyLevel), this.ConsistencyLevel, ignoreCase: true);
}
return new DocumentClient(new Uri(this.EndPoint),
accountKey,
new ConnectionPolicy()
{
ConnectionMode = Microsoft.Azure.Documents.Client.ConnectionMode.Direct,
ConnectionProtocol = Protocol.Tcp,
MaxRequestsPerTcpConnection = this.MaxRequestsPerTcpConnection,
MaxTcpConnectionsPerEndpoint = this.MaxTcpConnectionsPerEndpoint,
UserAgentSuffix = this.GetUserAgentPrefix(),
RetryOptions = new RetryOptions()
{
MaxRetryAttemptsOnThrottledRequests = 0
}
},
desiredConsistencyLevel: consistencyLevel);
}
private static void HandleParseError(IEnumerable<Error> errors)
{
using (ConsoleColorContext ct = new ConsoleColorContext(ConsoleColor.Red))
{
foreach (Error e in errors)
{
Trace.TraceInformation(e.ToString());
}
}
Environment.Exit(errors.Count());
}
}
}