Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/FSharp.Data.Http/Http.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,12 @@ type Http private () =

req.AutomaticDecompression <- DecompressionMethods.GZip ||| DecompressionMethods.Deflate

// Optimize HTTP performance with connection keep-alive and pooling
req.KeepAlive <- true
req.ServicePoint.UseNagleAlgorithm <- false // Disable Nagle for better latency
req.ServicePoint.Expect100Continue <- false // Reduce handshake overhead
req.ServicePoint.ConnectionLimit <- 10 // Allow more concurrent connections per endpoint

// set cookies
let addCookiesFromHeadersToCookieContainer, cookieContainer =
match cookieContainer with
Expand Down
1 change: 1 addition & 0 deletions tests/FSharp.Data.Benchmarks/FSharp.Data.Benchmarks.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<Compile Include="JsonBenchmarks.fs" />
<Compile Include="HttpBenchmarks.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
Expand Down
76 changes: 76 additions & 0 deletions tests/FSharp.Data.Benchmarks/HttpBenchmarks.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
namespace FSharp.Data.Benchmarks

open System
open System.Net
open BenchmarkDotNet.Attributes
open FSharp.Data

[<MemoryDiagnoser>]
[<SimpleJob>]
type HttpBenchmarks() =

// Use public HTTP test endpoints for realistic benchmarks
let httpBinUrl = "https://httpbin.org"
let smallEndpoint = $"{httpBinUrl}/get"
let jsonEndpoint = $"{httpBinUrl}/json"

[<Benchmark>]
member _.SingleHttpBinRequest() =
try
Http.RequestString(smallEndpoint, timeout = 5000)
with
| :? WebException -> "Network error" // Handle network issues in CI

[<Benchmark>]
member _.HttpBinJsonRequest() =
try
Http.RequestString(jsonEndpoint, timeout = 5000)
with
| :? WebException -> "Network error" // Handle network issues in CI

[<Benchmark>]
member _.MultipleSequentialRequests() =
for _ in 1..3 do
try
Http.RequestString(smallEndpoint, timeout = 5000) |> ignore
with
| :? WebException -> () // Handle network issues in CI

[<Benchmark>]
member _.PostRequest() =
let body = TextRequest "test data"
try
Http.RequestString($"{httpBinUrl}/post", httpMethod = "POST", body = body, timeout = 5000)
with
| :? WebException -> "Network error" // Handle network issues in CI

[<Benchmark>]
member _.RequestWithHeaders() =
let headers = [("User-Agent", "FSharp.Data.Benchmarks"); ("Accept", "application/json")]
try
Http.RequestString(smallEndpoint, headers = headers, timeout = 5000)
with
| :? WebException -> "Network error" // Handle network issues in CI

[<Benchmark>]
member _.TypeProviderWorkloadSimulation() =
// Simulate a type provider fetching sample data and caching
for _ in 1..2 do
try
Http.RequestString(jsonEndpoint, timeout = 5000) |> ignore
with
| :? WebException -> () // Handle network issues in CI

[<Benchmark>]
member _.MultipleConnectionsToSameHost() =
// Test connection reuse with keep-alive optimization
let endpoints = [
$"{httpBinUrl}/get"
$"{httpBinUrl}/json"
$"{httpBinUrl}/user-agent"
]
for endpoint in endpoints do
try
Http.RequestString(endpoint, timeout = 5000) |> ignore
with
| :? WebException -> () // Handle network issues in CI
2 changes: 2 additions & 0 deletions tests/FSharp.Data.Benchmarks/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ let main args =
match args with
| [| "json" |] -> BenchmarkRunner.Run<JsonBenchmarks>() |> ignore
| [| "conversions" |] -> BenchmarkRunner.Run<JsonConversionBenchmarks>() |> ignore
| [| "http" |] -> BenchmarkRunner.Run<HttpBenchmarks>() |> ignore
| _ ->
printfn "Running all benchmarks..."
BenchmarkRunner.Run<JsonBenchmarks>() |> ignore
BenchmarkRunner.Run<JsonConversionBenchmarks>() |> ignore
BenchmarkRunner.Run<HttpBenchmarks>() |> ignore

0
Loading