From 86fb13710926e9754c812a83a535abab0b72bbea Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Tue, 19 Nov 2024 22:12:04 -0500 Subject: [PATCH 1/9] add an offline precompile workload --- Project.toml | 4 +++- src/HTTP.jl | 5 +++++ src/precompile.jl | 24 ++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/precompile.jl diff --git a/Project.toml b/Project.toml index b6a27488..723eb66c 100644 --- a/Project.toml +++ b/Project.toml @@ -14,6 +14,7 @@ LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36" MbedTLS = "739be429-bea8-5141-9913-cc70e7f3736d" NetworkOptions = "ca575930-c2e3-43a9-ace4-1e988b2c1908" OpenSSL = "4d8831e6-92b7-49fb-bdf8-b643e874388c" +PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" SimpleBufferStream = "777ac1f9-54b0-4bf8-805c-2214025038e7" Sockets = "6462fe0b-24de-5631-8697-dd941f90decc" @@ -27,6 +28,7 @@ ExceptionUnwrapping = "0.1" LoggingExtras = "0.4.9,1" MbedTLS = "0.6.8, 0.7, 1" OpenSSL = "1.3" +PrecompileTools = "1.2.1" SimpleBufferStream = "1.1" URIs = "1.3" julia = "1.6" @@ -37,9 +39,9 @@ Deno_jll = "04572ae6-984a-583e-9378-9577a1c2574d" Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" +NetworkOptions = "ca575930-c2e3-43a9-ace4-1e988b2c1908" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" -NetworkOptions = "ca575930-c2e3-43a9-ace4-1e988b2c1908" [targets] test = ["BufferedStreams", "Deno_jll", "Distributed", "InteractiveUtils", "JSON", "Test", "Unitful", "NetworkOptions"] diff --git a/src/HTTP.jl b/src/HTTP.jl index 8dc68871..c4aae91b 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -631,4 +631,9 @@ function Base.parse(::Type{T}, str::AbstractString)::T where T <: Message return m end +# only run if precompiling +if ccall(:jl_generating_output, Cint, ()) == 1 + include("precompile.jl") +end + end # module diff --git a/src/precompile.jl b/src/precompile.jl new file mode 100644 index 00000000..2217eb39 --- /dev/null +++ b/src/precompile.jl @@ -0,0 +1,24 @@ +using PrecompileTools: @setup_workload, @compile_workload + +@setup_workload begin + # TODO: Are these all safe to call here and bake into the pkgimage? + Connections.__init__() + MultiPartParsing.__init__() + Parsers.__init__() + ConnectionRequest.__init__() + + gzip_data(data::String) = read(GzipCompressorStream(IOBuffer(data))) + + # random port in the dynamic/private range (49152–65535) which are are + # least likely to be used by well-known services + _port = 57813 + + server = HTTP.serve!("0.0.0.0", _port; verbose = -1) do req + HTTP.Response(200, ["Content-Encoding" => "gzip"], gzip_data("dummy response")) + end + @compile_workload begin + HTTP.get("http://localhost:$_port") + end + HTTP.forceclose(server) + server = nothing +end From 4510bd2a0cb818abe71d75fb71d2b57c01b0dfa4 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Wed, 20 Nov 2024 12:30:39 -0500 Subject: [PATCH 2/9] suggestions --- src/precompile.jl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/precompile.jl b/src/precompile.jl index 2217eb39..76245230 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -9,16 +9,15 @@ using PrecompileTools: @setup_workload, @compile_workload gzip_data(data::String) = read(GzipCompressorStream(IOBuffer(data))) - # random port in the dynamic/private range (49152–65535) which are are - # least likely to be used by well-known services - _port = 57813 - - server = HTTP.serve!("0.0.0.0", _port; verbose = -1) do req + server = HTTP.serve!("0.0.0.0"; verbose = -1, listenany=true) do req HTTP.Response(200, ["Content-Encoding" => "gzip"], gzip_data("dummy response")) end + _port = HTTP.port(server) + @compile_workload begin HTTP.get("http://localhost:$_port") end + HTTP.forceclose(server) server = nothing end From 4f3ecfcee4c03387330704bf2b6188de2ad6eba9 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Wed, 20 Nov 2024 12:36:27 -0500 Subject: [PATCH 3/9] fix --- src/precompile.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/precompile.jl b/src/precompile.jl index 76245230..1e8718c0 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -9,9 +9,14 @@ using PrecompileTools: @setup_workload, @compile_workload gzip_data(data::String) = read(GzipCompressorStream(IOBuffer(data))) - server = HTTP.serve!("0.0.0.0"; verbose = -1, listenany=true) do req + # random port in the dynamic/private range (49152–65535) which are are + # least likely to be used by well-known services + _port = 57813 + + server = HTTP.serve!("0.0.0.0", _port; verbose = -1, listenany=true) do req HTTP.Response(200, ["Content-Encoding" => "gzip"], gzip_data("dummy response")) end + # listenany allows changing port if that one is already in use, so check the actual port _port = HTTP.port(server) @compile_workload begin From 72075d17045c9bbe8f20979a23464c2d63b5ad13 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Wed, 20 Nov 2024 12:36:33 -0500 Subject: [PATCH 4/9] tweak inits --- src/precompile.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/precompile.jl b/src/precompile.jl index 1e8718c0..40f28f33 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -1,11 +1,13 @@ using PrecompileTools: @setup_workload, @compile_workload @setup_workload begin - # TODO: Are these all safe to call here and bake into the pkgimage? + # These need to be safe to call here and bake into the pkgimage, i.e. called twice. Connections.__init__() MultiPartParsing.__init__() Parsers.__init__() - ConnectionRequest.__init__() + + # Doesn't seem to be needed here, and might not be safe to call twice (here and during runtime) + # ConnectionRequest.__init__() gzip_data(data::String) = read(GzipCompressorStream(IOBuffer(data))) From 9f82f5bd14990e7ef032e252ec17c45551f79958 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Fri, 22 Nov 2024 09:43:33 -0500 Subject: [PATCH 5/9] precompile https --- src/precompile.jl | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/precompile.jl b/src/precompile.jl index 40f28f33..27df92db 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -15,14 +15,23 @@ using PrecompileTools: @setup_workload, @compile_workload # least likely to be used by well-known services _port = 57813 - server = HTTP.serve!("0.0.0.0", _port; verbose = -1, listenany=true) do req + cert, key = joinpath.(@__DIR__, "../test", "resources", ("cert.pem", "key.pem")) + sslconfig = MbedTLS.SSLConfig(cert, key) + + server = HTTP.serve!("0.0.0.0", _port; verbose = -1, listenany=true, sslconfig=sslconfig) do req HTTP.Response(200, ["Content-Encoding" => "gzip"], gzip_data("dummy response")) end # listenany allows changing port if that one is already in use, so check the actual port _port = HTTP.port(server) + url = "https://localhost:$_port" - @compile_workload begin - HTTP.get("http://localhost:$_port") + env = ["JULIA_NO_VERIFY_HOSTS" => "localhost", + "JULIA_SSL_NO_VERIFY_HOSTS" => nothing, + "JULIA_ALWAYS_VERIFY_HOSTS" => nothing] + withenv(env...) do + @compile_workload begin + HTTP.get(url); + end end HTTP.forceclose(server) From b3e27a7394e118b6784f2e60aa84548ddc0d7492 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Fri, 22 Nov 2024 09:51:43 -0500 Subject: [PATCH 6/9] only run workload if precompiling pkgimages --- src/HTTP.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/HTTP.jl b/src/HTTP.jl index c4aae91b..00b7b36d 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -631,8 +631,8 @@ function Base.parse(::Type{T}, str::AbstractString)::T where T <: Message return m end -# only run if precompiling -if ccall(:jl_generating_output, Cint, ()) == 1 +# only run if precompiling pkgimages +if VERSION >= v"1.9.0-0" && Base.JLOptions().use_pkgimages != 0 && ccall(:jl_generating_output, Cint, ()) == 1 include("precompile.jl") end From cb9b1f2f9cd221f51d2c6e6b512fd1dca1d5cb75 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Fri, 22 Nov 2024 21:18:31 -0700 Subject: [PATCH 7/9] Update src/precompile.jl --- src/precompile.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/precompile.jl b/src/precompile.jl index 27df92db..98b48296 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -11,10 +11,6 @@ using PrecompileTools: @setup_workload, @compile_workload gzip_data(data::String) = read(GzipCompressorStream(IOBuffer(data))) - # random port in the dynamic/private range (49152–65535) which are are - # least likely to be used by well-known services - _port = 57813 - cert, key = joinpath.(@__DIR__, "../test", "resources", ("cert.pem", "key.pem")) sslconfig = MbedTLS.SSLConfig(cert, key) From b4358cd0b9eda4c61564d1032cecd99a9f021507 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Fri, 22 Nov 2024 23:37:09 -0500 Subject: [PATCH 8/9] Revert "Update src/precompile.jl" This reverts commit cb9b1f2f9cd221f51d2c6e6b512fd1dca1d5cb75. --- src/precompile.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/precompile.jl b/src/precompile.jl index 98b48296..27df92db 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -11,6 +11,10 @@ using PrecompileTools: @setup_workload, @compile_workload gzip_data(data::String) = read(GzipCompressorStream(IOBuffer(data))) + # random port in the dynamic/private range (49152–65535) which are are + # least likely to be used by well-known services + _port = 57813 + cert, key = joinpath.(@__DIR__, "../test", "resources", ("cert.pem", "key.pem")) sslconfig = MbedTLS.SSLConfig(cert, key) From 7ae48cd7193c6d6fb3538919c029923a666f72a0 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Fri, 22 Nov 2024 23:37:25 -0500 Subject: [PATCH 9/9] precompile wheter or not pkgimages are used --- src/HTTP.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/HTTP.jl b/src/HTTP.jl index 00b7b36d..b997162f 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -631,8 +631,8 @@ function Base.parse(::Type{T}, str::AbstractString)::T where T <: Message return m end -# only run if precompiling pkgimages -if VERSION >= v"1.9.0-0" && Base.JLOptions().use_pkgimages != 0 && ccall(:jl_generating_output, Cint, ()) == 1 +# only run if precompiling +if VERSION >= v"1.9.0-0" && ccall(:jl_generating_output, Cint, ()) == 1 include("precompile.jl") end