Skip to content

Commit

Permalink
dex: add duration metric for SimulateTrade RPC (#4454)
Browse files Browse the repository at this point in the history
Backports #4454 for inclusion in v0.76.0. Refs #4402. Closes #4457.
(cherry picked from commit e1d8b2c)
  • Loading branch information
hdevalence authored and conorsch committed May 24, 2024
1 parent 974dd5d commit 75e39bc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use penumbra_proto::StateWriteProto;
use penumbra_sct::component::source::SourceContext;

use crate::{
component::{
metrics, position_manager::PositionManager as _, StateReadExt, StateWriteExt, SwapManager,
},
component::{position_manager::PositionManager as _, StateReadExt, StateWriteExt, SwapManager},
event,
swap::{proof::SwapProofPublic, Swap},
};
Expand Down Expand Up @@ -46,7 +44,6 @@ impl ActionHandler for Swap {
"Dex MUST be enabled to process swap actions."
);

let swap_start = std::time::Instant::now();
let swap = self;

// All swaps will be tallied for the block so the
Expand Down Expand Up @@ -77,8 +74,6 @@ impl ActionHandler for Swap {
);
state.add_recently_accessed_asset(swap.body.trading_pair.asset_2(), fixed_candidates);

metrics::histogram!(crate::component::metrics::DEX_SWAP_DURATION)
.record(swap_start.elapsed());
state.record_proto(event::swap(self));

Ok(())
Expand Down
7 changes: 4 additions & 3 deletions crates/core/component/dex/src/component/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ pub fn register_metrics() {
"The time spent filling routes while executing trades within the DEX"
);
describe_histogram!(
DEX_SWAP_DURATION,
DEX_RPC_SIMULATE_TRADE_DURATION,
Unit::Seconds,
"The time spent processing swaps within the DEX"
"The time spent processing a SimulateTrade RPC request"
);
}

Expand All @@ -52,4 +52,5 @@ pub const DEX_PATH_SEARCH_DURATION: &str = "penumbra_dex_path_search_duration_se
pub const DEX_ROUTE_FILL_DURATION: &str = "penumbra_dex_route_fill_duration_seconds";
pub const DEX_ARB_DURATION: &str = "penumbra_dex_arb_duration_seconds";
pub const DEX_BATCH_DURATION: &str = "penumbra_dex_batch_duration_seconds";
pub const DEX_SWAP_DURATION: &str = "penumbra_dex_swap_duration_seconds";
pub const DEX_RPC_SIMULATE_TRADE_DURATION: &str =
"penumbra_dex_rpc_simulate_trade_duration_seconds";
12 changes: 10 additions & 2 deletions crates/core/component/dex/src/component/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use penumbra_proto::{

use super::ExecutionCircuitBreaker;
use crate::{
component::metrics,
lp::position::{self, Position},
state_key, DirectedTradingPair, SwapExecution, TradingPair,
};
Expand Down Expand Up @@ -522,6 +523,7 @@ impl SimulationService for Server {
tonic::Status::invalid_argument(format!("error parsing output id: {:#}", e))
})?;

let start_time = std::time::Instant::now();
let state = self.storage.latest_snapshot();

let mut routing_params = state.routing_params().await.expect("routing params unset");
Expand Down Expand Up @@ -560,9 +562,15 @@ impl SimulationService for Server {
asset_id: input.asset_id,
};

Ok(tonic::Response::new(SimulateTradeResponse {
let rsp = tonic::Response::new(SimulateTradeResponse {
unfilled: Some(unfilled.into()),
output: Some(swap_execution.into()),
}))
});

let duration = start_time.elapsed();

metrics::histogram!(metrics::DEX_RPC_SIMULATE_TRADE_DURATION).record(duration);

Ok(rsp)
}
}

0 comments on commit 75e39bc

Please sign in to comment.