mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Merge branch 'xMAC/example_query_client' into 'master'
example query client See merge request veloren/veloren!4456
This commit is contained in:
commit
999b09f9d4
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -7107,6 +7107,7 @@ dependencies = [
|
|||||||
name = "veloren-query-server"
|
name = "veloren-query-server"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"clap",
|
||||||
"protocol",
|
"protocol",
|
||||||
"rand 0.8.5",
|
"rand 0.8.5",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
@ -9,16 +9,23 @@ edition = "2021"
|
|||||||
[features]
|
[features]
|
||||||
server = ["dep:rand"]
|
server = ["dep:rand"]
|
||||||
client = ["tokio/time"]
|
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 = []
|
default = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tokio = { workspace = true, features = ["net", "sync"] }
|
tokio = { workspace = true, features = ["net", "sync"] }
|
||||||
protocol = { version = "3.4.0", default-features = false, features = ["derive"] }
|
protocol = { version = "3.4.0", default-features = false, features = ["derive"] }
|
||||||
|
|
||||||
tracing-subscriber = { version = "0.3.7", optional = true }
|
tracing-subscriber = { version = "0.3.7", optional = true }
|
||||||
tracing = { workspace = true }
|
tracing = { workspace = true }
|
||||||
rand = { workspace = true, optional = true }
|
rand = { workspace = true, optional = true }
|
||||||
|
clap = { workspace = true, features = ["derive"], optional = true }
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "demo"
|
name = "query_client"
|
||||||
required-features = ["example"]
|
required-features = ["example"]
|
||||||
|
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "dummy_query_server"
|
||||||
|
required-features = ["example"]
|
73
common/query_server/examples/query_client.rs
Normal file
73
common/query_server/examples/query_client.rs
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -45,6 +45,7 @@ pub fn add_server_systems(dispatch_builder: &mut DispatcherBuilder) {
|
|||||||
// don't depend on chunk_serialize, as we assume everything is done in a SlowJow
|
// don't depend on chunk_serialize, as we assume everything is done in a SlowJow
|
||||||
dispatch::<chunk_send::Sys>(dispatch_builder, &[]);
|
dispatch::<chunk_send::Sys>(dispatch_builder, &[]);
|
||||||
dispatch::<item::Sys>(dispatch_builder, &[]);
|
dispatch::<item::Sys>(dispatch_builder, &[]);
|
||||||
|
dispatch::<server_info::Sys>(dispatch_builder, &[]);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_sync_systems(ecs: &mut specs::World) {
|
pub fn run_sync_systems(ecs: &mut specs::World) {
|
||||||
|
@ -34,6 +34,7 @@ impl<'a> System<'a> for Sys {
|
|||||||
if let Some(sender) = sender.as_ref()
|
if let Some(sender) = sender.as_ref()
|
||||||
&& tick.0 % INFO_SEND_INTERVAL == 0
|
&& tick.0 % INFO_SEND_INTERVAL == 0
|
||||||
{
|
{
|
||||||
|
tracing::trace!("Updating server info");
|
||||||
let count = players.count().try_into().unwrap_or(u16::MAX);
|
let count = players.count().try_into().unwrap_or(u16::MAX);
|
||||||
if let Err(error) = sender.send(ServerInfo {
|
if let Err(error) = sender.send(ServerInfo {
|
||||||
git_hash: *GIT_HASH,
|
git_hash: *GIT_HASH,
|
||||||
|
Loading…
Reference in New Issue
Block a user