diff --git a/congestion/bandwidth.go b/congestion/bandwidth.go new file mode 100644 index 000000000..6c5c54742 --- /dev/null +++ b/congestion/bandwidth.go @@ -0,0 +1,22 @@ +package congestion + +import "time" + +// Bandwidth of a connection +type Bandwidth uint64 + +const ( + // BitsPerSecond is 1 bit per second + BitsPerSecond Bandwidth = 1 + // KBitsPerSecond is 1000 bits per second + KBitsPerSecond = 1000 * BitsPerSecond + // BytesPerSecond is 1 byte per second + BytesPerSecond = 8 * BitsPerSecond + // KBytesPerSecond is 1000 bytes per second + KBytesPerSecond = 1000 * BytesPerSecond +) + +// BandwidthFromDelta calculates the bandwidth from a number of bytes and a time delta +func BandwidthFromDelta(bytes uint64, delta time.Duration) Bandwidth { + return Bandwidth(bytes) * Bandwidth(time.Second) / Bandwidth(delta) * BytesPerSecond +} diff --git a/congestion/bandwidth_test.go b/congestion/bandwidth_test.go new file mode 100644 index 000000000..b29058b94 --- /dev/null +++ b/congestion/bandwidth_test.go @@ -0,0 +1,15 @@ +package congestion_test + +import ( + "time" + + "github.com/lucas-clemente/quic-go/congestion" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Bandwidth", func() { + It("converts from time delta", func() { + Expect(congestion.BandwidthFromDelta(1, time.Millisecond)).To(Equal(1000 * congestion.BytesPerSecond)) + }) +})