From 6a11fefa404cae9530722db824a3b5afb6df03da Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 3 Oct 2022 19:01:10 +0200 Subject: [PATCH 1/2] Added precompile workload --- Project.toml | 2 ++ src/HTTP.jl | 1 + src/workload.jl | 13 +++++++++++++ 3 files changed, 16 insertions(+) create mode 100644 src/workload.jl diff --git a/Project.toml b/Project.toml index 65c919b54..c8a46c2ed 100644 --- a/Project.toml +++ b/Project.toml @@ -15,6 +15,7 @@ NetworkOptions = "ca575930-c2e3-43a9-ace4-1e988b2c1908" OpenSSL = "4d8831e6-92b7-49fb-bdf8-b643e874388c" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" SimpleBufferStream = "777ac1f9-54b0-4bf8-805c-2214025038e7" +SnoopPrecompile = "66db9d55-30c0-4569-8b51-7e840670fc0c" Sockets = "6462fe0b-24de-5631-8697-dd941f90decc" URIs = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4" UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" @@ -27,6 +28,7 @@ MbedTLS = "0.6.8, 0.7, 1" OpenSSL = "1" SimpleBufferStream = "1.1" URIs = "1.3" +SnoopPrecompile = "^1" julia = "1.6" [extras] diff --git a/src/HTTP.jl b/src/HTTP.jl index e432dfb3d..ba3008216 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -585,4 +585,5 @@ function Base.parse(::Type{T}, str::AbstractString)::T where T <: Message return m end +include("workload.jl") end # module diff --git a/src/workload.jl b/src/workload.jl new file mode 100644 index 000000000..1797d5326 --- /dev/null +++ b/src/workload.jl @@ -0,0 +1,13 @@ +using SnoopPrecompile +@precompile_all_calls begin + resize!(empty!(Parsers.status_line_regex), Threads.nthreads()) + resize!(empty!(Parsers.request_line_regex), Threads.nthreads()) + resize!(empty!(Parsers.header_field_regex), Threads.nthreads()) + resize!(empty!(Parsers.obs_fold_header_field_regex), Threads.nthreads()) + resize!(empty!(Parsers.empty_header_field_regex), Threads.nthreads()) + port, server = listenany(ip"0.0.0.0", 8080) + router = Router() + register!(router, "GET", "/read/**", _ -> Response(200)) + t = @async serve(router, "0.0.0.0", port, server=server) + resp = get("http://localhost:$port/read//$(homedir())") +end From ee189ecb557d1e01d3f5f13fa4b6c2e25bde77ed Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 4 Oct 2022 10:12:37 +0200 Subject: [PATCH 2/2] implement comments and fixed some tests --- src/workload.jl | 9 ++++++--- test/client.jl | 34 +++++++++++++++++----------------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/workload.jl b/src/workload.jl index 1797d5326..2a6a3c7b6 100644 --- a/src/workload.jl +++ b/src/workload.jl @@ -5,9 +5,12 @@ using SnoopPrecompile resize!(empty!(Parsers.header_field_regex), Threads.nthreads()) resize!(empty!(Parsers.obs_fold_header_field_regex), Threads.nthreads()) resize!(empty!(Parsers.empty_header_field_regex), Threads.nthreads()) - port, server = listenany(ip"0.0.0.0", 8080) router = Router() register!(router, "GET", "/read/**", _ -> Response(200)) - t = @async serve(router, "0.0.0.0", port, server=server) - resp = get("http://localhost:$port/read//$(homedir())") + server = HTTP.serve!(router, "0.0.0.0", 8080; listenany=true) + try + resp = get("http://localhost:$(port(server))/read//$(homedir())") + finally + close(server) + end end diff --git a/test/client.jl b/test/client.jl index ee6065e8a..df438908b 100644 --- a/test/client.jl +++ b/test/client.jl @@ -209,7 +209,7 @@ end @testset "Incomplete response with known content length" begin server = nothing try - server = HTTP.listen!("0.0.0.0", 8080) do http + server = HTTP.listen!("0.0.0.0", 8080; listenany=true) do http HTTP.setstatus(http, 200) HTTP.setheader(http, "Content-Length" => "64") # Promise 64 bytes... HTTP.startwrite(http) @@ -220,7 +220,7 @@ end end err = try - HTTP.get("http://localhost:8080"; retry=false) + HTTP.get("http://localhost:$(HTTP.port(server))"; retry=false) catch err err end @@ -290,12 +290,12 @@ end for interface in (IPv4(0), IPv6(0)) server = nothing try - server = HTTP.listen!(string(interface), 8080) do http + server = HTTP.listen!(string(interface), 8080; listenay=true) do http HTTP.setstatus(http, 200) HTTP.startwrite(http) HTTP.write(http, "hello, world") end - req = HTTP.get("http://localhost:8080") + req = HTTP.get("http://localhost:$(HTTP.port(server))") @test req.status == 200 @test String(req.body) == "hello, world" finally @@ -309,7 +309,7 @@ end @testset "Sockets.get(sock|peer)name(::HTTP.Stream)" begin server = nothing try - server = HTTP.listen!("0.0.0.0", 8080) do http + server = HTTP.listen!("0.0.0.0", 8080; listenany=true) do http sock = Sockets.getsockname(http) peer = Sockets.getpeername(http) str = sprint() do io @@ -334,7 +334,7 @@ end @test sock[2] == server_peerport peer = Sockets.getpeername(http) @test peer[1] == ip"127.0.0.1" - @test peer[2] == 8080 + @test peer[2] == HTTP.port(server) end finally @try Base.IOError close(server) @@ -366,7 +366,7 @@ end @testset "Implicit request headers" begin server = nothing try - server = HTTP.listen!("0.0.0.0", 8080) do http + server = HTTP.listen!("0.0.0.0", 8080; listenany=true) do http data = Dict{String,String}(http.message.headers) HTTP.setstatus(http, 200) HTTP.startwrite(http) @@ -376,20 +376,20 @@ end default_user_agent = "HTTP.jl/$VERSION" # Default values HTTP.setuseragent!(default_user_agent) - d = JSON.parse(IOBuffer(HTTP.get("http://localhost:8080").body)) - @test d["Host"] == "localhost:8080" + d = JSON.parse(IOBuffer(HTTP.get("http://localhost:$(HTTP.port(server))").body)) + @test d["Host"] == "localhost:$(HTTP.port(server))" @test d["Accept"] == "*/*" @test d["User-Agent"] == default_user_agent # Overwriting behavior headers = ["Host" => "http.jl", "Accept" => "application/json"] HTTP.setuseragent!("HTTP.jl test") - d = JSON.parse(IOBuffer(HTTP.get("http://localhost:8080", headers).body)) + d = JSON.parse(IOBuffer(HTTP.get("http://localhost:$(HTTP.port(server))", headers).body)) @test d["Host"] == "http.jl" @test d["Accept"] == "application/json" @test d["User-Agent"] == "HTTP.jl test" # No User-Agent HTTP.setuseragent!(nothing) - d = JSON.parse(IOBuffer(HTTP.get("http://localhost:8080").body)) + d = JSON.parse(IOBuffer(HTTP.get("http://localhost:$(HTTP.port(server))").body)) @test !haskey(d, "User-Agent") HTTP.setuseragent!(old_user_agent) @@ -492,7 +492,7 @@ end # Trivial implementation of a proxy server # We are only interested in the request passed in by the client # Returns 400 after reading the http request into req - proxy = listen(IPv4(0), 8082) + port, proxy = listenany(IPv4(0), 8082) try @async begin sock = accept(proxy) @@ -506,7 +506,7 @@ end end # Make the HTTP request - HTTP.get("https://example.com"; proxy="http://localhost:8082", retry=false, status_exception=false) + HTTP.get("https://example.com"; proxy="http://localhost:$port", retry=false, status_exception=false) # Test if the host header exist in the request @test "Host: example.com:443" in req @@ -519,7 +519,7 @@ end @testset "Retry with request/response body streams" begin shouldfail = Ref(true) - server = HTTP.listen!(8080) do http + server = HTTP.listen!(8080; listenany=true) do http @assert !eof(http) msg = String(read(http)) if shouldfail[] @@ -533,19 +533,19 @@ end req_body = IOBuffer("hey there sailor") seekstart(req_body) res_body = IOBuffer() - resp = HTTP.get("http://localhost:8080/retry"; body=req_body, response_stream=res_body) + resp = HTTP.get("http://localhost:$(HTTP.port(server))/retry"; body=req_body, response_stream=res_body) @test resp.status == 200 @test String(take!(res_body)) == "hey there sailor" # ensure if retry=false, that we write the response body immediately shouldfail[] = true seekstart(req_body) - resp = HTTP.get("http://localhost:8080/retry"; body=req_body, response_stream=res_body, retry=false, status_exception=false) + resp = HTTP.get("http://localhost:$(HTTP.port(server))/retry"; body=req_body, response_stream=res_body, retry=false, status_exception=false) @test String(take!(res_body)) == "500 unexpected error" # when retrying, we can still get access to the most recent failed response body in the response's request context shouldfail[] = true seekstart(req_body) println("making 3rd request") - resp = HTTP.get("http://localhost:8080/retry"; body=req_body, response_stream=res_body) + resp = HTTP.get("http://localhost:$(HTTP.port(server))/retry"; body=req_body, response_stream=res_body) @test resp.status == 200 @test String(take!(res_body)) == "hey there sailor" @test String(resp.request.context[:response_body]) == "500 unexpected error"