Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added precompile workload #930

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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]
Expand Down
1 change: 1 addition & 0 deletions src/HTTP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -585,4 +585,5 @@ function Base.parse(::Type{T}, str::AbstractString)::T where T <: Message
return m
end

include("workload.jl")
end # module
16 changes: 16 additions & 0 deletions src/workload.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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())
router = Router()
register!(router, "GET", "/read/**", _ -> Response(200))
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
34 changes: 17 additions & 17 deletions test/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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[]
Expand All @@ -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"
Expand Down