Skip to content

Commit

Permalink
output: use library's compact histogram format
Browse files Browse the repository at this point in the history
The sparse histogram format used for serialization is now
available in the library so remove the local implementation
and use the library's version of the structure.
  • Loading branch information
mihirn committed Aug 7, 2023
1 parent ce2bf90 commit bdd2428
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 47 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ foreign-types-shared = "0.3.1"
futures = "0.3.28"
http-body-util = "0.1.0-rc.2"
hyper = { version = "1.0.0-rc.3", features = ["http1", "http2", "client"]}
histogram = "0.7.4"
humantime = "2.1.0"
momento = "0.31.0"
metriken = "0.2.1"
Expand Down
51 changes: 6 additions & 45 deletions src/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use ratelimit::Ratelimiter;
use serde::Serialize;
use std::io::{BufWriter, Write};

use histogram::CompactHistogram;

#[macro_export]
macro_rules! output {
() => {
Expand Down Expand Up @@ -255,28 +257,12 @@ struct Responses {
miss: u64,
}

#[derive(Serialize)]
/// Sparse histogram in column-order, i.e., storing a single vector
/// per-field of non-zero buckets. Assuming index[0] = n,
/// (index[0], count[0]) corresponds to the nth bucket.
struct RequestLatencies {
/// parameters representing the resolution and the range of
/// the histogram tracking request latencies
m: u32,
r: u32,
n: u32,
/// indices for the non-zero buckets in the histogram
index: Vec<usize>,
/// histogram bucket counts corresponding to the indices
count: Vec<u32>,
}

#[derive(Serialize)]
struct Client {
connections: Connections,
requests: Requests,
responses: Responses,
request_latency: RequestLatencies,
request_latency: CompactHistogram,
}

#[derive(Serialize)]
Expand Down Expand Up @@ -308,43 +294,18 @@ struct JsonSnapshot {
}

// gets the non-zero buckets for the most recent window in the heatmap
fn heatmap_to_buckets(heatmap: &Heatmap) -> RequestLatencies {
fn heatmap_to_buckets(heatmap: &Heatmap) -> CompactHistogram {
// XXX: The heatmap corrects for wraparound and fixes indices once
// the heatmap is full so this returns the histogram for the last
// completed epoch, assuming a heatmap with a total of 60 valid
// histograms. However, this only kicks in after the entire histogram
// has been populated, so for the first minute, no histograms
// are returned (the histogram at offset 59 is still invalid).
if let Some(Some(histogram)) = heatmap.iter().map(|mut i| i.nth(59)) {
let p = histogram.parameters();
let mut index = Vec::new();
let mut count = Vec::new();

for (i, bucket) in histogram
.into_iter()
.enumerate()
.filter(|(_i, bucket)| bucket.count() != 0)
{
index.push(i);
count.push(bucket.count());
}

RequestLatencies {
m: p.0,
r: p.1,
n: p.2,
index,
count,
}
CompactHistogram::from(histogram)
} else {
trace!("no histogram");
RequestLatencies {
m: 0,
r: 0,
n: 0,
index: vec![],
count: vec![],
}
CompactHistogram::default()
}
}

Expand Down

0 comments on commit bdd2428

Please sign in to comment.