-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathApacheBench.cs
428 lines (381 loc) · 15.3 KB
/
ApacheBench.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace ab_viz {
/// <summary>
/// Apache Bench
/// </summary>
public class ApacheBench : IDisposable {
public static readonly string APACHE_BENCH_EXE = "ab.exe";
public static readonly string APACHE_BENCH_FLAG_VERSION = "-V";
public static readonly string APACHE_BENCH_FLAG_REQUESTS = "-n";
public static readonly string APACHE_BENCH_FLAG_CONCURRENCY = "-c";
public static readonly string APACHE_BENCH_FLAG_TIMELIMIT = "-t";
public static readonly string APACHE_BENCH_FLAG_WINDOWSIZE = "-b";
public static readonly string APACHE_BENCH_FLAG_ADDRESS = "-B";
public static readonly string APACHE_BENCH_FLAG_POSTFILE = "-p";
public static readonly string APACHE_BENCH_FLAG_PUTFILE = "-u";
public static readonly string APACHE_BENCH_FLAG_CONTENT_TYPE = "-T";
public static readonly string APACHE_BENCH_FLAG_COOKIE = "-C";
public static readonly string APACHE_BENCH_FLAG_HEADER = "-H";
public static readonly string APACHE_BENCH_FLAG_AUTHENTICATE = "-A";
public static readonly string APACHE_BENCH_FLAG_PROXY_AUTHENTICATE = "-P";
public static readonly string APACHE_BENCH_FLAG_PROXY_SERVER = "-X";
public static readonly string APACHE_BENCH_FLAG_KEEP_ALIVE = "-k";
public static readonly string APACHE_BENCH_FLAG_USE_HEAD = "-i";
public static readonly string APACHE_BENCH_FLAG_GNUPLOT = "-g";
public static readonly string DEFAULT_GNUPLOT_FILE = "graph.tsv";
public static readonly string APACHE_BENCH_FLAG_PERCENTAGE_FILE = "-e";
public static readonly string DEFAULT_PERCENTAGE_FILE = "summary.csv";
#region Public Properties
private string _URL = null;
/// <summary>
/// Gets the URL.
/// </summary>
/// <value>
/// The URL.
/// </value>
public string URL {
get {
return _URL;
}
}
private List<KeyValuePair<string, string>> _Arguments = null;
/// <summary>
/// Gets the arguments.
/// </summary>
/// <value>
/// The arguments.
/// </value>
public List<KeyValuePair<string, string>> Arguments {
get {
return _Arguments;
}
}
/// <summary>
/// Process object
/// </summary>
private Process _Process;
/// <summary>
/// Process parameters object
/// </summary>
private ProcessStartInfo _ProcessStartInfo;
private string _StandardOutput = string.Empty;
/// <summary>
/// Gets the standard output.
/// </summary>
/// <value>
/// The standard output.
/// </value>
public string StandardOutput {
get {
return _StandardOutput;
}
}
private string _StandardError = string.Empty;
/// <summary>
/// Gets the standard error.
/// </summary>
/// <value>
/// The standard error.
/// </value>
public string StandardError {
get {
return _StandardError;
}
}
private bool _ProcessExited = false;
/// <summary>
/// Gets a value indicating whether this instance has exited.
/// </summary>
/// <value>
/// <c>true</c> if this instance has exited; otherwise, <c>false</c>.
/// </value>
public bool HasExited {
get {
return (null != _Process) ? _Process.HasExited & _ProcessExited : false;
}
}
private int _CompletedPercentage = 0;
/// <summary>
/// Gets the completed percentage.
/// </summary>
/// <value>
/// The completed percentage.
/// </value>
public int CompletedPercentage {
get {
return _CompletedPercentage;
}
}
#endregion
#region Public Events
/// <summary>
/// Send the progress percentage
/// </summary>
public event EventHandler<int> InProgress;
/// <summary>
/// Sends the new data that has been received on standard output
/// </summary>
public event EventHandler<string> DataReceived;
/// <summary>
/// Invoked when process has completed execution
/// </summary>
public event EventHandler Completed;
#endregion
/// <summary>
/// Monitors Standard Output stream
/// </summary>
private Task _UpdateStandardOutput = null;
/// <summary>
/// Monitors Standard Error stream
/// </summary>
private Task _UpdateStandardError = null;
/// <summary>
/// Cancellation token for monitoring tasks
/// </summary>
private CancellationTokenSource _CancellationToken = null;
/// <summary>
/// Gets the num of requests.
/// </summary>
/// <value>
/// The num of requests.
/// </value>
public int NumOfRequests {
get {
int req = 0;
var arg = GetValueFromArguments(_Arguments, APACHE_BENCH_FLAG_REQUESTS);
if (null != arg) {
int.TryParse(arg, out req);
}
return req;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="ApacheBench" /> class.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="arguments">The arguments.</param>
public ApacheBench(string url, params KeyValuePair<string, string>[] arguments)
: this (url, new List<KeyValuePair<string,string>>(arguments)) { }
/// <summary>
/// Initializes a new instance of the <see cref="ApacheBench" /> class.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="arguments">The arguments.</param>
public ApacheBench(string url, List<KeyValuePair<string, string>> arguments) {
this._URL = null != url ? FormatURL(url) : url;
this._Arguments = arguments ?? new List<KeyValuePair<string,string>>();
}
/// <summary>
/// Creates the process start info object.
/// </summary>
private void CreateProcessStartInfo() {
if (!string.IsNullOrEmpty(this._URL)) {
//Add gnuplot flag
_Arguments.Add(new KeyValuePair<string, string>(APACHE_BENCH_FLAG_GNUPLOT, DEFAULT_GNUPLOT_FILE));
//Add percentage csv flag
_Arguments.Add(new KeyValuePair<string, string>(APACHE_BENCH_FLAG_PERCENTAGE_FILE, DEFAULT_PERCENTAGE_FILE));
}
this._ProcessStartInfo = new ProcessStartInfo(APACHE_BENCH_EXE) {
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
Arguments = string.Format(" {0} {1} ",
string.Join(" ", this._Arguments.ConvertAll<string>(kv => kv.Key + (!string.IsNullOrEmpty(kv.Value) ? " " + kv.Value : string.Empty))),
this._URL)
};
}
/// <summary>
/// Starts this instance.
/// </summary>
public bool Start() {
this.CleanUp();
this.CreateProcessStartInfo();
this._Process = new Process();
this._Process.EnableRaisingEvents = true;
this._Process.Exited += _Process_Exited;
//this._Process.OutputDataReceived += this._Process_OutputDataReceived;
//this._Process.ErrorDataReceived += this._Process_ErrorDataReceived;
this._Process.StartInfo = this._ProcessStartInfo;
bool status = this._Process.Start();
this._UpdateStandardOutput = Task.Factory.StartNew(UpdateStandardOutput);
this._CancellationToken = new CancellationTokenSource();
this._UpdateStandardError = Task.Factory.StartNew(UpdateStandardError, _CancellationToken.Token);
return status;
}
/// <summary>
/// Handles the Exited event of the _Process control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
private void _Process_Exited(object sender, EventArgs e) {
if(null != this._UpdateStandardOutput && TaskStatus.Running == this._UpdateStandardOutput.Status) {
this._UpdateStandardOutput.Wait(1000);
}
if (null != this._Process && !this._Process.StandardOutput.EndOfStream) {
this._StandardOutput += this._Process.StandardOutput.ReadToEnd();
}
if (null != this._UpdateStandardError && TaskStatus.Running == this._UpdateStandardError.Status) {
this._UpdateStandardOutput.Wait(1000);
}
if (null != this._Process && !this._Process.StandardError.EndOfStream) {
this._StandardError += this._Process.StandardError.ReadToEnd();
}
}
/// <summary>
/// Waits for exit.
/// </summary>
public void WaitForExit() {
if (null != this._Process) {
this._Process.WaitForExit();
}
}
/// <summary>
/// Updates the Standard Output
/// </summary>
private void UpdateStandardOutput() {
while (!this._Process.HasExited) {
string line = this._Process.StandardOutput.ReadLine();
this._StandardOutput += line + Environment.NewLine;
if (null != this.DataReceived) {
this.DataReceived(this, line);
}
}
if (null != _CancellationToken) {
_CancellationToken.Cancel();
}
if (null != _Process && null != _Process.StandardOutput && !_Process.StandardOutput.EndOfStream) {
this._StandardOutput += this._Process.StandardOutput.ReadToEnd();
}
if (null != this.Completed) {
this.Completed(this, new EventArgs());
}
this._ProcessExited = true;
}
/// <summary>
/// Updates the standard error.
/// </summary>
private void UpdateStandardError() {
while (!this._Process.HasExited) {
try {
int percentage = 0;
string line = this._Process.StandardError.ReadLine();
this._StandardError += line + Environment.NewLine;
if (null != line) {
var number = Regex.Match(line, @"\d+").Value;
percentage = (int)((double.Parse(number) / (double)this.NumOfRequests) * (double)100);
}
if (null != this.InProgress) {
this.InProgress(this, percentage);
}
}
catch (TaskCanceledException) { }
catch { }
}
}
public void Cancel() {
if (null != _Process && false == _Process.HasExited) {
_Process.Kill();
}
}
/// <summary>
/// Runs the ApacheBench in sync.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="arguments">The arguments.</param>
/// <returns>The data on StandardOutput</returns>
public static string RunSync(string url, params KeyValuePair<string, string>[] arguments) {
string output = string.Empty;
using (var ab = new ApacheBench(url, arguments)) {
if (ab.Start()) {
while (!ab.HasExited) ;
output = ab.StandardOutput;
}
}
return output;
}
/// <summary>
/// Determines whether [is apache bench present].
/// </summary>
/// <returns>
/// <c>true</c> if [is apache bench present]; otherwise, <c>false</c>.
/// </returns>
public static bool IsApacheBenchPresent() {
return File.Exists(APACHE_BENCH_EXE);
}
/// <summary>
/// Gets the version.
/// </summary>
/// <returns></returns>
public static string GetVersion() {
string version = null;
if (IsApacheBenchPresent()) {
try {
var output = ApacheBench.RunSync(null, new KeyValuePair<string, string>(APACHE_BENCH_FLAG_VERSION, null));
if (!string.IsNullOrEmpty(output)) {
var lines = output.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
version = lines[0].Split(',')[1].Trim();
}
}
catch {
//Log error
}
}
return version;
}
/// <summary>
/// Formats the URL.
/// </summary>
/// <param name="url">The URL.</param>
/// <returns></returns>
public static string FormatURL(string url) {
Uri address = new Uri(url, UriKind.Absolute);
return address.AbsoluteUri;
}
/// <summary>
/// Gets the value from arguments.
/// </summary>
/// <param name="args">The args.</param>
/// <param name="key">The key.</param>
/// <returns></returns>
public static string GetValueFromArguments(List<KeyValuePair<string, string>> args, string key) {
foreach (var kv in args) {
if (kv.Key == key) {
return kv.Value;
}
}
return null;
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose() {
CleanUp();
}
/// <summary>
/// Frees resources allocated to this instance.
/// </summary>
private void CleanUp() {
if (null != _UpdateStandardOutput) {
_UpdateStandardOutput.Dispose();
_UpdateStandardOutput = null;
}
if (null != _UpdateStandardError) {
_UpdateStandardError.Dispose();
_UpdateStandardError = null;
}
if (null != _Process) {
_Process.Dispose();
_Process = null;
}
}
}
}