Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[client] use types v2 (less alloc) #269

Merged
merged 46 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
ac8880a
rewrite me
niklasad1 Apr 7, 2021
17bce94
v2
niklasad1 Apr 9, 2021
ef11d59
PoC works without batch request
niklasad1 Apr 13, 2021
6c57ac6
remove `PartialEq` bounds
niklasad1 Apr 13, 2021
ba6dcd8
Merge remote-tracking branch 'origin/master' into na-client-types-v2
niklasad1 Apr 13, 2021
7388191
add naive benches types
niklasad1 Apr 13, 2021
6765fb6
misc
niklasad1 Apr 13, 2021
3314df5
remove useless lifetime
niklasad1 Apr 13, 2021
7c8885b
[ws client]: move request ID generation to client
niklasad1 Apr 14, 2021
1ae28b9
make tests compile again
niklasad1 Apr 14, 2021
9819bb5
[client transport]: kill leaky abstractions.
niklasad1 Apr 14, 2021
0b07e5d
[http client transport]: minor changes in the API.
niklasad1 Apr 15, 2021
ccf09cb
[ws client]: fix batch requests.
niklasad1 Apr 15, 2021
9b74206
fix nits
niklasad1 Apr 15, 2021
ed61ca4
Merge remote-tracking branch 'origin/master' into na-client-types-v2
niklasad1 Apr 15, 2021
c1094c3
[ws client]: generate two request IDs for subscrib
niklasad1 Apr 15, 2021
16e4a91
fix tests
niklasad1 Apr 15, 2021
32444b1
remove unused types + less alloc for params.
niklasad1 Apr 16, 2021
0bcb54d
fix nits
niklasad1 Apr 16, 2021
222df5d
more tweaks.
niklasad1 Apr 16, 2021
d949990
remove unused code
niklasad1 Apr 16, 2021
d298334
fix more nits
niklasad1 Apr 16, 2021
ee31b7d
remove unused legacy types
niklasad1 Apr 16, 2021
f2ee2a2
reorg types_v2 mod
niklasad1 Apr 16, 2021
26e2425
port macros to new types
niklasad1 Apr 16, 2021
1cbfba7
fix tests again; more jsonvalue
niklasad1 Apr 16, 2021
a47a0c9
[proc macros]: bring back impl Into for params.
niklasad1 Apr 16, 2021
dc25fb2
Merge remote-tracking branch 'origin/master' into na-client-types-v2
niklasad1 Apr 17, 2021
6bdc773
fix build
niklasad1 Apr 17, 2021
ce70d2e
[proc macros]: make it work for external crates.
niklasad1 Apr 18, 2021
6d0c784
[types]: remove weird From<Option<T>> to impl.
niklasad1 Apr 18, 2021
151b066
cleanup again
niklasad1 Apr 18, 2021
78153c9
[examples]: remove unused async-std dep
niklasad1 Apr 18, 2021
bee0f8b
Update types/src/v2/mod.rs
niklasad1 Apr 18, 2021
76d2c40
[types]: remove unsed dep smallvec
niklasad1 Apr 19, 2021
552b9de
Merge branch 'na-client-types-v2' of github.com:paritytech/jsonrpsee …
niklasad1 Apr 19, 2021
b055e6d
rewrite me
niklasad1 Apr 19, 2021
3a7a09b
[types]: error code impl ser/deser
niklasad1 Apr 19, 2021
bbdafb7
[types v2]: re-org with explicit mods
niklasad1 Apr 19, 2021
b922550
Merge remote-tracking branch 'origin/master' into na-client-types-v2
niklasad1 Apr 19, 2021
6670484
fix faulty test
niklasad1 Apr 19, 2021
714113b
add missed files
niklasad1 Apr 19, 2021
6c2210d
[ws client]: req_manager reserve unsubscribe slot.
niklasad1 Apr 20, 2021
6f0591a
simplify test code
niklasad1 Apr 20, 2021
dea03d8
add tracking issue for TODO
niklasad1 Apr 20, 2021
2e5267b
remove unused deps
niklasad1 Apr 20, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"http-server",
"test-utils",
"jsonrpsee",
"tests",
"types",
"utils",
"ws-client",
Expand Down
1 change: 1 addition & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ criterion = "0.3"
futures-channel = "0.3"
jsonrpsee = { path = "../jsonrpsee", features = ["full"] }
num_cpus = "1"
serde_json = "1"
tokio = { version = "1", features = ["full"] }

[[bench]]
Expand Down
40 changes: 35 additions & 5 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
use criterion::*;
use jsonrpsee::{
http_client::{jsonrpc::Params, Client, HttpClientBuilder},
http_client::{
traits::Client,
v2::params::{Id, JsonRpcParams},
v2::request::JsonRpcCallSer,
HttpClientBuilder,
},
ws_client::WsClientBuilder,
};
use std::sync::Arc;
use tokio::runtime::Runtime as TokioRuntime;

mod helpers;

criterion_group!(benches, http_requests, websocket_requests);
criterion_group!(benches, http_requests, websocket_requests, jsonrpsee_types_v2);
criterion_main!(benches);

fn v2_serialize<'a>(req: JsonRpcCallSer<'a>) -> String {
serde_json::to_string(&req).unwrap()
}

pub fn jsonrpsee_types_v2(crit: &mut Criterion) {
crit.bench_function("jsonrpsee_types_v2_array_ref", |b| {
b.iter(|| {
let params = &[1_u64.into(), 2_u32.into()];
let params = JsonRpcParams::ArrayRef(params);
let request = JsonRpcCallSer::new(Id::Number(0), "say_hello", params);
v2_serialize(request);
})
});

crit.bench_function("jsonrpsee_types_v2_vec", |b| {
b.iter(|| {
let params = JsonRpcParams::Array(vec![1_u64.into(), 2_u32.into()]);
let request = JsonRpcCallSer::new(Id::Number(0), "say_hello", params);
v2_serialize(request);
})
});
}

pub fn http_requests(crit: &mut Criterion) {
let rt = TokioRuntime::new().unwrap();
let url = rt.block_on(helpers::http_server());
Expand All @@ -22,7 +50,8 @@ pub fn http_requests(crit: &mut Criterion) {
pub fn websocket_requests(crit: &mut Criterion) {
let rt = TokioRuntime::new().unwrap();
let url = rt.block_on(helpers::ws_server());
let client = Arc::new(rt.block_on(WsClientBuilder::default().build(&url)).unwrap());
let client =
Arc::new(rt.block_on(WsClientBuilder::default().max_concurrent_requests(1024 * 1024).build(&url)).unwrap());
run_round_trip(&rt, crit, client.clone(), "ws_round_trip");
run_concurrent_round_trip(&rt, crit, client.clone(), "ws_concurrent_round_trip");
}
Expand All @@ -31,7 +60,7 @@ fn run_round_trip(rt: &TokioRuntime, crit: &mut Criterion, client: Arc<impl Clie
crit.bench_function(name, |b| {
b.iter(|| {
rt.block_on(async {
black_box(client.request::<String, _, _>("say_hello", Params::None).await.unwrap());
black_box(client.request::<String>("say_hello", JsonRpcParams::NoParams).await.unwrap());
})
})
});
Expand All @@ -51,7 +80,8 @@ fn run_concurrent_round_trip<C: 'static + Client + Send + Sync>(
for _ in 0..num_concurrent_tasks {
let client_rc = client.clone();
let task = rt.spawn(async move {
let _ = black_box(client_rc.request::<String, _, _>("say_hello", Params::None)).await;
let _ =
black_box(client_rc.request::<String>("say_hello", JsonRpcParams::NoParams).await.unwrap());
});
tasks.push(task);
}
Expand Down
4 changes: 4 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ path = "ws.rs"
[[example]]
name = "ws_subscription"
path = "ws_subscription.rs"

[[example]]
name = "proc_macro"
path = "proc_macro.rs"
6 changes: 4 additions & 2 deletions examples/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
// DEALINGS IN THE SOFTWARE.

use jsonrpsee::{
http_client::{jsonrpc::Params, Client, HttpClientBuilder},
http_client::{traits::Client, HttpClientBuilder, JsonValue},
http_server::HttpServerBuilder,
};
use std::net::SocketAddr;
Expand All @@ -37,8 +37,10 @@ async fn main() -> anyhow::Result<()> {
let server_addr = run_server().await?;
let url = format!("http://{}", server_addr);

let params: &[JsonValue] = &[1_u64.into(), 2.into(), 3.into()];

let client = HttpClientBuilder::default().build(url)?;
let response: Result<String, _> = client.request("say_hello", Params::None).await;
let response: Result<String, _> = client.request("say_hello", params.into()).await;
println!("r: {:?}", response);

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,34 @@
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use crate::jsonrpc::{self, wrapped::Params};
use core::fmt;
use jsonrpsee::{http_client::HttpClientBuilder, http_server::HttpServerBuilder};
use std::net::SocketAddr;

/// Notification received on a server.
///
/// Wraps around a `jsonrpc::Notification`.
#[derive(PartialEq)]
pub struct Notification(jsonrpc::Notification);

impl From<jsonrpc::Notification> for Notification {
fn from(notif: jsonrpc::Notification) -> Notification {
Notification(notif)
jsonrpsee::proc_macros::rpc_client_api! {
RpcApi {
#[rpc(method = "state_getPairs", positional_params)]
fn storage_pairs(prefix: usize, hash: Option<String>) -> Vec<u8>;
}
}

impl From<Notification> for jsonrpc::Notification {
fn from(notif: Notification) -> jsonrpc::Notification {
notif.0
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();

impl Notification {
/// Returns the method of this notification.
pub fn method(&self) -> &str {
&self.0.method
}
let server_addr = run_server().await?;
let url = format!("http://{}", server_addr);

/// Returns the parameters of the notification.
pub fn params(&self) -> Params {
Params::from(&self.0.params)
}
let client = HttpClientBuilder::default().build(url)?;
let response = RpcApi::storage_pairs(&client, 0_usize, Some("aaa".to_string())).await?;
println!("r: {:?}", response);

Ok(())
}

impl fmt::Debug for Notification {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Notification").field("method", &self.method()).field("params", &self.params()).finish()
}
async fn run_server() -> anyhow::Result<SocketAddr> {
let mut server = HttpServerBuilder::default().build("127.0.0.1:0".parse()?)?;
server.register_method("state_getPairs", |_| Ok(vec![1, 2, 3]))?;
let addr = server.local_addr();
tokio::spawn(async move { server.start().await });
addr
}
4 changes: 2 additions & 2 deletions examples/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
// DEALINGS IN THE SOFTWARE.

use jsonrpsee::{
ws_client::{jsonrpc::Params, Client, WsClientBuilder},
ws_client::{traits::Client, v2::params::JsonRpcParams, WsClientBuilder},
ws_server::WsServer,
};
use std::net::SocketAddr;
Expand All @@ -37,7 +37,7 @@ async fn main() -> anyhow::Result<()> {
let url = format!("ws://{}", addr);

let client = WsClientBuilder::default().build(&url).await?;
let response: String = client.request("say_hello", Params::None).await?;
let response: String = client.request("say_hello", JsonRpcParams::NoParams).await?;
println!("r: {:?}", response);

Ok(())
Expand Down
6 changes: 3 additions & 3 deletions examples/ws_subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
// DEALINGS IN THE SOFTWARE.

use jsonrpsee::{
ws_client::{jsonrpc::Params, SubscriptionClient, WsClientBuilder, WsSubscription},
ws_client::{traits::SubscriptionClient, v2::params::JsonRpcParams, Subscription, WsClientBuilder},
ws_server::WsServer,
};
use std::net::SocketAddr;
Expand All @@ -39,8 +39,8 @@ async fn main() -> anyhow::Result<()> {
let url = format!("ws://{}", addr);

let client = WsClientBuilder::default().build(&url).await?;
let mut subscribe_hello: WsSubscription<String> =
client.subscribe("subscribe_hello", Params::None, "unsubscribe_hello").await?;
let mut subscribe_hello: Subscription<String> =
client.subscribe("subscribe_hello", JsonRpcParams::NoParams, "unsubscribe_hello").await?;

let mut i = 0;
while i <= NUM_SUBSCRIPTION_RESPONSES {
Expand Down
1 change: 0 additions & 1 deletion http-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ log = "0.4"
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = "1.0"
thiserror = "1.0"
unicase = "2.6"
url = "2.2"
fnv = "1"

Expand Down
Loading