Skip to content

Commit

Permalink
Big update
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanWoollett-Light committed Mar 19, 2024
1 parent 733d2f1 commit 6e577b4
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 71 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Format
run: cargo fmt --check

- name: Lint
run: cargo clippy --tests -- --deny warnings

- name: Test
run: cargo test

Expand All @@ -23,7 +29,7 @@ jobs:
PRIVATE_KEY: "9bac68182b35f8df71b2543c38427758"
# Variables to upload
HEAD: ${{ github.event.pull_request.head.sha }}
DATA_FILE: "./metrics"
DATA_FILE: "./metrics.csv"
# Variables to diff
TOKEN: ${{ secrets.GITHUB_TOKEN }}
BASE: ${{ github.event.pull_request.base.sha }}
Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Format
run: cargo fmt --check

- name: Lint
run: cargo clippy --tests -- --deny warnings

- name: Test
run: cargo test

Expand All @@ -25,4 +31,4 @@ jobs:
PRIVATE_KEY: "9bac68182b35f8df71b2543c38427758"
# Variables to upload
HEAD: ${{ github.event.after }}
DATA_FILE: "./metrics"
DATA_FILE: "./metrics.csv"
48 changes: 12 additions & 36 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ edition = "2021"
[dependencies]

[dev-dependencies]
serde_json = "1.0.111"
serde = "1.0.195"
rand = "0.8.5"
26 changes: 0 additions & 26 deletions metrics

This file was deleted.

24 changes: 24 additions & 0 deletions metrics.csv
Original file line number Diff line number Diff line change
@@ -0,0 +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
23 changes: 17 additions & 6 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
use rand::distributions::{Distribution, Standard};
use rand::thread_rng;
use rand::Rng;
use std::collections::HashMap;
use std::collections::BTreeMap;
use std::io::Write;
use std::sync::Mutex;
use std::sync::OnceLock;

static METRICS: Metrics = Metrics::new();

struct Metrics(OnceLock<MetricsInner>);
struct MetricsInner(Mutex<(u64, HashMap<&'static str, u64>)>);

/// 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 {
Expand All @@ -18,7 +23,7 @@ impl Metrics {
fn handle(&self) -> MetricsHandle<'_> {
let metrics = self
.0
.get_or_init(|| MetricsInner(Mutex::new((0, HashMap::new()))));
.get_or_init(|| MetricsInner(Mutex::new((0, BTreeMap::new()))));
let mut guard = metrics.0.lock().unwrap();
let (count, _map) = &mut *guard;
*count += 1;
Expand All @@ -35,17 +40,23 @@ impl MetricsHandle<'_> {
}
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 file = std::fs::OpenOptions::new()
let mut file = std::fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open("metrics")
.open("metrics.csv")
.unwrap();
serde_json::to_writer_pretty(file, map).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();
}
}
}
Expand Down

0 comments on commit 6e577b4

Please sign in to comment.