Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for nanocpus (fixes issue #18) #19

Merged
merged 1 commit into from
Aug 27, 2024
Merged
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
add support for nano and micro
philgebhardt committed Aug 3, 2024
commit 079550d7d98ae5683d35fa612362af876113333d
50 changes: 50 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -95,6 +95,8 @@ fn parse_suffix(input: &str) -> IResult<&str, (Format, Scale)> {
tag("Ti"),
tag("Pi"),
tag("Ei"),
tag("n"),
tag("u"),
tag("m"),
tag("k"),
tag("M"),
@@ -114,6 +116,8 @@ fn parse_suffix(input: &str) -> IResult<&str, (Format, Scale)> {
"Pi" => (Format::BinarySI, Scale::Peta),
"Ei" => (Format::BinarySI, Scale::Exa),
//
"n" => (Format::DecimalSI, Scale::Nano),
"u" => (Format::DecimalSI, Scale::Micro),
"m" => (Format::DecimalSI, Scale::Milli),
"" => (Format::DecimalSI, Scale::One),
"k" => (Format::DecimalSI, Scale::Kilo),
@@ -202,6 +206,32 @@ mod tests {
assert_eq!(quantity.to_string(), "0".to_owned());
}

#[test]
fn test_nano_quantity() {
let quantity = parse_quantity_string("100000000n");
assert!(quantity.is_ok());

let quantity = quantity.unwrap().1;
assert_eq!(quantity.value, Decimal::new(100000000, 0));
assert_eq!(quantity.scale, Scale::Nano);
assert_eq!(quantity.format, Format::DecimalSI);

assert_eq!(quantity.to_string(), "100000000n");
}

#[test]
fn test_micro_quantity() {
let quantity = parse_quantity_string("100000u");
assert!(quantity.is_ok());

let quantity = quantity.unwrap().1;
assert_eq!(quantity.value, Decimal::new(100000, 0));
assert_eq!(quantity.scale, Scale::Micro);
assert_eq!(quantity.format, Format::DecimalSI);

assert_eq!(quantity.to_string(), "100000u");
}

#[test]
fn test_milli_quantity() {
let quantity = parse_quantity_string("100m");
@@ -245,6 +275,26 @@ mod tests {
assert_eq!(q3.to_string(), "2049Ki");
}

#[test]
fn test_quantity_addition_binary_si_mixed_scales_2() {
let q1 = parse_quantity_string("50m").unwrap().1;
let q2 = parse_quantity_string("50000000n").unwrap().1;

let q3 = q1 + q2;

assert_eq!(q3.to_string(), "100000000n");
}

#[test]
fn test_quantity_addition_binary_si_mixed_scales_3() {
let q1 = parse_quantity_string("1.5u").unwrap().1;
let q2 = parse_quantity_string("500n").unwrap().1;

let q3 = q1 + q2;

assert_eq!(q3.to_string(), "2000n");
}

#[test]
fn test_quantity_addition_binary_si_decimal_exponent() {
let q1 = parse_quantity_string("12Mi").unwrap().1;
6 changes: 6 additions & 0 deletions src/scale.rs
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@
/// scales are omitted for mathematical simplicity.
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Default)]
pub(crate) enum Scale {
Nano,
Micro,
Milli,
#[default]
One,
@@ -25,6 +27,8 @@ impl From<&Scale> for i32 {
fn from(value: &Scale) -> Self {
// https://en.wikipedia.org/wiki/Kilobyte
match value {
Scale::Nano => -3,
Scale::Micro => -2,
Scale::Milli => -1,
Scale::One => 0,
Scale::Kilo => 1,
@@ -42,6 +46,8 @@ impl TryFrom<i32> for Scale {

fn try_from(value: i32) -> Result<Self, Self::Error> {
match value {
-3 => Ok(Scale::Nano),
-2 => Ok(Scale::Micro),
-1 => Ok(Scale::Milli),
0 => Ok(Scale::One),
1 => Ok(Scale::Kilo),
4 changes: 4 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@ use crate::{format::Format, scale::Scale};
pub(crate) fn scale_format_to_string(scale: &Scale, format: &Format) -> String {
match format {
Format::BinarySI => match scale {
Scale::Nano => "n".to_owned(),
Scale::Micro => "u".to_owned(),
Comment on lines +7 to +8
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, like Milli, I suppose these should be "", given they aren't applicable?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question.
I'll have to look into the kube source and how they are handling it.

In the current implementation in this crate, I assumed that Milli would be the smallest unit, and then I "upgraded" it to a simple decimal. However, considering there are smaller denominations, this might need to be changed, and the smaller units should be returned. (Or add modulo logic so that if the value modulo 1000 is 0, the next higher unit is used, and the number is divided by 1000).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this should not block this PR from getting merged, as from a mathematical point of view, the results are correct.

Scale::Milli => "".to_owned(),
Scale::One => "".to_owned(),
Scale::Kilo => "Ki".to_owned(),
@@ -14,6 +16,8 @@ pub(crate) fn scale_format_to_string(scale: &Scale, format: &Format) -> String {
Scale::Exa => "Ei".to_owned(),
},
Format::DecimalSI => match scale {
Scale::Nano => "n".to_owned(),
Scale::Micro => "u".to_owned(),
Scale::Milli => "m".to_owned(),
Scale::One => "".to_owned(),
Scale::Kilo => "k".to_owned(),