added threading

This commit is contained in:
2025-03-21 09:22:22 +01:00
parent 163bbc72c3
commit 091f18c870

View File

@@ -19,8 +19,9 @@ internal class Program
string? token = null; string? token = null;
string? methodHttp = null; string? methodHttp = null;
string? time; string? time;
long? lTime; long lTime;
string? yn; string? yn;
int nThreads;
Stopwatch sw; Stopwatch sw;
using var client = new HttpClient(); using var client = new HttpClient();
@@ -85,18 +86,27 @@ internal class Program
} while (string.IsNullOrEmpty(time)); } while (string.IsNullOrEmpty(time));
lTime = long.Parse(time) * 1000; 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()) switch (methodHttp.ToUpper())
{ {
case "GET": case "GET":
sw = Stopwatch.StartNew(); int nThread = 1;
while (nThreads != 0)
while (sw.ElapsedMilliseconds < lTime)
{ {
Console.WriteLine("GET " + url + $" [{sw.ElapsedMilliseconds / 1000.0} s]"); Thread thread = new Thread(() => { Get(url, lTime); });
client.GetAsync(url); thread.Name = nThread.ToString();
thread.Start();
Console.WriteLine($"Thread {thread.Name} started.");
nThreads--;
nThread++;
} }
sw.Stop();
break; break;
case "POST": case "POST":
if (!string.IsNullOrEmpty(body)) if (!string.IsNullOrEmpty(body))
@@ -112,4 +122,17 @@ internal class Program
break; break;
} }
} }
}
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();
}
}