Skip to content
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
43 changes: 42 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ lto = true

[dependencies]
async-std = { version = "1.9.0", features = ["unstable", "attributes"] }
serde_json = "1.0.64"
serde_json = { version = "1.0.64", features = ["preserve_order"] }
shlex = "1.0.0"
nix = "0.20.0"
assert_cmd = "1.0.3"
Expand All @@ -21,6 +21,7 @@ lazy_static = "1.4.0"
serde = { version = "1.0.124", features = ["derive"] }
anyhow = "<=1.0.48"
which = "4.0.2"
indexmap = { version = "2.9.0", features = ["serde"] }

[target.'cfg(target_os = "linux")'.dependencies]
perfcnt = "0.8.0"
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use async_std::{
use serde_json::json;
use std::{collections::HashMap, env, os::unix::process::ExitStatusExt, process::exit};
use which::which;
use indexmap::IndexMap;

mod config;
use config::*;
Expand Down Expand Up @@ -118,7 +119,7 @@ fn run_service(config: &Config) -> Result<Option<Child>> {
async fn run_iteration(
config: &Config,
statsd_buf: Arc<RwLock<String>>,
) -> Result<HashMap<String, MetricValue>> {
) -> Result<IndexMap<String, MetricValue>> {
let mut sub_config: Config = config.clone();
let json_config = serde_yaml::to_string(&config)?;
sub_config.env.insert("SIRUN_ITERATION".into(), json_config);
Expand Down
4 changes: 2 additions & 2 deletions src/metric_value.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use indexmap::IndexMap;

#[derive(Serialize, Deserialize, Clone)]
#[serde(untagged)]
Expand Down Expand Up @@ -72,4 +72,4 @@ num_type!(i32);
num_type!(i64);
num_type!(f64);

pub(crate) type MetricMap = HashMap<String, MetricValue>;
pub(crate) type MetricMap = IndexMap<String, MetricValue>;
7 changes: 4 additions & 3 deletions src/statsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use async_std::{
net::UdpSocket,
sync::{Arc, Barrier, RwLock},
};
use std::{collections::HashMap, env};
use std::env;
use indexmap::IndexMap;

pub(crate) async fn statsd_listener(
barrier: Arc<Barrier>,
Expand Down Expand Up @@ -32,8 +33,8 @@ pub(crate) async fn statsd_listener(

pub(crate) async fn get_statsd_metrics(
udp_data: Arc<RwLock<String>>,
) -> Result<HashMap<String, MetricValue>> {
let mut metrics = HashMap::new();
) -> Result<IndexMap<String, MetricValue>> {
let mut metrics = IndexMap::new();
let udp_string = udp_data.read().await.clone();
let lines = udp_string.trim().lines();
udp_data.write().await.clear();
Expand Down
20 changes: 10 additions & 10 deletions src/summarize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use anyhow::*;
use async_std::io;
use std::collections::HashMap;
use indexmap::IndexMap;

use crate::metric_value::*;

Expand All @@ -26,7 +26,7 @@ fn stddev(m: f64, items: &[f64]) -> f64 {
}

fn summary(iterations: &[MetricValue]) -> MetricValue {
let mut stats: HashMap<String, Vec<f64>> = HashMap::new();
let mut stats: IndexMap<String, Vec<f64>> = IndexMap::new();
for iteration in iterations {
let iteration = iteration.as_map();
for (k, v) in iteration {
Expand All @@ -40,9 +40,9 @@ fn summary(iterations: &[MetricValue]) -> MetricValue {
stat.push(v.clone().as_f64());
}
}
let mut result = HashMap::new();
let mut result = IndexMap::new();
for (name, items) in stats {
let mut statistics = HashMap::new();
let mut statistics = IndexMap::new();
let m = mean(&items);
let s = stddev(m, &items);
statistics.insert("mean".to_owned(), m.into());
Expand All @@ -66,7 +66,7 @@ fn summary(iterations: &[MetricValue]) -> MetricValue {
pub(crate) async fn summarize() -> Result<()> {
let stdin = io::stdin();
let mut line = String::new();
let mut result_data: MetricMap = HashMap::new();
let mut result_data: MetricMap = IndexMap::new();
while stdin.read_line(&mut line).await? != 0 {
if let Ok(mut json_data) = serde_json::from_str::<MetricMap>(&line) {
let name = match json_data.get("name") {
Expand All @@ -76,30 +76,30 @@ pub(crate) async fn summarize() -> Result<()> {
continue;
}
};
json_data.remove("name");
json_data.shift_remove("name");
let variant = match json_data.get("variant") {
Some(variant) => variant.clone().as_string(),
None => {
line = String::new();
continue;
}
};
json_data.remove("variant");
json_data.shift_remove("variant");
let name_data: &mut MetricMap = match result_data.get_mut(&name) {
Some(data) => data.as_map_mut(),
None => {
result_data.insert(name.to_owned(), HashMap::new().into());
result_data.insert(name.to_owned(), IndexMap::new().into());
result_data.get_mut(&name).unwrap().as_map_mut()
}
};

if let Some((_, iterations)) = json_data.remove_entry("iterations") {
if let Some((_, iterations)) = json_data.shift_remove_entry("iterations") {
json_data.insert("summary".to_owned(), summary(&iterations.as_vec()));
} else {
line = String::new();
continue;
}
json_data.remove("iterations");
json_data.shift_remove("iterations");
name_data.insert(variant, json_data.into());
};
line = String::new();
Expand Down
7 changes: 5 additions & 2 deletions tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,13 @@ fn summarize() {
in_path.push("tests/fixtures/summary/in.ndjson");
let mut out_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
out_path.push("tests/fixtures/summary/out.json");
let expected_output: &str = &std::fs::read_to_string(out_path).unwrap();

run!("--summarize")
.write_stdin(std::fs::read(in_path).unwrap())
.output()
.expect(&std::fs::read_to_string(out_path).unwrap());
.assert()
.success()
.stdout(predicate::eq(expected_output));
}

#[test]
Expand Down
Loading