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? methodHttp = null;
string? time;
long? lTime;
long lTime;
string? yn;
int nThreads;
Stopwatch sw;
using var client = new HttpClient();
@@ -86,17 +87,26 @@ 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;
}
}
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();
}
}