From 091f18c870199da6371a3aced1646e5ab3607c3d Mon Sep 17 00:00:00 2001 From: Nastro_ Date: Fri, 21 Mar 2025 09:22:22 +0100 Subject: [PATCH] added threading --- LoadTest/Program.cs | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/LoadTest/Program.cs b/LoadTest/Program.cs index c517f7c..4f056e3 100644 --- a/LoadTest/Program.cs +++ b/LoadTest/Program.cs @@ -19,8 +19,9 @@ internal class Program string? token = null; string? methodHttp = null; string? time; - long? lTime; + long lTime; string? yn; + int nThreads; Stopwatch sw; using var client = new HttpClient(); @@ -85,18 +86,27 @@ internal class Program } while (string.IsNullOrEmpty(time)); lTime = long.Parse(time) * 1000; - + + do + { + Console.WriteLine("Insert Number of Threads: "); + nThreads = int.Parse(Console.ReadLine() ?? string.Empty); + } while (nThreads <= 0); + switch (methodHttp.ToUpper()) { case "GET": - sw = Stopwatch.StartNew(); - - while (sw.ElapsedMilliseconds < lTime) + int nThread = 1; + while (nThreads != 0) { - Console.WriteLine("GET " + url + $" [{sw.ElapsedMilliseconds / 1000.0} s]"); - client.GetAsync(url); + Thread thread = new Thread(() => { Get(url, lTime); }); + thread.Name = nThread.ToString(); + thread.Start(); + Console.WriteLine($"Thread {thread.Name} started."); + nThreads--; + nThread++; } - sw.Stop(); + break; case "POST": if (!string.IsNullOrEmpty(body)) @@ -112,4 +122,17 @@ internal class Program break; } } -} \ No newline at end of file + + private static void Get(string url, long lTime) + { + using var client = new HttpClient(); + Stopwatch sw = Stopwatch.StartNew(); + while (sw.ElapsedMilliseconds < lTime) + { + Console.WriteLine("GET " + url + $" [{sw.ElapsedMilliseconds / 1000.0} s]" + $"[# {Thread.CurrentThread.Name}]"); + var response = client.GetAsync(url); + Console.WriteLine(response.Result.StatusCode); + } + sw.Stop(); + } +}