From 640b0ce92084e0bced1a871a89d61ba595516266 Mon Sep 17 00:00:00 2001 From: Jonathan Woollett-Light Date: Sat, 13 Apr 2024 16:29:30 +0100 Subject: [PATCH] Use `cimetrics-rs` --- Cargo.lock | 63 ++++----------------------------- Cargo.toml | 4 +-- metrics.csv | 44 +++++++++++------------ tests/integration_tests.rs | 71 ++++---------------------------------- 4 files changed, 37 insertions(+), 145 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index db9129d..87d2777 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,10 +12,16 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" name = "cimetrics-hello-world" version = "0.1.0" dependencies = [ + "cimetrics-rs", "rand", - "serde", ] +[[package]] +name = "cimetrics-rs" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15229275d15e55c5a365cecab5975bce277abde081e4924f007ae5adf828e9c" + [[package]] name = "getrandom" version = "0.2.12" @@ -39,24 +45,6 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" -[[package]] -name = "proc-macro2" -version = "1.0.79" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" -dependencies = [ - "proc-macro2", -] - [[package]] name = "rand" version = "0.8.5" @@ -87,43 +75,6 @@ dependencies = [ "getrandom", ] -[[package]] -name = "serde" -version = "1.0.197" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.197" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "syn" -version = "2.0.53" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7383cd0e49fff4b6b90ca5670bfd3e9d6a733b3f90c686605aa7eec8c4996032" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "unicode-ident" -version = "1.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 483ce18..cd7592d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,5 +8,5 @@ edition = "2021" [dependencies] [dev-dependencies] -serde = "1.0.195" -rand = "0.8.5" \ No newline at end of file +rand = "0.8.5" +cimetrics-rs = "0.1.0" \ No newline at end of file diff --git a/metrics.csv b/metrics.csv index 46ceb44..4ee9b21 100644 --- a/metrics.csv +++ b/metrics.csv @@ -1,24 +1,24 @@ constant,1 inconsistent_constant,2 -inconsistent_random,0 -random_many_five,41340 -random_many_four,22238 -random_many_one,46205 -random_many_seven,14263 -random_many_six,55351 -random_many_three,47000 -random_many_two,44152 -random_one,165 -random_two,23786 -random_u16_one,33072 -random_u16_three,20772 -random_u16_two,46266 -random_u32_one,172148772 -random_u32_three,846543351 -random_u32_two,2924622195 -random_u8_one,74 -random_u8_three,23 -random_u8_two,160 -standard_one,847951 -standard_three,613723 -standard_two,319561 +inconsistent_random,90 +random_many_five,52398 +random_many_four,18135 +random_many_one,13248 +random_many_seven,32553 +random_many_six,50113 +random_many_three,19787 +random_many_two,38536 +random_one,7 +random_two,3017 +random_u16_one,7831 +random_u16_three,20177 +random_u16_two,59389 +random_u32_one,442696873 +random_u32_three,2742354946 +random_u32_two,1572729518 +random_u8_one,245 +random_u8_three,93 +random_u8_two,163 +standard_one,682996 +standard_three,933753 +standard_two,8863 diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index d61bf23..677b879 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -1,91 +1,32 @@ use rand::distributions::{Distribution, Standard}; use rand::thread_rng; use rand::Rng; -use std::collections::BTreeMap; -use std::io::Write; -use std::sync::Mutex; -use std::sync::OnceLock; - -static METRICS: Metrics = Metrics::new(); - -struct Metrics(OnceLock); - -/// Use a `BTreeMap` over a `HashMap` so metrics are output in a consistent order and give a useful -/// diff when commited. -struct MetricsInner(Mutex<(u64, BTreeMap<&'static str, u64>)>); - -struct MetricsHandle<'a>(&'a MetricsInner); - -impl Metrics { - pub const fn new() -> Self { - Self(OnceLock::new()) - } - fn handle(&self) -> MetricsHandle<'_> { - let metrics = self - .0 - .get_or_init(|| MetricsInner(Mutex::new((0, BTreeMap::new())))); - let mut guard = metrics.0.lock().unwrap(); - let (count, _map) = &mut *guard; - *count += 1; - - MetricsHandle(metrics) - } -} -impl MetricsHandle<'_> { - fn add(&self, name: &'static str, value: u64) { - let mut guard = self.0 .0.lock().unwrap(); - let (_count, map) = &mut *guard; - map.insert(name, value); - } -} -impl Drop for MetricsHandle<'_> { - fn drop(&mut self) { - use std::fmt::Write; - - let mut guard = self.0 .0.lock().unwrap(); - let (count, map) = &mut *guard; - *count -= 1; - if *count == 0 { - let mut file = std::fs::OpenOptions::new() - .create(true) - .truncate(true) - .write(true) - .open("metrics.csv") - .unwrap(); - let csv = map.iter_mut().fold(String::new(), |mut acc, (k, v)| { - writeln!(acc, "{k},{v}").unwrap(); - acc - }); - file.write_all(csv.as_bytes()).unwrap(); - } - } -} // Upload 1. #[test] fn constant() { - let handle = METRICS.handle(); + let handle = cimetrics_rs::handle(); handle.add("constant", 1); } // Upload a random integer between 0 and 2^8. #[test] fn random_one() { - let handle = METRICS.handle(); + let handle = cimetrics_rs::handle(); handle.add("random_one", rand::thread_rng().gen::() as u64); } // Upload a random integer between 0 and 2^16. #[test] fn random_two() { - let handle = METRICS.handle(); + let handle = cimetrics_rs::handle(); handle.add("random_two", rand::thread_rng().gen::() as u64); } // 1/4 of the time doesn't upload metrics. #[test] fn inconsistent() { - let handle = METRICS.handle(); + let handle = cimetrics_rs::handle(); if rand::thread_rng().gen::() > (u8::MAX / 4) { handle.add("inconsistent_constant", 2); handle.add("inconsistent_random", rand::thread_rng().gen::() as u64); @@ -94,7 +35,7 @@ fn inconsistent() { #[test] fn many() { - let handle = METRICS.handle(); + let handle = cimetrics_rs::handle(); let mut rng = rand::thread_rng(); handle.add("random_u8_one", rng.gen::() as u64); handle.add("random_u8_two", rng.gen::() as u64); @@ -117,7 +58,7 @@ fn many() { #[test] fn standard_distribution() { - let handle = METRICS.handle(); + let handle = cimetrics_rs::handle(); let mut rng = thread_rng(); let mut iter = Standard .sample_iter(&mut rng)