Created
November 26, 2025 06:30
-
-
Save devops-school/cb9c2ffa1877f9abbc7bb7a528be9d83 to your computer and use it in GitHub Desktop.
DOTNET: TLS & Its Versions impact on Performance
This file contains hidden or 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
| using System.Diagnostics; | |
| using System.Net; | |
| using System.Security.Authentication; | |
| using System.Threading; | |
| // Simple .NET minimal API to demo TLS 1.2 vs TLS 1.3 impact | |
| // and expose basic in-process metrics at /metrics | |
| var builder = WebApplication.CreateBuilder(args); | |
| // Decide TLS version from env var: TLS_VERSION=12 or TLS_VERSION=13 | |
| // Default = 1.2 | |
| var tlsEnv = Environment.GetEnvironmentVariable("TLS_VERSION") ?? "12"; | |
| var useTls13 = tlsEnv.Trim() == "13"; | |
| var tlsLabel = useTls13 ? "TLS 1.3" : "TLS 1.2"; | |
| // Configure Kestrel to listen on HTTPS 5001 with selected TLS | |
| builder.WebHost.ConfigureKestrel(options => | |
| { | |
| options.Listen(IPAddress.Loopback, 5001, listen => | |
| { | |
| listen.UseHttps(https => | |
| { | |
| https.SslProtocols = useTls13 ? SslProtocols.Tls13 : SslProtocols.Tls12; | |
| }); | |
| }); | |
| }); | |
| var app = builder.Build(); | |
| Console.WriteLine("========================================="); | |
| Console.WriteLine(" TLS Demo API"); | |
| Console.WriteLine($" Using: {tlsLabel}"); | |
| Console.WriteLine(" Listening on: https://localhost:5001"); | |
| Console.WriteLine(" Endpoints:"); | |
| Console.WriteLine(" GET /ping - simple test endpoint"); | |
| Console.WriteLine(" GET /metrics - shows basic latency metrics"); | |
| Console.WriteLine("========================================="); | |
| // Simple in-process metrics | |
| long totalRequests = 0; | |
| long totalDurationTicks = 0; | |
| long maxDurationTicks = 0; | |
| // A simple endpoint you can hit from Postman / curl | |
| // We simulate a tiny bit of work to make TLS overhead visible. | |
| app.MapGet("/ping", async () => | |
| { | |
| var sw = Stopwatch.StartNew(); | |
| // Simulate some app work: CPU + async wait | |
| // (You can change this to Task.Delay(5) or a CPU loop if you want.) | |
| await Task.Delay(5); | |
| sw.Stop(); | |
| Interlocked.Increment(ref totalRequests); | |
| Interlocked.Add(ref totalDurationTicks, sw.ElapsedTicks); | |
| // Update max duration | |
| long oldMax, newVal; | |
| do | |
| { | |
| oldMax = Interlocked.Read(ref maxDurationTicks); | |
| newVal = sw.ElapsedTicks > oldMax ? sw.ElapsedTicks : oldMax; | |
| } while (Interlocked.CompareExchange(ref maxDurationTicks, newVal, oldMax) != oldMax); | |
| return Results.Text("pong"); | |
| }); | |
| // Metrics endpoint so you can see load numbers in the app itself | |
| app.MapGet("/metrics", () => | |
| { | |
| var req = Interlocked.Read(ref totalRequests); | |
| var totalTicks = Interlocked.Read(ref totalDurationTicks); | |
| var maxTicks = Interlocked.Read(ref maxDurationTicks); | |
| var totalMs = TimeSpan.FromTicks(totalTicks).TotalMilliseconds; | |
| var avgMs = req > 0 ? totalMs / req : 0.0; | |
| var maxMs = TimeSpan.FromTicks(maxTicks).TotalMilliseconds; | |
| var result = new | |
| { | |
| tls = tlsLabel, | |
| totalRequests = req, | |
| averageDurationMs = avgMs, | |
| maxDurationMs = maxMs | |
| }; | |
| return Results.Json(result); | |
| }); | |
| app.Run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment