Skip to content

Commit

Permalink
IPA bench: log random seed and runtime at info level
Browse files Browse the repository at this point in the history
  • Loading branch information
andyleiserson committed Apr 5, 2024
1 parent 4dc9e60 commit c8ec1be
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
8 changes: 5 additions & 3 deletions ipa-core/benches/oneshot/ipa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,14 @@ async fn run(args: Args) -> Result<(), Error> {
let _prep_time = Instant::now();
let config = TestWorldConfig {
gateway_config: GatewayConfig::new(args.active()),
extra_log_target: option_env!("CARGO_CRATE_NAME"),
..TestWorldConfig::default()
};
// Construct TestWorld early to initialize logging.
let world = TestWorld::new_with(&config);

let seed = args.random_seed.unwrap_or_else(|| random());
tracing::trace!(
tracing::info!(
"Using random seed: {seed} for {q} records",
q = args.query_size
);
Expand Down Expand Up @@ -154,12 +157,11 @@ async fn run(args: Args) -> Result<(), Error> {
&order,
);

let world = TestWorld::new_with(config.clone());
tracing::trace!("Preparation complete in {:?}", _prep_time.elapsed());

let _protocol_time = Instant::now();
test_oprf_ipa::<BenchField>(&world, raw_data, &expected_results, args.config()).await;
tracing::trace!(
tracing::info!(
"{m:?} IPA for {q} records took {t:?}",
m = args.mode,
q = args.query_size,
Expand Down
2 changes: 1 addition & 1 deletion ipa-core/src/helpers/transport/in_memory/sharding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ mod tests {

#[test]
fn shards_talk_to_each_other() {
logging::setup();
logging::setup(None);
run(|| async {
let shard_count = 5;
let shard_network = InMemoryShardNetwork::with_shards(shard_count);
Expand Down
39 changes: 24 additions & 15 deletions ipa-core/src/test_fixture/logging.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::sync::Once;
use std::{env, sync::Once};

static INIT: Once = Once::new();

/// Set up logging for IPA
///
/// ## Panics
/// Does not, but compiler cannot be convinced otherwise
pub fn setup() {
static INIT: Once = Once::new();

pub fn setup(extra_log_target: Option<&'static str>) {
INIT.call_once(|| {
#[cfg(feature = "tokio-console")]
{
Expand All @@ -15,30 +15,39 @@ pub fn setup() {

#[cfg(not(feature = "tokio-console"))]
{
use std::str::FromStr;

use metrics_tracing_context::MetricsLayer;
use tracing::Level;
use tracing_subscriber::{
filter::Directive, fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter,
fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter,
};

let default_directive = if let Some(crate_name) = option_env!("CARGO_CRATE_NAME") {
// only print IPA crate logging by default
Directive::from_str(&format!("{crate_name}=INFO")).unwrap()
} else {
Level::INFO.into()
};
let directives = env::var(EnvFilter::DEFAULT_ENV).unwrap_or_else(|_| {
// The purpose of this logic is to only print logs from IPA
// library/binary crate(s) by default.
match (option_env!("CARGO_CRATE_NAME"), extra_log_target) {
(Some(t1), Some(t2)) => {
format!("{t1}=INFO,{t2}=INFO")
}
(Some(t1), None) | (None, Some(t1)) => {
format!("{t1}=INFO")
}
(None, None) => String::new(),
}
});

tracing_subscriber::registry()
.with(
EnvFilter::builder()
.with_default_directive(default_directive)
.from_env_lossy(),
.with_default_directive(Level::INFO.into())
.parse_lossy(directives),
)
.with(fmt::layer())
.with(MetricsLayer::new())
.init();
}
});
}

pub fn is_initialized() -> bool {
INIT.is_completed()
}
2 changes: 1 addition & 1 deletion ipa-core/src/test_fixture/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static ONCE: OnceCell<Snapshotter> = OnceCell::new();

fn setup() {
// logging is required to import span fields as metric values
logging::setup();
assert!(logging::is_initialized());

ONCE.get_or_init(|| {
assert!(
Expand Down
5 changes: 4 additions & 1 deletion ipa-core/src/test_fixture/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ pub struct TestWorldConfig {
pub role_assignment: Option<RoleAssignment>,
/// Seed for random generators used in PRSS
pub seed: u64,
/// Additional target to include in default logging directive
pub extra_log_target: Option<&'static str>,
}

impl ShardingScheme for NotSharded {
Expand Down Expand Up @@ -230,7 +232,7 @@ impl<S: ShardingScheme> TestWorld<S> {
/// If more than [`std::u32::MAX`] shards are requested.
#[must_use]
pub fn with_config(config: &TestWorldConfig) -> Self {
logging::setup();
logging::setup(config.extra_log_target);
println!("Using seed {seed}", seed = config.seed);

let shard_count = ShardIndex::try_from(S::SHARDS).unwrap();
Expand Down Expand Up @@ -268,6 +270,7 @@ impl Default for TestWorldConfig {
metrics_level: Level::DEBUG,
role_assignment: None,
seed: thread_rng().next_u64(),
extra_log_target: None,
}
}
}
Expand Down

0 comments on commit c8ec1be

Please sign in to comment.