Skip to content

Commit

Permalink
Merge pull request #1 from Fi3/ExampleBenches
Browse files Browse the repository at this point in the history
Example sv1
  • Loading branch information
Priceless-P authored Aug 1, 2023
2 parents 931a35d + 0a52d5f commit db9a3b3
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 370 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ version = "0.1.0"
edition = "2021"

[dependencies]
async-std = "1.10.0"
async-std={version = "1.10.0", features = ["attributes"]}
criterion = "0.5.1"
# prometheus = "0.13.3"
async-channel = "1.4.0"
v1={path="../protocols/v1", package="sv1_api"}
serde_json = "1.0.70"
iai="0.1.1"
iai="0.1"
roles_logic_sv2 = { path = "../protocols/v2/roles-logic-sv2" }
serde = { version = "1.0.89", default-features = false, features = ["derive", "alloc"] }
num-bigint = "0.4.3"
Expand All @@ -19,6 +19,7 @@ bitcoin="0.27.1"
codec_sv2 = { path = "../protocols/v2/codec-sv2", features=["noise_sv2"] }
binary_sv2 = { path = "../protocols/v2/binary-sv2/binary-sv2" }
network_helpers = { path = "../utils/network-helpers", features=["async_std"] }
rand = "0.8.4"

[[bench]]
name = "criterion_sv1_benchmark"
Expand All @@ -35,7 +36,7 @@ name = "criterion_sv2_benchmark"
path = "benches/src/sv2/criterion_sv2_benchmark.rs"
harness = false

[[bench]]
name = "iai_sv2_pool_benchmark"
path = "benches/src/sv2/iai_sv2_pool_benchmark.rs"
harness = false
# [[bench]]
# name = "iai_sv2_pool_benchmark"
# path = "benches/src/sv2/iai_sv2_pool_benchmark.rs"
# harness = false
155 changes: 87 additions & 68 deletions benches/benches/src/sv1/criterion_sv1_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,110 +2,129 @@
//! It measures connection time, send subscription latency and share submission time.

use async_std::task;
use criterion::{Criterion, Throughput};
use criterion::{Criterion, Throughput,black_box};
use std::env;
use v1::ClientStatus;
use v1::IsClient;

#[path = "./lib/client.rs"]
mod client;
use crate::client::*;
use async_std::sync::{Arc, Mutex};
use async_std::sync::{Arc, Mutex,MutexGuard};
use std::time::Duration;

async fn client_connect() -> Arc<Mutex<Client<'static>>> {
let client = Client::new(0, "127.0.0.1:3002".parse().unwrap()).await;
task::sleep(Duration::from_millis(200)).await;
client
}
async fn send_configure_benchmark(client: &mut Arc<Mutex<Client<'static>>>) {
let mut client = client.lock().await;
client.send_configure().await;
client.status = ClientStatus::Configured;
}
fn benchmark_get_subscribe(c: &mut Criterion, mut client: Client) {
let mut group = c.benchmark_group("client-sv1-get-subscribe");

async fn send_subscribe_benchmark(client: &mut Arc<Mutex<Client<'static>>>) {
let mut client = client.lock().await;
client.send_subscribe().await;
return client.status = ClientStatus::Subscribed;
group.bench_function("client-sv1-get-subscribe", |b| {
b.iter(|| {
client.status = ClientStatus::Configured;
// TODO add bench also for Some(extranonce)
client.subscribe(black_box(10), None).unwrap();
});
});
}

async fn send_authorize_benchmark(client: &mut Arc<Mutex<Client<'static>>>) {
let mut client = client.lock().await;
let username = env::var("WALLET_ADDRESS").unwrap_or("user".to_string());
client
.send_authorize(username.to_string(), "12345".to_string())
.await;
fn benchmark_subscribe_serialize(c: &mut Criterion, mut client: Client) {
let mut group = c.benchmark_group("client-sv1-subscribe-serialize");

group.bench_function("client-sv1-subscribe-serialize", |b| {
b.iter(|| {
client.status = ClientStatus::Configured;
// TODO add bench also for Some(extranonce)
let mut subscribe = client.subscribe(black_box(10), None).unwrap();
Client::serialize_message(black_box(subscribe));
});
});
}
async fn send_submit_benchmark(client: &mut Arc<Mutex<Client<'static>>>) {
let mut client = client.lock().await;
let username = env::var("WALLET_ADDRESS").unwrap_or("user".to_string());
client.send_submit(username.as_str()).await;

fn benchmark_subscribe_serialize_deserialize(c: &mut Criterion, mut client: Client) {
let mut group = c.benchmark_group("client-sv1-subscribe-serialize-deserialize");

group.bench_function("client-sv1-subscribe-serialize-deserialize", |b| {
b.iter(|| {
client.status = ClientStatus::Configured;
let mut subscribe = client.subscribe(black_box(10), None).unwrap();
let mut serialized = Client::serialize_message(black_box(subscribe));
Client::parse_message(black_box(serialized));
});
});
}

fn benchmark_connection_time(c: &mut Criterion) {
c.bench_function("connection_time", |b| {
fn benchmark_subscribe_serialize_deserialize_handle(c: &mut Criterion, mut client: Client) {
let mut group = c.benchmark_group("client-sv1-subscribe-serialize-deserialize-handle");

group.bench_function("client-sv1-subscribe-serialize-deserialize-handle", |b| {
b.iter(|| {
let client = task::block_on(client_connect());
drop(client);
client.status = ClientStatus::Configured;
let mut subscribe = client.subscribe(black_box(10), None).unwrap();
let mut serialized = Client::serialize_message(black_box(subscribe));
let deserilized = Client::parse_message(black_box(serialized));
client.handle_message(black_box(deserilized));
});
});
}

fn benchmark_configure(c: &mut Criterion) {
const SUBSCRIBE_MESSAGE_SIZE: u64 = 112;
let mut group = c.benchmark_group("sv1");
group.throughput(Throughput::Bytes(SUBSCRIBE_MESSAGE_SIZE));
fn benchmark_get_authorize(c: &mut Criterion, mut client: Client) {
let mut group = c.benchmark_group("client-sv1-get-authorize");

group.bench_function("configure", |b| {
group.bench_function("client-sv1-get-authorize", |b| {
b.iter(|| {
let mut client = task::block_on(client_connect());
task::block_on(send_configure_benchmark(&mut client));
drop(client);
client.status = ClientStatus::Configured;
let authorize = client.authorize(black_box(10),black_box("user".to_string()),black_box("passowrd".to_string())).unwrap();
});
});
}
fn benchmark_subscribe(c: &mut Criterion) {
const SUBSCRIBE_MESSAGE_SIZE: u64 = 112;
let mut group = c.benchmark_group("sv1");
group.throughput(Throughput::Bytes(SUBSCRIBE_MESSAGE_SIZE));

group.bench_function("subscribe", |b| {
fn benchmark_authorize_serialize(c: &mut Criterion, mut client: Client) {
let mut group = c.benchmark_group("client-sv1-authorize-serialize");

group.bench_function("client-sv1-authorize-serialize", |b| {
b.iter(|| {
let mut client = task::block_on(client_connect());
task::block_on(send_configure_benchmark(&mut client));
task::block_on(send_subscribe_benchmark(&mut client));
drop(client);
client.status = ClientStatus::Configured;
let authorize = client.authorize(black_box(10),black_box("user".to_string()),black_box("passowrd".to_string())).unwrap();
let serialized = Client::serialize_message(black_box(authorize));
});
});
group.finish();
}

async fn share_submit() {
let mut client = client_connect().await;
send_configure_benchmark(&mut client).await;
send_subscribe_benchmark(&mut client).await;
task::sleep(Duration::from_millis(1000)).await;
send_authorize_benchmark(&mut client).await;
send_submit_benchmark(&mut client).await
fn benchmark_authorize_serialize_deserialize(c: &mut Criterion, mut client: Client) {
let mut group = c.benchmark_group("client-sv1-authorize-serialize-deserialize");

group.bench_function("client-sv1-authorize-serialize-deserialize", |b| {
b.iter(|| {
client.status = ClientStatus::Configured;
let authorize = client.authorize(black_box(10),black_box("user".to_string()),black_box("passowrd".to_string())).unwrap();
let serialized = Client::serialize_message(black_box(authorize));
Client::parse_message(black_box(serialized));
});
});
}

fn benchmark_share_submit(c: &mut Criterion) {
const SUBSCRIBE_MESSAGE_SIZE: u64 = 112;
let mut group = c.benchmark_group("sv1");
group.throughput(Throughput::Bytes(SUBSCRIBE_MESSAGE_SIZE));
group.bench_function("test_submit", |b| {
fn benchmark_authorize_serialize_deserialize_handle(c: &mut Criterion, mut client: Client) {
let mut group = c.benchmark_group("client-sv1-authorize-serialize-deserialize-handle");

group.bench_function("client-sv1-authorize-serialize-deserialize-handle", |b| {
b.iter(|| {
task::block_on(share_submit());
})
client.status = ClientStatus::Configured;
let authorize = client.authorize(black_box(10),black_box("user".to_string()),black_box("passowrd".to_string())).unwrap();
let serialized = Client::serialize_message(black_box(authorize));
let deserilized = Client::parse_message(black_box(serialized));
client.handle_message(black_box(deserilized));
});
});
group.finish();
}

fn main() {
let mut criterion = Criterion::default().sample_size(50);
benchmark_connection_time(&mut criterion);
benchmark_configure(&mut criterion);
benchmark_subscribe(&mut criterion);
benchmark_share_submit(&mut criterion);
let mut criterion = Criterion::default().sample_size(50).measurement_time(std::time::Duration::from_secs(5));
let client = Client::new(90);
benchmark_get_subscribe(&mut criterion, client.clone());
benchmark_subscribe_serialize(&mut criterion, client.clone());
benchmark_subscribe_serialize_deserialize(&mut criterion, client.clone());
benchmark_subscribe_serialize_deserialize_handle(&mut criterion, client.clone());
benchmark_get_authorize(&mut criterion, client.clone());
benchmark_authorize_serialize(&mut criterion, client.clone());
benchmark_authorize_serialize_deserialize(&mut criterion, client.clone());
benchmark_authorize_serialize_deserialize_handle(&mut criterion, client.clone());
criterion.final_summary();
}
70 changes: 25 additions & 45 deletions benches/benches/src/sv1/iai_sv1_benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,42 @@
//! The code uses iai library to measure the system requirements of sv1 client.

use async_std::{
sync::{Arc, Mutex},
task,
};
use iai::{black_box, main};
use std::{env, time::Duration};
use v1::ClientStatus;
use std::{env, time::Duration, boxed::Box};
use v1::{ClientStatus,IsClient};

#[path = "./lib/client.rs"]
mod client;
use crate::client::Client;
use std::cell::UnsafeCell;

async fn client_connect() -> Arc<Mutex<Client<'static>>> {
let client = Client::new(0, "127.0.0.1:3002".parse().unwrap()).await;
task::sleep(Duration::from_millis(200)).await;
client
}
async fn send_configure_benchmark(client: &mut Arc<Mutex<Client<'static>>>) {
let mut client = client.lock().await;
client.send_configure().await;
fn get_subscirbe() {
let mut client = Client::new(0);
client.status = ClientStatus::Configured;
black_box(client.subscribe(black_box(10), None).unwrap());
}

async fn send_subscribe_benchmark(client: &mut Arc<Mutex<Client<'static>>>) {
let mut client = client.lock().await;
client.send_subscribe().await;
return client.status = ClientStatus::Subscribed;
}

async fn send_authorize_benchmark(client: &mut Arc<Mutex<Client<'static>>>) {
let mut client = client.lock().await;
let username = env::var("WALLET_ADDRESS").unwrap_or("user".to_string());
client
.send_authorize(username.to_string(), "12345".to_string())
.await;
}
async fn send_submit_benchmark(client: &mut Arc<Mutex<Client<'static>>>) {
let mut client = client.lock().await;
let username = env::var("WALLET_ADDRESS").unwrap_or("user".to_string());
client.send_submit(username.as_str()).await;
}

async fn bench_submit() {
let mut client = client_connect().await;
send_configure_benchmark(&mut client).await;
send_subscribe_benchmark(&mut client).await;
task::sleep(Duration::from_millis(1000)).await;
send_authorize_benchmark(&mut client).await;
send_submit_benchmark(&mut client).await
fn serialize_subscirbe() {
let mut client = Client::new(0);
client.status = ClientStatus::Configured;
let mut subscribe = client.subscribe(black_box(10), None).unwrap();
black_box(Client::serialize_message(black_box(subscribe)));
}

fn benchmark_submit_share() {
task::block_on(bench_submit());
fn serialize_deserialize_subscirbe() {
let mut client = Client::new(0);
client.status = ClientStatus::Configured;
let mut subscribe = client.subscribe(black_box(10), None).unwrap();
let serlialized = Client::serialize_message(black_box(subscribe));
Client::parse_message(black_box(serlialized));
}

fn iai_sv1_share_submit() {
black_box(benchmark_submit_share());
fn serialize_deserialize_handle_subscirbe() {
let mut client = Client::new(0);
client.status = ClientStatus::Configured;
let mut subscribe = client.subscribe(black_box(10), None).unwrap();
let serlialized = Client::serialize_message(black_box(subscribe));
let serialized = Client::parse_message(black_box(serlialized));
client.handle_message(black_box(serialized));
}

main!(iai_sv1_share_submit);
main!(get_subscirbe,serialize_subscirbe,serialize_deserialize_subscirbe,serialize_deserialize_handle_subscirbe);
Loading

0 comments on commit db9a3b3

Please sign in to comment.