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

Adding a benchmark for transaction signing #14174

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions crates/sui-core/src/authority_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,16 @@ impl ValidatorService {
.into_inner()
}

pub async fn handle_transaction_for_testing(
&self,
transaction: Transaction,
) -> HandleTransactionResponse {
self.transaction(tonic::Request::new(transaction))
.await
.unwrap()
.into_inner()
}

async fn handle_transaction(
self,
request: tonic::Request<Transaction>,
Expand Down
20 changes: 20 additions & 0 deletions crates/sui-single-node-benchmark/src/benchmark_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::sync::Arc;
use sui_types::base_types::{ObjectID, ObjectRef, SuiAddress, SUI_ADDRESS_LENGTH};
use sui_types::crypto::{get_account_key_pair, AccountKeyPair};
use sui_types::effects::TransactionEffects;
use sui_types::messages_grpc::HandleTransactionResponse;
use sui_types::object::Object;
use sui_types::transaction::{CertifiedTransaction, SignedTransaction, Transaction};
use tracing::info;
Expand Down Expand Up @@ -232,4 +233,23 @@ impl BenchmarkContext {
*gas_objects = Arc::new(refreshed_gas_objects);
}
}

pub(crate) async fn validator_sign_transactions(
&self,
transactions: Vec<Transaction>,
) -> Vec<HandleTransactionResponse> {
info!(
"Started signing {} transactions. You can now attach a profiler",
transactions.len(),
);
let tasks: FuturesUnordered<_> = transactions
.into_iter()
.map(|tx| {
let validator = self.validator();
tokio::spawn(async move { validator.sign_transaction(tx).await })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no need to spawn a task here, since you do tasks.collect().await

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. That's on me (I wrote it that way in all other places by copy paste).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually Zhe points out that this may not run in parallel, so we may want to leave the spawn here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least for tx signing benchmark, removing the spawn actually makes it slightly faster. It could be because signing is too cheap that parallelism doesn't help

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I thought about the spawn() pattern for the initial PRs. Maybe only for executions using a separate task makes sense.

})
.collect();
let results: Vec<_> = tasks.collect().await;
results.into_iter().map(|r| r.unwrap()).collect()
}
}
2 changes: 2 additions & 0 deletions crates/sui-single-node-benchmark/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,6 @@ pub enum Component {
/// and store the sequenced transactions into the store. It covers the consensus-independent
/// portion of the code in consensus handler.
ValidatorWithFakeConsensus,
/// Benchmark only validator signing compoment: `handle_transaction`.
TxnSigning,
}
37 changes: 34 additions & 3 deletions crates/sui-single-node-benchmark/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub async fn benchmark_simple_transfer(tx_count: u64, component: Component) {
let transactions = ctx
.generate_transactions(Arc::new(NonMoveTxGenerator::new()))
.await;
benchmark_transactions(&ctx, transactions).await;
benchmark_transactions(&ctx, transactions, component).await;
}

/// Benchmark Move transactions.
Expand Down Expand Up @@ -57,7 +57,7 @@ pub async fn benchmark_move_transactions(
root_objects,
)))
.await;
benchmark_transactions(&ctx, transactions).await;
benchmark_transactions(&ctx, transactions, component).await;
}

/// In order to benchmark transactions that can read dynamic fields, we must first create
Expand Down Expand Up @@ -104,8 +104,23 @@ async fn preparing_dynamic_fields(
root_objects
}

async fn benchmark_transactions(
ctx: &BenchmarkContext,
transactions: Vec<Transaction>,
component: Component,
) {
match component {
Component::TxnSigning => {
benchmark_transaction_signing(ctx, transactions).await;
}
_ => {
benchmark_transaction_execution(ctx, transactions).await;
}
}
}

/// Benchmark parallel execution of a vector of transactions and measure the TPS.
async fn benchmark_transactions(ctx: &BenchmarkContext, transactions: Vec<Transaction>) {
async fn benchmark_transaction_execution(ctx: &BenchmarkContext, transactions: Vec<Transaction>) {
let mut transactions = ctx.certify_transactions(transactions).await;

// Print out a sample transaction and its effects so that we can get a rough idea
Expand All @@ -129,3 +144,19 @@ async fn benchmark_transactions(ctx: &BenchmarkContext, transactions: Vec<Transa
tx_count as f64 / elapsed
);
}

/// Benchmark parallel signing a vector of transactions and measure the TPS.
async fn benchmark_transaction_signing(ctx: &BenchmarkContext, transactions: Vec<Transaction>) {
let sample_transaction = &transactions[0];
info!("Sample transaction: {:?}", sample_transaction.data());

let tx_count = transactions.len();
let start_time = std::time::Instant::now();
ctx.validator_sign_transactions(transactions).await;
let elapsed = start_time.elapsed().as_millis() as f64 / 1000f64;
info!(
"Transaction signing finished in {}s, TPS={}.",
elapsed,
tx_count as f64 / elapsed,
);
}
9 changes: 9 additions & 0 deletions crates/sui-single-node-benchmark/src/single_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use sui_types::committee::Committee;
use sui_types::crypto::AccountKeyPair;
use sui_types::effects::{TransactionEffects, TransactionEffectsAPI};
use sui_types::executable_transaction::VerifiedExecutableTransaction;
use sui_types::messages_grpc::HandleTransactionResponse;
use sui_types::object::Object;
use sui_types::transaction::{
CertifiedTransaction, Transaction, VerifiedCertificate, VerifiedTransaction,
Expand Down Expand Up @@ -115,6 +116,7 @@ impl SingleValidator {
cert: CertifiedTransaction,
component: Component,
) -> TransactionEffects {
assert!(!matches!(component, Component::TxnSigning));
let effects = match component {
Component::Baseline => {
let cert = VerifiedExecutableTransaction::new_from_certificate(
Expand Down Expand Up @@ -142,8 +144,15 @@ impl SingleValidator {
.await;
response.signed_effects.into_data()
}
Component::TxnSigning => unreachable!(),
};
assert!(effects.status().is_ok());
effects
}

pub async fn sign_transaction(&self, transaction: Transaction) -> HandleTransactionResponse {
self.validator_service
.handle_transaction_for_testing(transaction)
.await
}
}
Loading