Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal class Program
- {
- private const int totalRequests = 800;
- private static void Main(string[] args)
- {
- var cancellationTockenSource = new CancellationTokenSource();
- LoadTest(cancellationTockenSource);
- Console.WriteLine("Press any key to break");
- Console.ReadLine();
- cancellationTockenSource.Cancel();
- }
- private static async Task LoadTest(CancellationTokenSource cancellationTockenSource)
- {
- Stopwatch stopWatch = Stopwatch.StartNew();
- var requests = new List<Task>(totalRequests);
- for (int i = 0; i < totalRequests; ++i)
- {
- requests.Add(Request(cancellationTockenSource.Token));
- }
- try
- {
- await Task.WhenAll(requests);
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- }
- stopWatch.Stop();
- Console.WriteLine(string.Format("{0} req/sec", stopWatch.Elapsed.TotalSeconds/totalRequests));
- }
- private static HttpRequestMessage CreateMessage()
- {
- var url = new Uri("http://ya.ru:8080/x");
- var message = new HttpRequestMessage(HttpMethod.Get, url);
- message.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
- return message;
- }
- protected static async Task<string> Request(CancellationToken token)
- {
- using (var client = new HttpClient())
- {
- HttpResponseMessage response = await client.SendAsync(CreateMessage(), token);
- string content = await response.Content.ReadAsStringAsync();
- return content;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment