Repository navigation
Expand file tree
/
Copy pathPerfCommand.cs
More file actions
167 lines (140 loc) · 5.94 KB
/
Copy pathPerfCommand.cs
File metadata and controls
167 lines (140 loc) · 5.94 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using DnsClient;
using Microsoft.Extensions.CommandLineUtils;
using Microsoft.Extensions.Logging;
namespace DigApp
{
public class PerfCommand : DnsCommand
{
private int _clients;
private string _query;
private int _runtime;
private long _reportExcecutions = 0;
private long _allExcecutions = 0;
private bool _running;
private LookupClientOptions _settings;
private LookupClient _lookup;
private int _errors;
private int _success;
private Spiner _spinner;
private bool _runSync;
public CommandOption ClientsArg { get; private set; }
public CommandArgument QueryArg { get; private set; }
public CommandOption RuntimeArg { get; private set; }
public CommandOption SyncArg { get; private set; }
public PerfCommand(CommandLineApplication app, ILoggerFactory loggerFactory, string[] originalArgs) : base(app, loggerFactory, originalArgs)
{
}
protected override void Configure()
{
QueryArg = App.Argument("query", "the domain query to run.", false);
ClientsArg = App.Option("-c | --clients", "Number of clients to run", CommandOptionType.SingleValue);
RuntimeArg = App.Option("-r | --runtime", "Time in seconds to run", CommandOptionType.SingleValue);
SyncArg = App.Option("--sync", "Run synchronous api", CommandOptionType.NoValue);
base.Configure();
}
protected override async Task<int> Execute()
{
_clients = ClientsArg.HasValue() ? int.Parse(ClientsArg.Value()) : 10;
_runtime = RuntimeArg.HasValue() ? int.Parse(RuntimeArg.Value()) <= 1 ? 5 : int.Parse(RuntimeArg.Value()) : 5;
_query = string.IsNullOrWhiteSpace(QueryArg.Value) ? string.Empty : QueryArg.Value;
_runSync = SyncArg.HasValue();
_settings = GetLookupSettings();
_settings.EnableAuditTrail = false;
_lookup = GetDnsLookup(_settings);
_running = true;
Console.WriteLine($"; <<>> Starting perf run with {_clients} clients running for {_runtime} seconds <<>>");
Console.WriteLine($"; ({_settings.NameServers.Count} Servers, caching:{_settings.UseCache}, minttl:{_settings.MinimumCacheTimeout?.TotalMilliseconds})");
_spinner = new Spiner();
_spinner.Start();
var sw = Stopwatch.StartNew();
var timeoutTask = Task.Delay(_runtime * 1000).ContinueWith((t) =>
{
_running = false;
});
var tasks = new List<Task>
{
timeoutTask
};
for (var clientIndex = 0; clientIndex < _clients; clientIndex++)
{
tasks.Add(ExcecuteRun());
}
tasks.Add(CollectPrint());
await Task.WhenAny(tasks.ToArray());
double elapsedSeconds = sw.ElapsedMilliseconds / 1000d;
// results
_spinner.Stop();
Console.WriteLine(string.Join("-", Enumerable.Repeat("-", 50)));
Console.WriteLine($";; results:\t\t");
Console.WriteLine(string.Join("-", Enumerable.Repeat("-", 50)));
Console.WriteLine($";; run for {elapsedSeconds}sec {_clients} clients.");
var successPercent = _errors == 0 ? 100 : _success == 0 ? 0 : (100 - ((double)_errors / (_success) * 100));
Console.WriteLine($";; {_errors:N0} errors {_success:N0} ok {successPercent:N2}% success.");
var execPerSec = _allExcecutions / elapsedSeconds;
Console.WriteLine($";; {execPerSec:N2} queries per second.");
return 0;
}
private async Task CollectPrint()
{
var waitCount = 0;
while (waitCount < _runtime)
{
waitCount++;
await Task.Delay(1000);
_spinner.Message = $"Requests per sec: {_reportExcecutions:N2}.";
Interlocked.Exchange(ref _reportExcecutions, 0);
}
_running = false;
}
private async Task ExcecuteRun()
{
//var swatch = Stopwatch.StartNew();
var options = GetLookupSettings();
options.EnableAuditTrail = false;
var lookup = GetDnsLookup(options);
while (_running)
{
try
{
IDnsQueryResponse response;
if (!_runSync)
{
response = await lookup.QueryAsync(_query, QueryType.A);
}
else
{
response = await Task.Run(() => lookup.Query(_query, QueryType.A));
await Task.Delay(0);
}
if (Logger.IsEnabled(LogLevel.Information))
{
Logger.LogInformation("Response received for {0} {1}", _query, response.Header);
}
Interlocked.Increment(ref _allExcecutions);
Interlocked.Increment(ref _reportExcecutions);
if (response.HasError)
{
Logger.LogWarning("Response has error.\n{0}", response.AuditTrail);
Interlocked.Increment(ref _errors);
}
else
{
Interlocked.Increment(ref _success);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Logger.LogError(101, ex, "Error running query {0}.", _query);
Interlocked.Increment(ref _errors);
}
}
}
}
}