Skip to content

Commit 5c1d0ef

Browse files
authored
Merge pull request #1494 from alexbozhenko/b_is_for_bytes
Allow B and b when specifying number of bytes
2 parents b57635a + 6476f6b commit 5c1d0ef

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

cli/bench_command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func configureBenchCommand(app commandHost) {
9292
f.Flag("msgs", "Number of messages to publish or subscribe to").Default("100000").IntVar(&c.numMsg)
9393
f.Flag("progress", "Enable or disable the progress bar").Default("true").BoolVar(&c.progressBar)
9494
f.Flag("csv", "Save benchmark data to CSV file").StringVar(&c.csvFile)
95-
f.Flag("size", "Size of the test messages").Default("128").StringVar(&c.msgSizeString)
95+
f.Flag("size", "Size of the test messages").Default("128B").StringVar(&c.msgSizeString)
9696
// TODO: support randomized payload data
9797
}
9898

internal/util/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ func Base64IfNotPrintable(val []byte) string {
595595
}
596596

597597
var bytesUnitSplitter = regexp.MustCompile(`^(\d+)(\w+)`)
598-
var errInvalidByteString = errors.New("bytes must end in K, KB, M, MB, G, GB, T or TB")
598+
var errInvalidByteString = errors.New("bytes must end in B, K, KB, M, MB, G, GB, T or TB")
599599
var errBytesOutOfRangeString = errors.New("bytes value out of allowed range")
600600
var errInvalidBitSize = errors.New("invalid bit size")
601601
var validBitSizes = []int{8, 16, 32, 64}
@@ -645,7 +645,7 @@ func ParseStringAsBytes(s string, bitSize int) (int64, error) {
645645
}
646646

647647
suffix := matches[2]
648-
suffixMap := map[string]int64{"K": 10, "KB": 10, "KIB": 10, "M": 20, "MB": 20, "MIB": 20, "G": 30, "GB": 30, "GIB": 30, "T": 40, "TB": 40, "TIB": 40}
648+
suffixMap := map[string]int64{"B": 0, "K": 10, "KB": 10, "KIB": 10, "M": 20, "MB": 20, "MIB": 20, "G": 30, "GB": 30, "GIB": 30, "T": 40, "TB": 40, "TIB": 40}
649649

650650
mult, ok := suffixMap[strings.ToUpper(suffix)]
651651
if !ok {

internal/util/util_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,17 @@ func TestParseStringAsBytes(t *testing.T) {
210210
error bool
211211
errorKind error
212212
}{
213+
{input: "1B", expect: 1},
214+
{input: "1b", expect: 1},
213215
{input: "1", expect: 1},
214216
{input: "1000", expect: 1000},
215217
{input: "1K", expect: 1024},
216218
{input: "1k", expect: 1024},
217219
{input: "1KB", expect: 1024},
218220
{input: "1KiB", expect: 1024},
219221
{input: "1kb", expect: 1024},
222+
{input: "1024B", expect: 1024},
223+
{input: "1024b", expect: 1024},
220224
{input: "1M", expect: 1024 * 1024},
221225
{input: "1MB", expect: 1024 * 1024},
222226
{input: "1MiB", expect: 1024 * 1024},
@@ -232,10 +236,10 @@ func TestParseStringAsBytes(t *testing.T) {
232236
{input: "9223372036854775807", expect: math.MaxInt64},
233237
{input: "9223372036854775807", bits: 32, error: true, errorKind: errBytesOutOfRangeString},
234238
{input: "9223372036854775807", bits: 12, error: true, errorKind: errInvalidBitSize},
239+
{input: "", expect: -1},
235240
{input: "-1", expect: -1},
236241
{input: "-10", expect: -1},
237242
{input: "-10GB", expect: -1},
238-
{input: "1B", error: true},
239243
{input: "1FOO", error: true},
240244
{input: "FOO", error: true},
241245
}

0 commit comments

Comments
 (0)