Created Post function with threading
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
//Body
|
||||
//Per quanto tempo fare il test
|
||||
//Metodo HTTP
|
||||
//Numero Thread(?)
|
||||
//Numero Thread
|
||||
|
||||
using System.Diagnostics;
|
||||
|
||||
@@ -22,6 +22,7 @@ internal class Program
|
||||
long lTime;
|
||||
string? yn;
|
||||
int nThreads;
|
||||
int nThread = 1;
|
||||
Stopwatch sw;
|
||||
|
||||
using var client = new HttpClient();
|
||||
@@ -96,7 +97,6 @@ internal class Program
|
||||
switch (methodHttp.ToUpper())
|
||||
{
|
||||
case "GET":
|
||||
int nThread = 1;
|
||||
while (nThreads != 0)
|
||||
{
|
||||
Thread thread = new Thread(() => { Get(url, lTime); });
|
||||
@@ -106,18 +106,19 @@ internal class Program
|
||||
nThreads--;
|
||||
nThread++;
|
||||
}
|
||||
|
||||
break;
|
||||
case "POST":
|
||||
if (!string.IsNullOrEmpty(body))
|
||||
{
|
||||
sw = Stopwatch.StartNew();
|
||||
while (sw.ElapsedMilliseconds < lTime)
|
||||
while (nThreads != 0)
|
||||
{
|
||||
Console.WriteLine("POST" + url + $" [{sw.ElapsedMilliseconds / 1000.0} s]");
|
||||
client.PostAsync(url, new StringContent(body));
|
||||
Thread thread = new Thread(() => { Post(url, lTime, body); });
|
||||
thread.Name = nThread.ToString();
|
||||
thread.Start();
|
||||
Console.WriteLine($"Thread {thread.Name} started.");
|
||||
nThreads--;
|
||||
nThread++;
|
||||
}
|
||||
sw.Stop();
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -135,4 +136,17 @@ internal class Program
|
||||
}
|
||||
sw.Stop();
|
||||
}
|
||||
|
||||
private static void Post(string url, long lTime, string body)
|
||||
{
|
||||
using var client = new HttpClient();
|
||||
Stopwatch sw = Stopwatch.StartNew();
|
||||
while (sw.ElapsedMilliseconds < lTime)
|
||||
{
|
||||
Console.WriteLine("POST" + url + $" [{sw.ElapsedMilliseconds / 1000.0} s] " + $"[# {Thread.CurrentThread.Name}]");
|
||||
var response = client.PostAsync(url, new StringContent(body));
|
||||
Console.WriteLine(response.Result.StatusCode);
|
||||
}
|
||||
sw.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user