private static void DoWork(Action action, TimeSpan duration, int?count, ConcurrentQueue <WorkerThreadResult> results, Stopwatch sw, CancellationToken cancellationToken, ManualResetEventSlim resetEvent, int workerIndex) { var result = new WorkerThreadResult(); var sw2 = new Stopwatch(); var sw3 = new Stopwatch(); var current = 0; // To save memory we only track response times from the first 20 workers var trackResponseTime = workerIndex < 20; while (!cancellationToken.IsCancellationRequested && duration.TotalMilliseconds > sw.Elapsed.TotalMilliseconds && (!count.HasValue || current < count.Value)) { current++; try { sw2.Restart(); action.Invoke(); result.Add((int)(sw.ElapsedTicks / Stopwatch.Frequency), (float)sw2.ElapsedTicks / Stopwatch.Frequency * 1000, trackResponseTime); } catch (Exception ex) { result.AddError((int)(sw.ElapsedTicks / Stopwatch.Frequency), (float)sw2.ElapsedTicks / Stopwatch.Frequency * 1000, ex); } } results.Enqueue(result); resetEvent.Set(); }
public WorkerThreadResults() { _result = new WorkerThreadResult(); var socketWorker = new SocketWorkerJob(new Uri("http://localhost:5000")); _worker = socketWorker.Init(0, _result).Result; }
private async Task DoWork_Duration(TimeSpan duration, Stopwatch sw, ConcurrentQueue <WorkerThreadResult> results, CancellationToken cancellationToken, ManualResetEventSlim resetEvent, int workerIndex) { IWorkerJob job; var workerThreadResult = new WorkerThreadResult(); try { job = await _workerJob.Init(workerIndex, workerThreadResult); } catch (Exception) { workerThreadResult.AddError((int)sw.ElapsedMilliseconds, 0, false); results.Enqueue(workerThreadResult); resetEvent.Set(); return; } while (!cancellationToken.IsCancellationRequested && duration.TotalMilliseconds > sw.Elapsed.TotalMilliseconds) { try { await job.DoWork(); } catch (Exception) { workerThreadResult.AddError((int)sw.ElapsedMilliseconds, 0, false); } } results.Enqueue(job.GetResults()); resetEvent.Set(); }
private SocketWorkerJob(int index, Uri uri, WorkerThreadResult workerThreadResult) { _index = index; _uri = uri; _stopwatch = new Stopwatch(); _stopwatch.Start(); _localStopwatch = new Stopwatch(); _workerThreadResult = workerThreadResult; IPAddress ip; if (_uri.HostNameType == UriHostNameType.Dns) { var host = Dns.GetHostEntry(_uri.Host); ip = host.AddressList.First(i => i.AddressFamily == AddressFamily.InterNetwork); } else { ip = IPAddress.Parse(_uri.Host); } var endPoint = new IPEndPoint(ip, _uri.Port); _httpWorker = new HttpWorker(new HttpWorkerClient(endPoint, uri), uri); }
public TrackedApiClient(int index, WorkerThreadResult workerThreadResult, string baseAddress, ApiContext context = null, string defaultQueryString = null) : base(baseAddress, context, defaultQueryString) { _index = index; _workerThreadResult = workerThreadResult; _baseAddress = baseAddress; _stopwatch = new Stopwatch(); //_stopwatch.Start(); _localStopwatch = new Stopwatch(); }
private SocketWorkerJob(int index, Uri uri, WorkerThreadResult workerThreadResult) { _index = index; _uri = uri; _stopwatch = Stopwatch.StartNew(); _localStopwatch = new Stopwatch(); _workerThreadResult = workerThreadResult; _httpWorker = new HttpWorker(new HttpWorkerClient(uri), uri); }
private GameClientWorkerJob(string baseAddress, int index, WorkerThreadResult workerThreadResult, IRequestSequence requestSequence, TrackedApiClient client) { _baseAddress = baseAddress; _index = index; _workerThreadResult = workerThreadResult; _requestSequence = requestSequence; _client = client; //_gameRequestSequence = new GameRequestSequence(_baseAddress, _index, _workerThreadResult); }
private HttpClientWorkerJob(int index, Uri uri, WorkerThreadResult workerThreadResult) { _index = index; _uri = uri; _stopwatch = new Stopwatch(); _stopwatch.Start(); _localStopwatch = new Stopwatch(); _workerThreadResult = workerThreadResult; _httpClient = new HttpClient(); }
private SocketWorkerJob(int index, Uri uri, Dictionary <string, string> headers, WorkerThreadResult workerThreadResult) { _index = index; _uri = uri; _headers = headers; _stopwatch = Stopwatch.StartNew(); _localStopwatch = new Stopwatch(); _workerThreadResult = workerThreadResult; _httpWorker = new HttpWorker(new HttpWorkerClient(uri), uri, headers); }
private async Task DoWork_Count(int count, ConcurrentQueue <WorkerThreadResult> results, CancellationToken cancellationToken, ManualResetEventSlim resetEvent, int workerIndex) { var workerThreadResult = new WorkerThreadResult(); var job = await _workerJob.Init(workerIndex, workerThreadResult); for (var i = 0; i < count && !cancellationToken.IsCancellationRequested; i++) { await job.DoWork(); } results.Enqueue(job.GetResults()); resetEvent.Set(); }
private HttpClientWorkerJob(int index, Uri uri, WorkerThreadResult workerThreadResult) { _index = index; _uri = uri; _stopwatch = Stopwatch.StartNew(); _localStopwatch = new Stopwatch(); _workerThreadResult = workerThreadResult; // ignore server certificate var handler = new HttpClientHandler() { ServerCertificateCustomValidationCallback = (a, b, c, d) => true }; _httpClient = new HttpClient(handler); }
public static void Init(int index, WorkerThreadResult workerThreadResult) { if (lazy.Value != null) { var plgs = lazy.Value.Plgs; if (plgs == null) { return; } foreach (var item in plgs) { item.Value.Init(index, workerThreadResult); } } }
public LoginUserTest(int index, WorkerThreadResult workerThreadResult) { _index = index; _stopwatch = Stopwatch.StartNew(); _localStopwatch = new Stopwatch(); _workerThreadResult = workerThreadResult; client = new RestClient(Url); param = new List <LoginRequest> { new LoginRequest() { Username = "******", Password = "******" }, new LoginRequest() { Username = "******", Password = "******" }, }; }
public static async Task <TrackedApiClient> NewGuestAsync(int index, WorkerThreadResult workerThreadResult, string baseAddress) { ApiClient.SerializationModel = Generator.Generate(new[] { typeof(UserLoginResponse).GetTypeInfo().Assembly, typeof(Picasso.Common.PicassoDto).GetTypeInfo().Assembly }); var unauthenticated = new TrackedApiClient(index, workerThreadResult, baseAddress, null); var res = await unauthenticated.PostAsync <UserLoginResponse>("users", new UserCreateRequest { AutoGenerateName = true, SsoToken = Guid.NewGuid().ToString("n"), }); res.AssertSuccessful(); return(new TrackedApiClient(index, workerThreadResult, baseAddress, new ApiContext { SessionToken = res.Data.SecretKey })); }
public Task <IWorkerJob> Init(int index, WorkerThreadResult workerThreadResult) { _client = new TrackedApiClient(index, workerThreadResult, _baseAddress); _requestSequence.Register(_client); return(Task.FromResult <IWorkerJob>(new GameClientWorkerJob(_baseAddress, index, workerThreadResult, _requestSequence, _client))); }
protected GameRequestSequence(string baseAddress, int index, WorkerThreadResult workerThreadResult) { _baseAddress = baseAddress; _index = index; _workerThreadResult = workerThreadResult; }
public Task <IWorkerJob> Init(int index, WorkerThreadResult workerThreadResult) { return(Task.FromResult <IWorkerJob>(new SocketWorkerJob(index, _uri, workerThreadResult))); }
private static void DoWork(Uri uri, TimeSpan duration, int?count, int pipelining, ConcurrentQueue <WorkerThreadResult> results, Stopwatch sw, CancellationToken cancellationToken, ManualResetEventSlim resetEvent, int workerIndex, string requestString, string body) { var result = new WorkerThreadResult(); var sw2 = new Stopwatch(); var sw3 = new Stopwatch(); byte[] bodyByteArray = null; byte[] requestByteArray = null; if (!string.IsNullOrEmpty(requestString)) { requestByteArray = Encoding.UTF8.GetBytes(requestString); } if (!string.IsNullOrEmpty(body)) { bodyByteArray = Encoding.UTF8.GetBytes(body); } var worker = new HttpWorker(uri, request: requestByteArray, data: bodyByteArray); var current = 0; // To save memory we only track response times from the first 20 workers var trackResponseTime = workerIndex < 20; // Priming connection ... if (!count.HasValue) { try { int tmpStatusCode; if (pipelining > 1) { worker.WritePipelined(pipelining); worker.Flush(); for (var j = 0; j < pipelining; j++) { worker.ReadPipelined(out tmpStatusCode); } } else { worker.Write(); worker.Flush(); worker.Read(out tmpStatusCode); } } catch (Exception ex) { } } if (pipelining == 1) { while (!cancellationToken.IsCancellationRequested && duration.TotalMilliseconds > sw.Elapsed.TotalMilliseconds && (!count.HasValue || current < count.Value)) { current++; try { sw2.Restart(); worker.Write(); worker.Flush(); int statusCode; var length = worker.Read(out statusCode); result.Add((int)(sw.ElapsedTicks / Stopwatch.Frequency), length, (float)sw2.ElapsedTicks / Stopwatch.Frequency * 1000, statusCode, trackResponseTime); } catch (Exception ex) { result.AddError((int)(sw.ElapsedTicks / Stopwatch.Frequency), (float)sw2.ElapsedTicks / Stopwatch.Frequency * 1000, ex); } } } else { try { sw2.Restart(); worker.WritePipelined(pipelining); worker.Flush(); } catch (Exception ex) { result.AddError((int)(sw.ElapsedTicks / Stopwatch.Frequency), (float)sw2.ElapsedTicks / Stopwatch.Frequency * 1000, ex); } while (!cancellationToken.IsCancellationRequested && duration.TotalMilliseconds > sw.Elapsed.TotalMilliseconds) { try { for (var j = 0; j < pipelining; j++) { int statusCode; var length = worker.ReadPipelined(out statusCode); result.Add((int)Math.Floor((float)sw.ElapsedTicks / Stopwatch.Frequency), length, (float)sw2.ElapsedTicks / Stopwatch.Frequency * 1000, statusCode, trackResponseTime); if (j == 0 && !cancellationToken.IsCancellationRequested && duration.TotalMilliseconds > sw.Elapsed.TotalMilliseconds) { sw3.Restart(); worker.WritePipelined(pipelining); worker.Flush(); } } var tmp = sw2; sw2 = sw3; sw3 = tmp; } catch (Exception ex) { result.AddError((int)(sw.ElapsedTicks / Stopwatch.Frequency), (float)sw2.ElapsedTicks / Stopwatch.Frequency * 1000, ex); } } } results.Enqueue(result); resetEvent.Set(); }
public ValueTask <IWorkerJob> Init(int index, WorkerThreadResult workerThreadResult) { return(new ValueTask <IWorkerJob>(new SocketWorkerJob(index, _uri, workerThreadResult))); }
public ValueTask <IWorkerJob> Init(int index, WorkerThreadResult workerThreadResult) { return(new ValueTask <IWorkerJob>(new LoginUserTest(index, workerThreadResult))); }
public ValueTask <IWorkerJob> Init(int index, WorkerThreadResult workerThreadResult) { return(new ValueTask <IWorkerJob>(new HttpClientWorkerJob(index, _uri, _headers, workerThreadResult))); }