From 4cf6e2be6491f92a7c348825a392d3332ec27277 Mon Sep 17 00:00:00 2001 From: Xavrax Date: Fri, 15 Mar 2024 13:21:43 +0100 Subject: [PATCH] wtf --- examples/benchmark.rs | 79 ++++++++++++++++++++++++++++++++++++++++ src/transport/reqwest.rs | 22 +++++++++++ 2 files changed, 101 insertions(+) create mode 100644 examples/benchmark.rs diff --git a/examples/benchmark.rs b/examples/benchmark.rs new file mode 100644 index 00000000..7f9ed604 --- /dev/null +++ b/examples/benchmark.rs @@ -0,0 +1,79 @@ +use futures::stream::StreamExt; +use pubnub::{ + subscribe::{EventEmitter, EventSubscriber, SubscriptionParams}, + Keyset, PubNubClientBuilder, +}; +use serde::Serialize; +use std::{ + env, + sync::{Arc, Mutex, RwLock}, + time::{Duration, Instant, SystemTime}, +}; +use tokio::time::sleep; + +#[derive(Serialize)] +struct Message { + url: String, + description: String, +} + +#[tokio::main] +async fn main() -> Result<(), Box> { + let publish_key = env::var("SDK_PUB_KEY")?; + let subscribe_key = env::var("SDK_SUB_KEY")?; + + let client = PubNubClientBuilder::with_reqwest_transport() + .with_keyset(Keyset { + subscribe_key, + publish_key: Some(publish_key), + secret_key: None, + }) + .with_user_id("user_id") + .build()?; + + let sub = client.subscription(SubscriptionParams { + channels: Some(&["kekw"]), + channel_groups: None, + options: None, + }); + + sub.subscribe(); + + tokio::spawn(sub.messages_stream().for_each(|msg| async move { + println!( + "sub {:?}", + SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_millis() + ); + + println!("Received message: {:?}", msg); + })); + + println!( + "pub {:?}", + SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_millis() + ); + + // publish simple string + client + .publish_message("chuj dupa cipa") + .channel("kekw") + .execute() + .await?; + println!( + "pu2 {:?}", + SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_millis() + ); + + sleep(Duration::from_secs(1)).await; + + Ok(()) +} diff --git a/src/transport/reqwest.rs b/src/transport/reqwest.rs index 18bb30fc..04c6746b 100644 --- a/src/transport/reqwest.rs +++ b/src/transport/reqwest.rs @@ -17,6 +17,7 @@ not(feature = "std") ))] use crate::dx::pubnub_client::PubNubClientDeserializerBuilder; +use std::time::SystemTime; #[cfg(all(any(feature = "subscribe", feature = "presence"), feature = "std"))] use crate::dx::pubnub_client::PubNubClientRuntimeBuilder; @@ -86,6 +87,7 @@ impl Transport for TransportReqwest { #[cfg(feature = "std")] let timeout = request.timeout; + let is_publish = request.path.contains("publish"); #[cfg(feature = "std")] let mut builder = match request.method { TransportMethod::Get => self.prepare_get_method(request, request_url), @@ -105,6 +107,16 @@ impl Transport for TransportReqwest { TransportMethod::Delete => self.prepare_delete_method(request, request_url), }?; + if is_publish { + println!( + "p00 {:?}", + SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_millis() + ); + } + let result = builder .headers(headers) .send() @@ -114,6 +126,16 @@ impl Transport for TransportReqwest { response: None, })?; + if is_publish { + println!( + "p01 {:?}", + SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_millis() + ); + } + let headers = result.headers().clone(); let status = result.status(); result