-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathDFrameClient.cs
302 lines (258 loc) · 10.3 KB
/
DFrameClient.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
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
using System.Net.Http.Json;
using System.Runtime.Serialization;
using UnitGenerator;
namespace DFrame.RestSdk;
public class DFrameClient
{
readonly HttpClient httpClient;
readonly string rootAddress;
string? urlIsRunning;
string? urlConnections;
string? urlLatestResult;
string? urlResultsCount;
string? urlResultsList;
string? urlCancel;
string? urlRequest;
string? urlRepeat;
string? urlDuration;
string? urlInfinite;
public DFrameClient(string rootAddress)
: this(new HttpClient(), rootAddress)
{
}
public DFrameClient(HttpClient httpClient, string rootAddress)
{
this.httpClient = httpClient;
this.rootAddress = rootAddress.TrimEnd('/');
}
// Get Apis
public async Task<int> GetConnectionCountAsync(CancellationToken cancellationToken = default)
{
var response = await httpClient.GetAsync(urlConnections ??= $"{rootAddress}/api/connections", cancellationToken);
return await response.EnsureSuccessStatusCode().Content.ReadFromJsonAsync<int>();
}
public async Task<bool> IsRunningAsync(CancellationToken cancellationToken = default)
{
var response = await httpClient.GetAsync(urlIsRunning ??= $"{rootAddress}/api/isrunning", cancellationToken);
return await response.EnsureSuccessStatusCode().Content.ReadFromJsonAsync<bool>();
}
public async Task<ExecutionResult?> GetLatestResultAsync(CancellationToken cancellationToken = default)
{
var response = await httpClient.GetAsync(urlLatestResult ??= $"{rootAddress}/api/latestresult", cancellationToken);
return await response.EnsureSuccessStatusCode().Content.ReadFromJsonAsync<ExecutionResult>();
}
public async Task<int> GetResultsCountAsync(CancellationToken cancellationToken = default)
{
var response = await httpClient.GetAsync(urlResultsCount ??= $"{rootAddress}/api/resultscount", cancellationToken);
return await response.EnsureSuccessStatusCode().Content.ReadFromJsonAsync<int>();
}
public async Task<ExecutionSummary[]> GetResultsListAsync(CancellationToken cancellationToken = default)
{
var response = await httpClient.GetAsync(urlResultsList ??= $"{rootAddress}/api/resultslist", cancellationToken);
return (await response.EnsureSuccessStatusCode().Content.ReadFromJsonAsync<ExecutionSummary[]>())!;
}
public async Task<ExecutionResult?> GetResultAsync(ExecutionId executionId, CancellationToken cancellationToken = default)
{
var response = await httpClient.GetAsync($"{rootAddress}/api/getresult?executionId={executionId}", cancellationToken);
return (await response.EnsureSuccessStatusCode().Content.ReadFromJsonAsync<ExecutionResult?>());
}
// Post Apis
public async Task CancelAsync(CancellationToken cancellationToken = default)
{
var response = await httpClient.PostAsync(urlCancel ??= $"{rootAddress}/api/cancel", null, cancellationToken);
response.EnsureSuccessStatusCode();
}
public async Task<ExecutionSummary> ExecuteRequestAsync(RequestBody body, CancellationToken cancellationToken = default)
{
var response = await httpClient.PostAsJsonAsync(urlRequest ??= $"{rootAddress}/api/request", body, cancellationToken);
return (await response.EnsureSuccessStatusCode().Content.ReadFromJsonAsync<ExecutionSummary>())!;
}
public async Task<ExecutionSummary> ExecuteRepeatAsync(RepeatBody body, CancellationToken cancellationToken = default)
{
var response = await httpClient.PostAsJsonAsync(urlRepeat ??= $"{rootAddress}/api/repeat", body, cancellationToken);
return (await response.EnsureSuccessStatusCode().Content.ReadFromJsonAsync<ExecutionSummary>())!;
}
public async Task<ExecutionSummary> ExecuteDurationAsync(DurationBody body, CancellationToken cancellationToken = default)
{
var response = await httpClient.PostAsJsonAsync(urlDuration ??= $"{rootAddress}/api/duration", body, cancellationToken);
return (await response.EnsureSuccessStatusCode().Content.ReadFromJsonAsync<ExecutionSummary>())!;
}
public async Task<ExecutionSummary> ExecuteInfiniteAsync(InfiniteBody body, CancellationToken cancellationToken = default)
{
var response = await httpClient.PostAsJsonAsync(urlInfinite ??= $"{rootAddress}/api/infinite", body, cancellationToken);
return (await response.EnsureSuccessStatusCode().Content.ReadFromJsonAsync<ExecutionSummary>())!;
}
// Utils
/// <summary>
/// pollingSpan: 5 seconds.
/// </summary>
public Task WaitUntilCanExecute(CancellationToken cancellationToken = default)
{
return WaitUntilCanExecute(TimeSpan.FromSeconds(5), cancellationToken);
}
public async Task WaitUntilCanExecute(TimeSpan pollingSpan, CancellationToken cancellationToken = default)
{
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
var isRunning = await IsRunningAsync(cancellationToken);
if (!isRunning) return; // ok, can execute.
await Task.Delay(pollingSpan, cancellationToken);
}
}
}
public class ExecutionResult
{
public ExecutionSummary Summary { get; set; } = default!;
public SummarizedExecutionResult[] Results { get; set; } = default!;
}
public class RequestBody
{
public string Workload { get; set; } = default!;
public Dictionary<string, string?>? Parameters { get; set; }
public int Concurrency { get; set; }
public int? Workerlimit { get; set; }
public long TotalRequest { get; set; }
}
public class RepeatBody
{
public string Workload { get; set; } = default!;
public Dictionary<string, string?>? Parameters { get; set; }
public int Concurrency { get; set; }
public int? Workerlimit { get; set; }
public long TotalRequest { get; set; }
public int IncreaseTotalRequest { get; set; }
public int IncreaseTotalWorker { get; set; }
public int RepeatCount { get; set; }
}
public class DurationBody
{
public string Workload { get; set; } = default!;
public Dictionary<string, string?>? Parameters { get; set; }
public int Concurrency { get; set; }
public int? Workerlimit { get; set; }
public int ExecuteTimeSeconds { get; set; }
}
public class InfiniteBody
{
public string Workload { get; set; } = default!;
public Dictionary<string, string?>? Parameters { get; set; }
public int Concurrency { get; set; }
public int? Workerlimit { get; set; }
}
[DataContract]
public class ExecutionSummary
{
// inits
[DataMember]
public string Workload { get; set; } = default!;
[DataMember]
public ExecutionId ExecutionId { get; set; }
[DataMember]
public int WorkerCount { get; set; }
[DataMember]
public int WorkloadCount { get; set; }
[DataMember]
public int Concurrency { get; set; }
[DataMember]
public Dictionary<string, string> Parameters { get; set; } = default!;
[DataMember]
public DateTime StartTime { get; set; }
// modifiable
[DataMember]
public TimeSpan? RunningTime { get; set; }
[DataMember]
public long TotalRequest { get; set; } // set in init and after completed
[DataMember]
public long? SucceedSum { get; set; }
[DataMember]
public long? ErrorSum { get; set; }
[DataMember]
public double? RpsSum { get; set; }
}
public enum ExecutionStatus
{
Running,
Succeed,
Failed,
// Canceled
}
[DataContract]
public class SummarizedExecutionResult
{
[DataMember]
DateTime? ExecuteBegin { get; set; }
[DataMember]
DateTime? ExecuteCompleted { get; set; }
[DataMember]
public WorkerId WorkerId { get; set; }
[DataMember]
public int WorkloadCount { get; set; }
[DataMember]
public Dictionary<string, string> Metadata { get; set; }
[DataMember]
public Dictionary<WorkloadId, Dictionary<string, string>?>? Results { get; set; }
[DataMember]
public ExecutionStatus ExecutionStatus { get; set; }
[DataMember]
public bool Error { get; set; }
[DataMember]
public string? ErrorMessage { get; set; }
[DataMember]
public long CompleteCount { get; set; }
[DataMember]
public long SucceedCount { get; set; }
[DataMember]
public long ErrorCount { get; set; }
[DataMember]
public TimeSpan TotalElapsed { get; set; }
[DataMember]
public TimeSpan Latest { get; set; }
[DataMember]
public TimeSpan Min { get; set; }
[DataMember]
public TimeSpan Max { get; set; }
// Calc from elapsedValues when completed.
[DataMember]
public TimeSpan? Median { get; set; }
[DataMember]
public TimeSpan? Percentile90 { get; set; }
[DataMember]
public TimeSpan? Percentile95 { get; set; }
[IgnoreDataMember]
public TimeSpan Avg => (SucceedCount == 0) ? TimeSpan.Zero : TimeSpan.FromTicks(TotalElapsed.Ticks / SucceedCount);
[IgnoreDataMember]
public double Rps => (TotalElapsed.TotalSeconds == 0 || (ExecuteBegin == null)) ? 0 : (SucceedCount / RunningTime.TotalSeconds);
[IgnoreDataMember]
public TimeSpan RunningTime
{
get
{
if (ExecuteBegin == null)
{
return TimeSpan.Zero;
}
if (ExecuteCompleted == null)
{
return DateTime.UtcNow - ExecuteBegin.Value;
}
return ExecuteCompleted.Value - ExecuteBegin.Value;
}
}
// for serialize.
public SummarizedExecutionResult()
{
Metadata = new(0);
}
}
internal static class GenerateOptions
{
internal const UnitGenerateOptions Guid = UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.ParseMethod | UnitGenerateOptions.Comparable | UnitGenerateOptions.WithoutComparisonOperator;
internal const UnitGenerateOptions GuidJson = UnitGenerateOptions.MessagePackFormatter | UnitGenerateOptions.ParseMethod | UnitGenerateOptions.Comparable | UnitGenerateOptions.WithoutComparisonOperator | UnitGenerateOptions.JsonConverter | UnitGenerateOptions.JsonConverterDictionaryKeySupport;
}
[UnitOf(typeof(Guid), GenerateOptions.GuidJson)]
public readonly partial struct ExecutionId { }
[UnitOf(typeof(Guid), GenerateOptions.GuidJson)]
public readonly partial struct WorkerId { }
[UnitOf(typeof(Guid), GenerateOptions.GuidJson)]
public readonly partial struct WorkloadId { }