Skip to content

Commit f761446

Browse files
poisson upscaling without storing count in the sample types
1 parent 789c6ed commit f761446

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

Diff for: profiling/src/api.rs

+37
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ pub enum UpscalingInfo {
196196
count_value_offset: usize,
197197
sampling_distance: u64,
198198
},
199+
PoissonCount {
200+
// sum_value_offset is an offset in the profile values type array
201+
sum_value_offset: usize,
202+
count_value: u64,
203+
sampling_distance: u64,
204+
},
199205
Proportional {
200206
scale: f64,
201207
},
@@ -213,6 +219,15 @@ impl std::fmt::Display for UpscalingInfo {
213219
"Poisson = sum_value_offset: {}, count_value_offset: {}, sampling_distance: {}",
214220
sum_value_offset, count_value_offset, sampling_distance
215221
),
222+
UpscalingInfo::PoissonCount {
223+
sum_value_offset,
224+
count_value,
225+
sampling_distance,
226+
} => write!(
227+
f,
228+
"Poisson = sum_value_offset: {}, count_value: {}, sampling_distance: {}",
229+
sum_value_offset, count_value, sampling_distance
230+
),
216231
UpscalingInfo::Proportional { scale } => {
217232
write!(f, "Proportional = scale: {}", scale)
218233
}
@@ -241,6 +256,28 @@ impl UpscalingInfo {
241256
sampling_distance
242257
)
243258
}
259+
UpscalingInfo::PoissonCount {
260+
sum_value_offset,
261+
count_value,
262+
sampling_distance,
263+
} => {
264+
anyhow::ensure!(
265+
sum_value_offset < &number_of_values,
266+
"sum_value_offset {} must be strictly less than {}",
267+
sum_value_offset,
268+
number_of_values
269+
);
270+
anyhow::ensure!(
271+
count_value != &0,
272+
"count_value {} must be greater than 0",
273+
count_value
274+
);
275+
anyhow::ensure!(
276+
sampling_distance != &0,
277+
"sampling_distance {} must be greater than 0",
278+
sampling_distance
279+
)
280+
}
244281
UpscalingInfo::Proportional { scale: _ } => (),
245282
}
246283
anyhow::Ok(())

Diff for: profiling/src/internal/profile/mod.rs

+32
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,38 @@ mod api_tests {
14101410
assert_eq!(first.values, vec![1, 298, 29]);
14111411
}
14121412

1413+
#[test]
1414+
fn test_upscaling_by_value_on_one_value_with_poisson_count() {
1415+
let sample_types = create_samples_types();
1416+
1417+
let mut profile = Profile::new(SystemTime::now(), &sample_types, None);
1418+
1419+
let sample1 = api::Sample {
1420+
locations: vec![],
1421+
values: vec![1, 16, 29],
1422+
labels: vec![],
1423+
};
1424+
1425+
profile.add_sample(sample1, None).expect("add to success");
1426+
1427+
let upscaling_info = UpscalingInfo::PoissonCount {
1428+
sum_value_offset: 1,
1429+
count_value: 29,
1430+
sampling_distance: 10,
1431+
};
1432+
let values_offset: Vec<usize> = vec![1];
1433+
profile
1434+
.add_upscaling_rule(values_offset.as_slice(), "", "", upscaling_info)
1435+
.expect("Rule added");
1436+
1437+
let serialized_profile = pprof::roundtrip_to_pprof(profile).unwrap();
1438+
1439+
assert_eq!(serialized_profile.samples.len(), 1);
1440+
let first = serialized_profile.samples.first().expect("one sample");
1441+
1442+
assert_eq!(first.values, vec![1, 298, 29]);
1443+
}
1444+
14131445
#[test]
14141446
fn test_upscaling_by_value_on_zero_value_with_poisson() {
14151447
let sample_types = create_samples_types();

Diff for: profiling/src/internal/upscaling.rs

+14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ impl UpscalingRule {
2828
let avg = values[sum_value_offset] as f64 / values[count_value_offset] as f64;
2929
1_f64 / (1_f64 - (-avg / sampling_distance as f64).exp())
3030
}
31+
UpscalingInfo::PoissonCount {
32+
sum_value_offset,
33+
count_value,
34+
sampling_distance,
35+
} => {
36+
// This should not happen, but if it happens,
37+
// do not upscale
38+
if values[sum_value_offset] == 0 || count_value == 0 {
39+
return 1_f64;
40+
}
41+
42+
let avg = values[sum_value_offset] as f64 / count_value as f64;
43+
1_f64 / (1_f64 - (-avg / sampling_distance as f64).exp())
44+
}
3145
UpscalingInfo::Proportional { scale } => scale,
3246
}
3347
}

0 commit comments

Comments
 (0)