public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "hellocities")] HttpRequest req, [DurableClient] IDurableClient client, ILogger log) { string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); int? numberOrchestrations = string.IsNullOrEmpty(requestBody) || requestBody == "null" ? null : (int?) JsonConvert.DeserializeObject<int>(requestBody); TimeSpan timeout = TimeSpan.FromSeconds(200); if (!numberOrchestrations.HasValue) { // we are running a single orchestration. string orchestrationInstanceId = await client.StartNewAsync(nameof(HelloSequence)); var response = await client.WaitForCompletionOrCreateCheckStatusResponseAsync(req, orchestrationInstanceId, timeout); return response; } else { // call several orchestrations in a loop and wait for all of them to complete var testname = Util.MakeTestName(req); try { log.LogWarning($"Starting {testname} {numberOrchestrations.Value}..."); var stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); var tasks = new List<Task<bool>>(); async Task<bool> RunOrchestration(int iteration) { var startTime = DateTime.UtcNow; var orchestrationInstanceId = $"Orch{iteration}"; log.LogInformation($"{testname} starting {orchestrationInstanceId}"); await client.StartNewAsync(nameof(HelloSequence), orchestrationInstanceId); await client.WaitForCompletionOrCreateCheckStatusResponseAsync(req, orchestrationInstanceId, timeout); if (DateTime.UtcNow < startTime + timeout) { log.LogInformation($"{testname} completed {orchestrationInstanceId}"); return true; } else { log.LogInformation($"{testname} timeout {orchestrationInstanceId}"); return false; } } for (int i = 0; i < numberOrchestrations; i++) { tasks.Add(RunOrchestration(i)); } await Task.WhenAll(tasks); stopwatch.Stop(); int timeouts = tasks.Count(t => !t.Result); double elapsedSeconds = elapsedSeconds = stopwatch.Elapsed.TotalSeconds; log.LogWarning($"Completed {testname} with {timeouts} timeouts in {elapsedSeconds}s."); object resultObject = timeouts > 0 ? (object)new { testname, timeouts } : new { testname, elapsedSeconds }; string resultString = $"{JsonConvert.SerializeObject(resultObject, Formatting.None)}\n"; return new OkObjectResult(resultString); } catch (Exception e) { return new ObjectResult( new { testname, error = e.ToString(), }); } } }