private async void AssignProbe(SimulatedUser user) { if (user.bHasFinishedWork) { return; } user.NumberOfConcurrentUsers = SimulatedUserList.Where(x => x.bHasStartedWork && !x.bHasFinishedWork).Count(); EndpointProbe probe = TestSchedule.GetNextProbe(); await Task.Run(async() => { if (probe != null) { if (user.bAsyncUser) { AssignProbe(user); } ProbeResult probeResult = await user.ExecuteProbe(probe); ResultSet.Add(probeResult); RaiseNewFreeUser(user); } }); }
public async Task <ProbeResult> ExecuteProbe(EndpointProbe probe) { string payloadString = probe.Payload?.GetType() == typeof(JObject) ? JsonConvert.SerializeObject(probe.Payload) : probe.Payload?.ToString() ?? string.Empty; HttpRequestMessage request = new HttpRequestMessage { Method = probe.Method, Content = new StringContent( payloadString, Encoding.UTF8, "application/json") }; try { Stopwatch timer = Stopwatch.StartNew(); HttpResponseMessage response = TargetAPI.SendAsync(request).Result; timer.Stop(); return(new ProbeResult { Endpoint = probe.Endpoint, Method = probe.Method.Method, RequestPayload = request.Content.ReadAsStringAsync().Result, UserID = UserID, ConcurrentUsersAtRequestTime = NumberOfConcurrentUsers, bSuccessResponse = response.IsSuccessStatusCode ? true : false, StatusCode = (int)response.StatusCode, StatusMessage = response.StatusCode.ToString(), RoundTripTime = timer.ElapsedMilliseconds }); } catch (TaskCanceledException) { return(new ProbeResult { Endpoint = probe.Endpoint, Method = probe.Method.Method, RequestPayload = request.Content.ReadAsStringAsync().Result, UserID = UserID, ConcurrentUsersAtRequestTime = NumberOfConcurrentUsers, bRequestTimeout = true, StatusMessage = $"Client-side request timeout (longer than {Defaults.RequestTimeoutInSeconds})" }); } finally { ++Progress; } }
public EndpointProbe GetNextProbe() { if (HasBeenCompleted) { return(null); } lock (LockObject) { EndpointProbe probe = EndpointProbeList[ProgrammeOfWork[Progress]]; ++Progress; return(probe); } }