-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
148 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,79 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
| ||
namespace ClientBenchmark | ||
{ | ||
class HttpClientWrapper | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
public class HttpClientWrapper | ||
{ | ||
private const int MaxRetries = 1; | ||
private const int RetryDelayMilliseconds = 1000; | ||
private object lockObject = new object(); | ||
private readonly HttpClient httpClient; | ||
|
||
public HttpClientWrapper() | ||
{ | ||
// Configure HttpClient to use HTTP/3 with HTTP/2 fallback | ||
var handler = new HttpClientHandler(); | ||
handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls13 | System.Security.Authentication.SslProtocols.Tls12; // Support TLS 1.2 for HTTP/2 fallback | ||
|
||
httpClient = new HttpClient(handler); | ||
} | ||
|
||
public async Task<string> GetWithRetryAsync(string url) | ||
{ | ||
int attempts = 0; | ||
Version httpVersion = new Version(3, 0); // Start with HTTP/3 | ||
|
||
while (attempts < MaxRetries) | ||
{ | ||
try | ||
{ | ||
var request = new HttpRequestMessage(HttpMethod.Get, url); | ||
request.Version = httpVersion; | ||
|
||
var response = await httpClient.SendAsync(request); | ||
|
||
if (response.IsSuccessStatusCode) | ||
{ | ||
var data = await response.Content.ReadAsStringAsync(); | ||
lock (lockObject) | ||
{ | ||
bytes += data.Length; | ||
count++; | ||
} | ||
return data; | ||
} | ||
else if (httpVersion.Major == 3 && response.StatusCode == HttpStatusCode.UpgradeRequired) | ||
{ | ||
// HTTP/3 not supported, fallback to HTTP/2 | ||
Console.WriteLine("HTTP/3 not supported, falling back to HTTP/2"); | ||
httpVersion = new Version(2, 0); | ||
} | ||
else | ||
{ | ||
Console.WriteLine($"Attempt {attempts + 1} failed. Status code: {response.StatusCode}"); | ||
} | ||
} | ||
catch (HttpRequestException ex) | ||
{ | ||
Console.WriteLine($"Attempt {attempts + 1} failed. Error: {ex.Message}"); | ||
} | ||
|
||
attempts++; | ||
if (attempts < MaxRetries) | ||
{ | ||
await Task.Delay(RetryDelayMilliseconds); | ||
} | ||
} | ||
|
||
throw new Exception($"Failed to get successful response after {MaxRetries} attempts"); | ||
} | ||
|
||
// Assuming these are class-level variables | ||
public int bytes; | ||
public int count; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ClientBenchmark | ||
{ | ||
public static class Utilities | ||
{ | ||
private static readonly char[] chars = | ||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray(); | ||
|
||
public static string GenerateRandomString(int length) | ||
{ | ||
StringBuilder result = new StringBuilder(length); | ||
Random random = new Random(); | ||
|
||
for (int i = 0; i < length; i++) | ||
{ | ||
result.Append(chars[random.Next(chars.Length)]); | ||
} | ||
|
||
return result.ToString(); | ||
} | ||
public static void KillExistingProcesses(string processName) | ||
{ | ||
try | ||
{ | ||
foreach (var process in Process.GetProcessesByName(processName)) | ||
{ | ||
Console.WriteLine($"Killing process: {process.ProcessName} (ID: {process.Id})"); | ||
process.Kill(); | ||
process.WaitForExit(); // Optionally wait for the process to exit | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Error killing process: {ex.Message}"); | ||
} | ||
} | ||
|
||
public static async Task PollHttpRequest(HttpClient httpClient, string url) | ||
{ | ||
bool serverStarted = false; | ||
while (!serverStarted) | ||
{ | ||
try | ||
{ | ||
var response = await httpClient.GetAsync(url); | ||
serverStarted = response.IsSuccessStatusCode; | ||
} | ||
catch | ||
{ | ||
// Server is not ready yet, wait a bit before retrying | ||
await Task.Delay(10); | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |