public async Task <object> CreateResponseBenchmark([FromQuery] string formId, [FromQuery] string incorrectRatio) { bool formIdParseSuccess = ObjectId.TryParse(formId, out ObjectId formIdObject); if (!formIdParseSuccess) { return(BadRequest(new { success = false, message = "Invalid Form Id" })); } bool incorrectRatioParseSuccess = float.TryParse(incorrectRatio, out float incorrectRatioValue); if (!incorrectRatioParseSuccess) { incorrectRatioValue = 0; } try { FormResponseModel formResponse = await $"{Constants.BASE_URL}" .AppendPathSegment("form") .AppendPathSegment($"{formId}") .GetJsonAsync <FormResponseModel>(); NewResponseViewModel newResponse = responseGenerator.GenerateRandomResponse( formResponse.form, incorrectRatioValue); var watch = Stopwatch.StartNew(); ResponseModel response = await $"{Constants.BASE_URL}" .AppendPathSegment("response") .SetQueryParams(new { formId }) .PostJsonAsync(newResponse) .ReceiveJson <ResponseModel>(); watch.Stop(); return(Ok(new { success = true, timeElapsed = watch.ElapsedMilliseconds, response })); } catch (Exception e) { Console.WriteLine(e.Message); return(new JsonResult(new { success = false, message = e.Message })); } }
public async Task <object> CreateFormBenchmark([FromQuery] string fieldCount, [FromQuery] string totalForms) { bool isValidFieldCount = int.TryParse(fieldCount, out int fieldCountValue); if (!isValidFieldCount) { return(BadRequest(new { success = false, message = "Invalid Field Count" })); } bool isValidTotalForms = int.TryParse(totalForms, out int totalFormsValue); if (!isValidTotalForms) { totalFormsValue = 1; } List <FormBenchmarkModel> forms = new List <FormBenchmarkModel>(); Stopwatch watch; long totalTime = 0; try { for (int i = 0; i < totalFormsValue; i++) { NewFormViewModel newForm = formGenerator.GenerateRandomForm(fieldCountValue); watch = Stopwatch.StartNew(); FormResponseModel formResponse = await $"{Constants.BASE_URL}" .AppendPathSegment("form") .PostJsonAsync(newForm) .ReceiveJson <FormResponseModel>(); watch.Stop(); forms.Add(new FormBenchmarkModel { formId = formResponse.form.Id, timeElapsed = watch.ElapsedMilliseconds }); totalTime += watch.ElapsedMilliseconds; } return(Ok(new { success = true, totalFormsCreated = forms.Count, totalTime, averageTime = totalTime / forms.Count, forms })); } catch (Exception e) { Console.WriteLine(e.Message); return(new JsonResult(new { success = false, totalFormsCreated = forms.Count, forms, message = e.Message })); } }