Skip to content

Instantly share code, notes, and snippets.

@tqwewe
Last active May 13, 2021 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tqwewe/2af00500f51b2237fbf23591142a06ab to your computer and use it in GitHub Desktop.
Save tqwewe/2af00500f51b2237fbf23591142a06ab to your computer and use it in GitHub Desktop.
Hyper slow body reading
[dependencies]
hyper = { version = "0.14", features = ["full"] }
tokio = { version = "1", features = ["full"] }
use hyper::{body::HttpBody, Client};
use tokio::time::Instant;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
for _ in 0..2 {
let start = Instant::now();
// Still inside `async fn main`...
let client = Client::new();
// Parse an `http::Uri`...
let uri = "http://registry.yarnpkg.com/react".parse()?;
// Await the response...
let mut resp = client.get(uri).await?;
// let mut buf: Vec<u8> = Vec::with_capacity(resp.size_hint().upper().unwrap_or(0) as usize);
let mut body = resp.into_body();
while let Some(chunk) = body.data().await {
// buf.append(&mut chunk?.to_vec());
}
// println!("{}", &String::from_utf8(buf).unwrap()[0..1]);
println!("MS: {}", start.elapsed().as_millis());
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment