sui_rpc_benchmark/json_rpc/runner.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use super::request_loader::JsonRpcRequestLine;
use crate::config::BenchmarkConfig;
/// This module implements the JSON RPC benchmark runner.
/// The main function is `run_queries`, which runs the queries concurrently
/// and records the overall and per-method stats.
use anyhow::{Context as _, Result};
use dashmap::DashMap;
use phf::phf_map;
use serde::Deserialize;
use serde_json::Value;
use std::{
collections::HashMap,
sync::{Arc, Mutex},
time::Instant,
};
use sui_indexer_alt_framework::task::TrySpawnStreamExt;
use tokio::time::timeout;
use tracing::{debug, info, warn};
/// static map of method names to the index of their cursor parameter
static METHOD_CURSOR_POSITIONS: phf::Map<&'static str, usize> = phf_map! {
// based on function headers in crates/sui-json-rpc-api/src/indexer.rs
"suix_getOwnedObjects" => 2,
"suix_queryTransactionBlocks" => 1,
// based on function headers in crates/sui-json-rpc-api/src/coin.rs
"suix_getCoins" => 2,
"suix_getAllCoins" => 1,
};
static METHOD_LENGTHS: phf::Map<&'static str, usize> = phf_map! {
// based on function headers in crates/sui-json-rpc-api/src/indexer.rs
"suix_getOwnedObjects" => 4,
"suix_queryTransactionBlocks" => 4,
// based on function headers in crates/sui-json-rpc-api/src/coin.rs
"suix_getCoins" => 4,
"suix_getAllCoins" => 3,
};
/// Statistics for a single JSON RPC method
#[derive(Clone, Default)]
pub struct PerMethodStats {
pub total_sent: usize,
pub total_errors: usize,
pub total_latency_ms: f64,
}
/// Aggregated statistics for all JSON RPC requests
#[derive(Clone, Default)]
pub struct JsonRpcStats {
pub total_sent: usize,
pub total_errors: usize,
pub total_latency_ms: f64,
pub per_method: HashMap<String, PerMethodStats>,
}
/// Tracks pagination state for active pagination requests
/// The key is a tuple of method name and the params `Vec<Value>`, where the cursor parameter is set to `null`.
/// The value is the cursor for the next page.
#[derive(Default)]
struct PaginationCursorState {
requests: DashMap<(String, Vec<Value>), Value>,
}
impl JsonRpcStats {
pub fn new() -> Self {
Self::default()
}
fn record_request(&mut self, method: &str, latency_ms: f64, is_error: bool) {
self.total_sent += 1;
self.total_latency_ms += latency_ms;
if is_error {
self.total_errors += 1;
}
let method_stats = self.per_method.entry(method.to_string()).or_default();
method_stats.total_sent += 1;
method_stats.total_latency_ms += latency_ms;
if is_error {
method_stats.total_errors += 1;
}
}
}
impl PaginationCursorState {
fn new() -> Self {
Self {
requests: DashMap::new(),
}
}
/// Returns the index of the cursor parameter for a method, if it exists;
/// Otherwise, it means no cursor transformation is needed for this method.
fn get_method_cursor_index(method: &str) -> Option<usize> {
METHOD_CURSOR_POSITIONS.get(method).copied()
}
fn get_method_key(
method: &str,
params: &[Value],
) -> Result<(String, Vec<Value>), anyhow::Error> {
let cursor_idx = METHOD_CURSOR_POSITIONS
.get(method)
.with_context(|| format!("method {} not found in cursor positions", method))?;
let mut key_params = params.to_vec();
if let Some(param_to_modify) = key_params.get_mut(*cursor_idx) {
*param_to_modify = Value::Null;
} else {
let method_length = METHOD_LENGTHS
.get(method)
.with_context(|| format!("method {} not found in method lengths", method))?;
key_params.resize(*method_length, Value::Null);
}
Ok((method.to_string(), key_params))
}
fn update_params_cursor(
params: &mut Value,
cursor_idx: usize,
new_cursor: Option<&Value>,
method: &str,
) -> Result<(), anyhow::Error> {
let params_array = params
.get_mut("params")
.and_then(|v| v.as_array_mut())
.with_context(|| format!("params not found or not an array for method {}", method))?;
// If the cursor parameter is not present, extend the array to include it.
if params_array.len() <= cursor_idx {
let method_length = METHOD_LENGTHS
.get(method)
.with_context(|| format!("method {} not found in method lengths", method))?;
params_array.resize(*method_length, Value::Null);
}
let param_to_modify = params_array.get_mut(cursor_idx).with_context(|| {
format!(
"Failed to access cursor parameter at index {} for method {}",
cursor_idx, method
)
})?;
*param_to_modify = match new_cursor {
Some(cursor) => cursor.clone(),
None => Value::Null,
};
Ok(())
}
/// Updates the stored cursor for a given method and parameters.
/// The new cursor value is read from the response of a successful previous request.
///
/// # Arguments
/// * `key` - A tuple containing the method name and parameters
/// * `cursor` - The new cursor value to store, or None to remove the stored value
///
/// # Returns
/// * `Option<Value>` - The stored cursor value if it exists, otherwise None
fn update(&self, key: (String, Vec<Value>), cursor: Option<Value>) {
if let Some(cursor) = cursor {
self.requests.insert(key, cursor);
} else {
self.requests.remove(&key);
}
}
/// Returns a stored cursor for a given method and parameters.
/// The cursor value is originally read from the response of a successful previous request.
///
/// # Arguments
/// * `key` - A tuple containing the method name and parameters
///
/// # Returns
/// * `Option<Value>` - The stored cursor value if it exists, otherwise None
fn get(&self, key: &(String, Vec<Value>)) -> Option<Value> {
self.requests.get(key).map(|entry| entry.clone())
}
}
pub async fn run_queries(
endpoint: &str,
requests: &[JsonRpcRequestLine],
config: &BenchmarkConfig,
) -> Result<JsonRpcStats> {
let concurrency = config.concurrency;
let shared_stats = Arc::new(Mutex::new(JsonRpcStats::new()));
let pagination_state = Arc::new(PaginationCursorState::new());
let client = reqwest::Client::new();
let endpoint = endpoint.to_owned();
let duration = config.duration;
let methods_to_skip = config.json_rpc_methods_to_skip.clone();
info!("Skipping methods: {:?}", methods_to_skip);
let requests: Vec<_> = requests
.iter()
.filter(|r| !methods_to_skip.contains(&r.method))
// TODO: remove this hack when the SDK has removed all MatchAny & MatchAll related implementation.
// Skip suix_getOwnedObjects requests with MatchAny & MatchAll filters b/c it's not supported.
.filter(|r| {
!(r.method == "suix_getOwnedObjects"
&& r.body_json
.get("params")
.and_then(|p| p.as_array())
.and_then(|p| p.get(1))
.and_then(|p| p.get("filter"))
.and_then(|f| f.as_object())
.map(|f| f.contains_key("MatchAny") || f.contains_key("MatchAll"))
.unwrap_or(false))
})
.cloned()
.collect();
let total_requests = requests.len();
debug!(
"Starting benchmark with {} requests at concurrency {}",
total_requests, concurrency
);
let start_time = Instant::now();
let stats = shared_stats.clone();
let process_requests = async {
#[derive(Debug)]
enum BenchmarkError {
Other(anyhow::Error),
}
impl From<anyhow::Error> for BenchmarkError {
fn from(e: anyhow::Error) -> Self {
BenchmarkError::Other(e)
}
}
let result = futures::stream::iter(requests.into_iter())
.try_for_each_spawned(concurrency, |mut request_line| {
let client = client.clone();
let endpoint = endpoint.clone();
let pagination_state = pagination_state.clone();
let task_stats = stats.clone();
let params = request_line
.body_json
.get("params")
.and_then(|v| v.as_array())
.map(|a| a.to_vec())
.unwrap_or_else(|| {
// Some methods like rpc.discover might not have params
debug!("No params found for method: {}, using empty array", request_line.method);
Vec::new()
});
async move {
// Update the cursor parameter if the request uses pagination
if let Some(cursor_idx) = PaginationCursorState::get_method_cursor_index(&request_line.method) {
if !params.is_empty() {
let method_key = match PaginationCursorState::get_method_key(&request_line.method, ¶ms) {
Ok(key) => key,
Err(e) => return Err(BenchmarkError::Other(e)),
};
if let Err(e) = PaginationCursorState::update_params_cursor(
&mut request_line.body_json,
cursor_idx,
pagination_state.get(&method_key).as_ref(),
&request_line.method,
) {
return Err(BenchmarkError::Other(e));
}
}
}
let now = Instant::now();
debug!("Sending request for method: {}", request_line.method);
let res = client
.post(&endpoint)
.json(&request_line.body_json)
.send()
.await;
let elapsed_ms = now.elapsed().as_millis() as f64;
// update pagination cursor if the request is successful.
let mut is_error = true;
if let Ok(resp) = res {
if resp.status().is_success() {
let supports_pagination = PaginationCursorState::get_method_cursor_index(&request_line.method).is_some();
if supports_pagination {
#[derive(Deserialize)]
struct Body {
result: Result,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Result {
has_next_page: bool,
next_cursor: Option<Value>,
}
let resp_text = match resp.text().await {
Ok(text) => text,
Err(e) => {
return Err(BenchmarkError::Other(anyhow::anyhow!(
"Failed to get response text for method {}: {}",
request_line.method, e
)));
}
};
let parse_result = serde_json::from_str::<Body>(&resp_text);
if let Ok(Body { result }) = parse_result {
let method_key = match PaginationCursorState::get_method_key(
&request_line.method,
¶ms,
) {
Ok(key) => key,
Err(e) => return Err(BenchmarkError::Other(e)),
};
if result.has_next_page {
debug!("Updated pagination cursor for method: {}, has_next_page: true",
request_line.method);
pagination_state.update(method_key, result.next_cursor);
} else {
pagination_state.update(method_key, None);
}
is_error = false;
} else {
warn!(
method = request_line.method,
body = ?request_line.body_json,
error = ?parse_result.err(),
response = resp_text,
"Response received but JSON parsing failed"
);
}
} else {
is_error = false;
}
} else {
let status = resp.status();
let resp_text = match resp.text().await {
Ok(text) => text,
Err(e) => {
return Err(BenchmarkError::Other(anyhow::anyhow!(
"Failed to get error response text for method {}: {}",
request_line.method, e
)));
}
};
warn!(
method = request_line.method,
status = ?status,
body = ?request_line.body_json,
response = resp_text,
"Response received but status is not success"
);
}
} else {
warn!(
method = request_line.method,
body = ?request_line.body_json,
error = ?res,
"Failed to get response"
);
}
let mut stats = task_stats
.lock()
.expect("Thread holding stats lock panicked");
stats.record_request(&request_line.method, elapsed_ms, is_error);
Ok::<(), BenchmarkError>(())
}
})
.await;
match result {
Ok(()) => Ok(()),
Err(BenchmarkError::Other(e)) => Err(e),
}
};
if let Some(duration_val) = duration {
match timeout(duration_val, process_requests).await {
Ok(result) => result?,
Err(_) => debug!("Benchmark timed out after reaching duration limit"),
}
} else {
process_requests.await?;
}
let elapsed = start_time.elapsed();
let final_stats = shared_stats
.lock()
.expect("Thread holding stats lock panicked")
.clone();
info!(
"Benchmark completed in {:?}. Total requests: {}, errors: {}, avg latency: {:.2}ms",
elapsed,
final_stats.total_sent,
final_stats.total_errors,
if final_stats.total_sent > 0 {
final_stats.total_latency_ms / final_stats.total_sent as f64
} else {
0.0
}
);
Ok(final_stats)
}