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

Support transfer rates in MiB/s or KiB/s etc #66

Open
JamesDunne opened this issue Dec 11, 2017 · 1 comment
Open

Support transfer rates in MiB/s or KiB/s etc #66

JamesDunne opened this issue Dec 11, 2017 · 1 comment

Comments

@JamesDunne
Copy link

Really just need a way to format byte rates as float64 would be sufficient enough. An additional "/s" can easily be appended.

@dustin
Copy link
Owner

dustin commented Apr 21, 2018

The implementation that comes to mind is this:

func Rate(v float64, unit string, t time.Duration) string {
	return SI(v/t.Seconds(), unit+"/s")
}

e.g.:

func TestRate(t *testing.T) {
	tests := []struct {
		v    float64
		t    time.Duration
		want string
	}{
		{100, time.Second, "100 B/s"},
		{100, 500 * time.Millisecond, "200 B/s"},
		{100, 2 * time.Second, "50 B/s"},
	}

	for _, test := range tests {
		if got := Rate(test.v, "B", test.t); got != test.want {
			t.Errorf(`Rate %v, "B", %v) = %v; want %v`, test.v, test.t, got, test.want)
		}
	}
}

Is it worth adding that function? The downside is that I'd have to manage possible additional double formatting strings, which you could do easily today with ComputeSI in a custom implementation. The exact API there is convenient if you're OK with how the doubles format, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants