Last active
August 18, 2017 13:47
-
-
Save PureKrome/68e723291fac392d28511cb87a34e754 to your computer and use it in GitHub Desktop.
Testing a slow request (like a bad performing DB query)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Mvc; | |
namespace wwwlearn.Controllers | |
{ | |
[Route("")] | |
public class ValuesController : Controller | |
{ | |
// GET api/values | |
[HttpGet("test1")] | |
public IEnumerable<string> Get() | |
{ | |
return new string[] { "value1", "value2" }; | |
} | |
[HttpGet("slowtest/")] | |
[HttpGet("slowtest/{delayTimeInSeconds:int}")] | |
public async Task<string> Get(CancellationToken cancellationToken, | |
int delayTimeInSeconds = 10) | |
{ | |
var stopwatch = Stopwatch.StartNew(); | |
var message = "Starting to do slow work"; | |
await Task.Run(async () => | |
{ | |
await Task.Delay(delayTimeInSeconds * 1000, cancellationToken); | |
message = $"Finished slow delay of {delayTimeInSeconds} seconds."; | |
}, cancellationToken); | |
stopwatch.Stop(); | |
if (cancellationToken.IsCancellationRequested) | |
{ | |
message = $"request was cancelled after {stopwatch.ElapsedMilliseconds:N0} milliseconds"; | |
} | |
return message; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment