|
| 1 | +#[macro_use] |
| 2 | +extern crate log; |
| 3 | + |
| 4 | +use rsocket_rust::prelude::*; |
| 5 | +use rsocket_rust_transport_tcp::TcpClientTransport; |
| 6 | +use std::error::Error; |
| 7 | +use std::sync::{ |
| 8 | + atomic::{AtomicU32, Ordering}, |
| 9 | + Arc, |
| 10 | +}; |
| 11 | +use std::time::SystemTime; |
| 12 | +use tokio::runtime::Runtime; |
| 13 | +use tokio::sync::Notify; |
| 14 | + |
| 15 | +const TOTAL: u32 = 1_000_000; |
| 16 | + |
| 17 | +fn main() -> Result<(), Box<dyn Error + Send + Sync>> { |
| 18 | + env_logger::builder().format_timestamp_millis().init(); |
| 19 | + |
| 20 | + let mut rt = Runtime::new()?; |
| 21 | + let client = rt.block_on(async { |
| 22 | + RSocketFactory::connect() |
| 23 | + .transport(TcpClientTransport::from("127.0.0.1:7878")) |
| 24 | + .start() |
| 25 | + .await |
| 26 | + })?; |
| 27 | + // simulate 1KB payload. |
| 28 | + let req = Payload::builder() |
| 29 | + .set_data_utf8("X".repeat(1024).as_ref()) |
| 30 | + .build(); |
| 31 | + let counter = Arc::new(AtomicU32::new(0)); |
| 32 | + let start_time = SystemTime::now(); |
| 33 | + let notify = Arc::new(Notify::new()); |
| 34 | + for _ in 0..TOTAL { |
| 35 | + let client = client.clone(); |
| 36 | + let counter = counter.clone(); |
| 37 | + let notify = notify.clone(); |
| 38 | + let req = req.clone(); |
| 39 | + rt.spawn(async move { |
| 40 | + client.request_response(req).await.expect("Request failed"); |
| 41 | + let current = counter.fetch_add(1, Ordering::SeqCst) + 1; |
| 42 | + if current >= TOTAL { |
| 43 | + notify.notify(); |
| 44 | + } |
| 45 | + }); |
| 46 | + } |
| 47 | + rt.block_on(async move { |
| 48 | + notify.notified().await; |
| 49 | + }); |
| 50 | + let costs = SystemTime::now() |
| 51 | + .duration_since(start_time) |
| 52 | + .unwrap() |
| 53 | + .as_millis(); |
| 54 | + info!( |
| 55 | + "total={}, cost={}ms, qps={}", |
| 56 | + TOTAL, |
| 57 | + costs, |
| 58 | + 1000f64 * (TOTAL as f64) / (costs as f64) |
| 59 | + ); |
| 60 | + Ok(()) |
| 61 | +} |
0 commit comments