Skip to content

Commit

Permalink
feat: allow metrics to be sampled at custom intervals (#192)
Browse files Browse the repository at this point in the history
Adds a new config parameter to control the file-exposition interval. Previously, this functionality was limited to secondly sampling. Now the user may specify other values, including sub-secondly sampling.
  • Loading branch information
mihirn authored Apr 8, 2024
1 parent af0f1d3 commit 2866300
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
3 changes: 3 additions & 0 deletions configs/segcache.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ duration = 300
# optionally, specify the format for the output stats file (default is json)
# possible values are (json, msgpack, parquet).
#metrics_format = "parquet"
# optionally, specify the sampling interval for metrics. Input is a string
# with the unit attached; for example "100ms" or "1s". Default to 100ms.
#metrics_interval = "1s"
# run the admin thread with a HTTP listener at the address provided, this allows
# stats exposition via HTTP
admin = "127.0.0.1:9090"
Expand Down
28 changes: 28 additions & 0 deletions src/config/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ pub enum MetricsFormat {
Parquet,
}

pub fn metrics_interval() -> String {
"100ms".into()
}

#[derive(Clone, Deserialize)]
pub struct General {
/// The protocol to be used for the test.
Expand All @@ -29,6 +33,9 @@ pub struct General {
/// ignored if no metrics output is specified.
#[serde(default)]
metrics_format: MetricsFormat,
/// The reporting interval. Specify time along with unit; default to 100ms.
#[serde(default = "metrics_interval")]
metrics_interval: String,
/// The admin listen address
admin: String,
/// The initial seed for initializing the PRNGs. This can be any string and
Expand Down Expand Up @@ -57,6 +64,13 @@ impl General {
self.metrics_format
}

pub fn metrics_interval(&self) -> Duration {
self.metrics_interval
.parse::<humantime::Duration>()
.unwrap()
.into()
}

pub fn admin(&self) -> String {
self.admin.clone()
}
Expand All @@ -73,4 +87,18 @@ impl General {
Seed512(seed)
}
}

pub fn validate(&self) {
let interval = self.metrics_interval.parse::<humantime::Duration>();
if let Err(e) = interval {
eprintln!("metrics_interval is not valid: {e}");
std::process::exit(1);
}

let interval = interval.unwrap().as_millis();
if interval < Duration::from_millis(10).as_millis() {
eprintln!("metrics_interval should be larger than 10ms");
std::process::exit(1);
}
}
}
16 changes: 9 additions & 7 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ impl Config {
std::process::exit(1);
}
}
let toml = toml::from_str(&content);
match toml {
Ok(toml) => toml,
Err(error) => {
eprintln!("Failed to parse TOML config: {filename}\n{error}");
let config: Config = toml::from_str(&content)
.map_err(|e| {
eprintln!("Failed to parse TOML config: {filename}\n{e}");
std::process::exit(1);
}
}
})
.unwrap();

config.general.validate();
config.workload.ratelimit().validate();
config
}

pub fn general(&self) -> &General {
Expand Down
2 changes: 1 addition & 1 deletion src/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ pub async fn metrics(config: Config) {
// get the stop time
let stop = start + config.general().duration();

let mut interval = tokio::time::interval_at(start, Duration::from_secs(1));
let mut interval = tokio::time::interval_at(start, config.general().metrics_interval());

let snapshotter = SnapshotterBuilder::new()
.metadata("source".to_string(), env!("CARGO_BIN_NAME").to_string())
Expand Down
1 change: 0 additions & 1 deletion src/workload/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,6 @@ pub struct Ratelimit {
impl Ratelimit {
pub fn new(config: &Config) -> Option<Self> {
let ratelimit_config = config.workload().ratelimit();
ratelimit_config.validate();

if !ratelimit_config.is_dynamic() {
return None;
Expand Down

0 comments on commit 2866300

Please sign in to comment.