From 23003b48df693e6a2fcce13755aac0408aa96a18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=A4rtens?= Date: Thu, 9 May 2024 14:53:54 +0200 Subject: [PATCH] Add a query client example --- Cargo.lock | 1 + common/query_server/Cargo.toml | 11 ++- .../{demo.rs => dummy_query_server.rs} | 0 common/query_server/examples/query_client.rs | 73 +++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) rename common/query_server/examples/{demo.rs => dummy_query_server.rs} (100%) create mode 100644 common/query_server/examples/query_client.rs diff --git a/Cargo.lock b/Cargo.lock index 3d7d31585e..1f6359bed6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7107,6 +7107,7 @@ dependencies = [ name = "veloren-query-server" version = "0.1.0" dependencies = [ + "clap", "protocol", "rand 0.8.5", "tokio", diff --git a/common/query_server/Cargo.toml b/common/query_server/Cargo.toml index c7132d69af..c7f125278b 100644 --- a/common/query_server/Cargo.toml +++ b/common/query_server/Cargo.toml @@ -9,16 +9,23 @@ edition = "2021" [features] server = ["dep:rand"] client = ["tokio/time"] -example = ["tokio/macros", "tokio/rt-multi-thread", "dep:tracing-subscriber", "client", "server"] +example = ["tokio/macros", "tokio/rt-multi-thread", "dep:tracing-subscriber", "dep:clap", "client", "server"] default = [] [dependencies] tokio = { workspace = true, features = ["net", "sync"] } protocol = { version = "3.4.0", default-features = false, features = ["derive"] } + tracing-subscriber = { version = "0.3.7", optional = true } tracing = { workspace = true } rand = { workspace = true, optional = true } +clap = { workspace = true, features = ["derive"], optional = true } [[example]] -name = "demo" +name = "query_client" required-features = ["example"] + + +[[example]] +name = "dummy_query_server" +required-features = ["example"] \ No newline at end of file diff --git a/common/query_server/examples/demo.rs b/common/query_server/examples/dummy_query_server.rs similarity index 100% rename from common/query_server/examples/demo.rs rename to common/query_server/examples/dummy_query_server.rs diff --git a/common/query_server/examples/query_client.rs b/common/query_server/examples/query_client.rs new file mode 100644 index 0000000000..52a1e30a39 --- /dev/null +++ b/common/query_server/examples/query_client.rs @@ -0,0 +1,73 @@ +use std::time::Duration; + +use clap::Parser; +use tracing::{error, info}; +use veloren_query_server::client::QueryClient; + +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct Args { + host: String, + + #[arg(short, long, default_value_t = 14006)] + port: u16, +} + +#[tokio::main] +async fn main() { + tracing_subscriber::fmt::init(); + let args = Args::parse(); + + let mut ip = match tokio::net::lookup_host(format!("{}:{}", args.host, args.port)).await { + Ok(ip) => ip, + Err(e) => { + error!(?e, "Couldn't look up hostname: {}", &args.host); + return; + }, + }; + let addr = match ip.next() { + Some(ip) => ip, + None => { + error!("No IP-Addr found for: {}", &args.host); + return; + }, + }; + info!("Connecting to server at: {addr}"); + + let mut client = QueryClient::new(addr); + const REQUESTS: usize = 10; + let mut infos = vec![]; + + for _ in 0..REQUESTS { + let info = client.server_info().await; + match &info { + Ok((_, ping)) => { + info!("Ping: {}ms", ping.as_millis()); + }, + Err(e) => error!(?e, "Failed to fetch info from server"), + } + infos.push(info); + } + + let successful = infos.iter().filter(|info| info.is_ok()).count(); + let errors = infos.iter().filter(|info| info.is_err()).count(); + let avg_ping_sum: Duration = infos + .iter() + .filter_map(|info| info.as_ref().ok().map(|info| info.1)) + .sum(); + + println!("successful: {successful}"); + println!("errors: {errors}"); + if successful > 0 { + println!("avg_ping: {:?}", avg_ping_sum / (successful as u32)); + } + + if let Some(last_info) = infos + .iter() + .rev() + .filter_map(|info| info.as_ref().ok().map(|info| info.0)) + .next() + { + println!("{:?}", last_info); + } +}