Skip to content

Commit

Permalink
Add support for terabyte and petabyte memory units. (google#138)
Browse files Browse the repository at this point in the history
* Add support for terabyte and petabyte memory units.

This fixes google#137 (but not google#136, this will come separately once we decide
what we want to do if anything).

* Add a test.

* Update copyright year
  • Loading branch information
aalexand authored and Gabriel Marin committed Dec 17, 2020
1 parent 2db1a5e commit 163e971
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
20 changes: 16 additions & 4 deletions internal/measurement/measurement.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,16 @@ func memoryLabel(value int64, fromUnit, toUnit string) (v float64, u string, ok

switch fromUnit {
case "byte", "b":
case "kilobyte", "kb":
case "kb", "kbyte", "kilobyte":
value *= 1024
case "megabyte", "mb":
case "mb", "mbyte", "megabyte":
value *= 1024 * 1024
case "gigabyte", "gb":
case "gb", "gbyte", "gigabyte":
value *= 1024 * 1024 * 1024
case "tb", "tbyte", "terabyte":
value *= 1024 * 1024 * 1024 * 1024
case "pb", "pbyte", "petabyte":
value *= 1024 * 1024 * 1024 * 1024 * 1024
default:
return 0, "", false
}
Expand All @@ -188,8 +192,12 @@ func memoryLabel(value int64, fromUnit, toUnit string) (v float64, u string, ok
toUnit = "kb"
case value < 1024*1024*1024:
toUnit = "mb"
default:
case value < 1024*1024*1024*1024:
toUnit = "gb"
case value < 1024*1024*1024*1024*1024:
toUnit = "tb"
default:
toUnit = "pb"
}
}

Expand All @@ -203,6 +211,10 @@ func memoryLabel(value int64, fromUnit, toUnit string) (v float64, u string, ok
output, toUnit = float64(value)/(1024*1024), "MB"
case "gb", "gbyte", "gigabyte":
output, toUnit = float64(value)/(1024*1024*1024), "GB"
case "tb", "tbyte", "terabyte":
output, toUnit = float64(value)/(1024*1024*1024*1024), "TB"
case "pb", "pbyte", "petabyte":
output, toUnit = float64(value)/(1024*1024*1024*1024*1024), "PB"
}
return output, toUnit, true
}
Expand Down
43 changes: 43 additions & 0 deletions internal/measurement/measurement_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package measurement

import (
"testing"
)

func TestScale(t *testing.T) {
for _, tc := range []struct {
value int64
fromUnit, toUnit string
wantValue float64
wantUnit string
}{
{1, "s", "ms", 1000, "ms"},
{1, "kb", "b", 1024, "B"},
{1, "kbyte", "b", 1024, "B"},
{1, "kilobyte", "b", 1024, "B"},
{1, "mb", "kb", 1024, "kB"},
{1, "gb", "mb", 1024, "MB"},
{1024, "gb", "tb", 1, "TB"},
{1024, "tb", "pb", 1, "PB"},
{2048, "mb", "auto", 2, "GB"},
} {
if gotValue, gotUnit := Scale(tc.value, tc.fromUnit, tc.toUnit); gotValue != tc.wantValue || gotUnit != tc.wantUnit {
t.Errorf("Scale(%d, %q, %q) = (%f, %q), want (%f, %q)",
tc.value, tc.fromUnit, tc.toUnit, gotValue, gotUnit, tc.wantValue, tc.wantUnit)
}
}
}

0 comments on commit 163e971

Please sign in to comment.