-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix throughput calculation from collectors metrics (#1621)
Co-authored-by: Amir Blum <amirgiraffe@gmail.com>
- Loading branch information
Showing
4 changed files
with
56 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package collectormetrics | ||
|
||
import "time" | ||
|
||
func calculateThroughput(diff float64, currentTime, prevTime time.Time) int64 { | ||
elapsed := currentTime.Sub(prevTime).Seconds() | ||
|
||
var throughput int64 | ||
if diff > 0 && elapsed > 0 { | ||
throughput = int64(diff / elapsed) | ||
} | ||
|
||
return throughput | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package collectormetrics | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCalculateThroughput(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
diff float64 | ||
currentTime time.Time | ||
prevTime time.Time | ||
want int64 | ||
}{ | ||
{name: "no diff and no time change", diff: 0, currentTime: time.Now(), prevTime: time.Now(), want: 0}, | ||
{name: "diff of 1 in 1 second", diff: 1, currentTime: time.Now(), prevTime: time.Now().Add(-1 * time.Second), want: 1}, | ||
{name: "diff of 1 in 2 seconds", diff: 1, currentTime: time.Now(), prevTime: time.Now().Add(-2 * time.Second), want: 0}, | ||
{name: "diff of 0 in 1 second", diff: 0, currentTime: time.Now(), prevTime: time.Now().Add(-1 * time.Second), want: 0}, | ||
{name: "diff of 100 in 10 seconds", diff: 100, currentTime: time.Now(), prevTime: time.Now().Add(-10 * time.Second), want: 10}, | ||
{name: "diff of 100 in 0.1 seconds", diff: 100, currentTime: time.Now(), prevTime: time.Now().Add(-100 * time.Millisecond), want: 1000}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
got := calculateThroughput(c.diff, c.currentTime, c.prevTime) | ||
if got != c.want { | ||
t.Errorf("calculateThroughput(%f, %v, %v) = %d, want %d", c.diff, c.currentTime, c.prevTime, got, c.want) | ||
} | ||
}) | ||
} | ||
|
||
} |