# HTTP.jl async is slow compared to python+aiohttp

**URL:** <https://discourse.julialang.org/t/http-jl-async-is-slow-compared-to-python-aiohttp/96736>\
**Category:** Performance\
**Tags:** data\
**Created:** [March 29, 2023, 1:36am UTC](https://discourse.julialang.org/t/http-jl-async-is-slow-compared-to-python-aiohttp/96736 "2023-03-29T01:36:00Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 30, 2023, 5:46am UTC](https://discourse.julialang.org/t/http-jl-async-is-slow-compared-to-python-aiohttp/96736/5 "2023-03-30T05:46:03Z")

</div>

# Introduction

I went through several steps of optimization of the Julia code. Basically we need to make sure we are running compiled code and that we have optimized memory usage as much as possible.

I used `bmon` to check my network usage. The first thing I noticed below was that the peak receiving bandwidth was higher with asyncio. This made me wonder if there connection limit was holding HTTP.jl back.

The effective wall times are now comparable for me after optimization. Your situation may require further tuning with a higher bandwidth connection.

## Python / asyncio

Here’s what I see with Python `asyncio.run`:

```julia-auto
In [11]: %time cdatas = asyncio.run(get_items(keys))
CPU times: user 18.7 s, sys: 3.81 s, total: 22.5 s
Wall time: 37.7 s

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/d/0d7a6bb761cbe6457a8106b17e69477ceb482e5b.png)

## Initial Julia code

Here is what I see with your Julia code:

```julia-auto
julia> const urls = map(i->"https://mur-sst.s3.us-west-2.amazonaws.com/zarr-v1/analysed_sst/$i.1.0", 0:99);

julia> @time asyncmap(url->HTTP.request("GET", url, status_exception=false).body, urls);
 73.768142 seconds (12.42 M allocations: 5.409 GiB, 0.81% gc time, 15.31% compilation time)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/9/d9ae657158bb8e413e529b49d819917a0d6195d2.png)

## Compiled function

Putting this into a function and making sure it gets compiled, I then get the following results via Julia:

```julia-auto
julia> function f()
           asyncmap(url->HTTP.request("GET", url, status_exception=false).body, urls);
       end

julia> @time f()
 51.969691 seconds (3.01 M allocations: 4.816 GiB, 0.42% gc time)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/7/073510b166656c0d21aa7d8798fb5dca58bf7b60.png)

## Julia optimization with preallocated buffers

Optimizations:

1. Use a function to compile the code
2. Make all the globals `const`
3. Increase the `connection_limit`
4. Preallocate the buffers
5. Using `Threads.@spawn` to allow tasks to use a threads.

```julia-auto
julia> const urls = map(i->"https://mur-sst.s3.us-west-2.amazonaws.com/zarr-v1/analysed_sst/$i.1.0", 0:99);

julia> const buffers = [IOBuffer(; sizehint = 64*1024*1024, maxsize=64*1024*1024) for x in 1:100]

julia> function f()
           seekstart.(buffers)
           @sync map(urls, buffers) do url, buffer
               Threads.@spawn HTTP.request("GET", url, status_exception=false, connection_limit=25, response_stream=buffer)
           end
       end

julia> @time f()
 35.779649 seconds (5.80 M allocations: 176.242 MiB, 0.21% compilation time)

julia> seekstart.(buffers); read.(buffers)
100-element Vector{Vector{UInt8}}:
 [0x02, 0x01, 0x21, 0x02, 0x60, 0x38, 0xdc, 0x03, 0x00, 0x00 … 0x0f, 0x02, 0x00, 0x08, 0x50, 0xb5, 0xb5, 0xb5, 0xb4, 0xb4]
...

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/a/aa638a7c3390b551d88394ccc42f81e4c678e88f.png)

## Discussion

Part of the optimization above is general to Julia. Make your globals const or at least binding them to a type assists in precompilation. Creating a function is also helpful for precompilation. Managing memory is also important and I suspect that this accounts for some difference.

Above we preallocated a lot of memory partially based on prior knowledge. This prior knowledge could be obtained via a single HTTP request to the following URL and then parsing the returned XML:

[https://mur-sst.s3.us-west-2.amazonaws.com/?prefix=zarr-v1/analysed\_sst](https://mur-sst.s3.us-west-2.amazonaws.com/?prefix=zarr-v1/analysed_sst)

This uses the Amazon S3 ListObjectsV2 API:

> **[ListObjectsV2 - Amazon Simple Storage Service](https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html)**
>
> Returns some or all (up to 1,000) of the objects in a bucket with each request. You can use the request parameters as selection criteria to return a subset of the objects in a bucket. A 200 OK response can contain valid or invalid XML. Make sure to...

We may not have to preallocate all the memory. We just need enough to handle the number of concurrent connections. We could then copy the memory out and then reuse the `IOBuffer`s.

---

_[View the full topic](https://discourse.julialang.org/t/http-jl-async-is-slow-compared-to-python-aiohttp/96736)._
