Skip to content

Instantly share code, notes, and snippets.

@kamoshi
Created March 28, 2023 15:47
Show Gist options
  • Save kamoshi/5f96e4ed07952b0c3284a3e2d9061e0c to your computer and use it in GitHub Desktop.
Save kamoshi/5f96e4ed07952b0c3284a3e2d9061e0c to your computer and use it in GitHub Desktop.
use std::error::Error;
use clap::Parser;
use futures::StreamExt;
use reqwest::Client;
#[derive(clap::Parser)]
struct Cli {
#[arg(short, long, default_value_t = 1)]
count: u32,
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn Error>> {
const URL: &str = "http://127.0.0.1:8983/solr/radio/select?q=utt:hello";
let cli = Cli::parse();
let requests = Vec::from_iter(0..cli.count);
let time_start = std::time::Instant::now();
let tasks = futures::stream::iter(requests.chunks(200))
.flat_map(|chunk| {
let client = Client::new();
futures::stream::iter(chunk)
.map(move |i| client.get(format!("{URL} OR utt:{i}")).send())
})
.buffer_unordered(800)
.collect::<Vec<_>>()
.await;
let time_elapsed = time_start.elapsed();
let successful = tasks.iter().filter(|&x| x.is_ok()).count();
//println!("{}", tasks.pop().unwrap()?.text().await?);
println!("Received count: {}", successful);
println!("Time elapsed (ms): {}", time_elapsed.as_millis());
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment