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

feat: Adds sample rate getter in Monitor & computes send/receive rates using configured sample rate #1116

Merged
merged 8 commits into from
Oct 26, 2023
7 changes: 7 additions & 0 deletions libs/flowrate/flowrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ func New(sampleRate, windowSize time.Duration) *Monitor {
}
}

// GetSampleRate returns the current sampling rate.
func (m *Monitor) GetSampleRate() time.Duration {
m.mu.Lock()
defer m.mu.Unlock()
return m.sRate
}

// Update records the transfer of n bytes and returns n. It should be called
// after each Read/Write operation, even if n is 0.
func (m *Monitor) Update(n int) int {
Expand Down
31 changes: 17 additions & 14 deletions p2p/conn/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,12 @@ func TestMConnectionSendRate(t *testing.T) {
// maxSendRate returns the maximum send rate in bytes per second based on the MConnection's SendRate and other configs. It is used to calculate the highest expected value for the peak send rate.
// The returned value is slightly higher than the configured SendRate.
func (c *MConnection) maxSendRate() int64 {
// the sample rate is set when creating the MConnection and setting up its send monitor i.e., `c.sendMonitor`
// it defaults to 100ms which is what we use here
sampleRate := 100 * time.Millisecond
sendRate := round(float64(c.config.SendRate) * sampleRate.Seconds())
batchSizeBytes := int64(numBatchPacketMsgs * c._maxPacketMsgSize)
effectiveRatePerSample := int64(math.Ceil(float64(sendRate)/float64(batchSizeBytes))) * batchSizeBytes
effectiveSendRate := 10 * effectiveRatePerSample
sampleRate := c.sendMonitor.GetSampleRate().Seconds()
numberOfSamplePerSecond := 1 / sampleRate
sendRate := float64(round(float64(c.config.SendRate) * sampleRate))
batchSizeBytes := float64(numBatchPacketMsgs * c._maxPacketMsgSize)
effectiveRatePerSample := math.Ceil(sendRate/batchSizeBytes) * batchSizeBytes
effectiveSendRate := round(numberOfSamplePerSecond * effectiveRatePerSample)

return effectiveSendRate
}
Expand Down Expand Up @@ -229,20 +228,24 @@ func TestMConnectionReceiveRate(t *testing.T) {
maxRecvRate := clientConn.maxRecvRate()

assert.True(t, peakRecvRate <= maxRecvRate, fmt.Sprintf("peakRecvRate %d > maxRecvRate %d", peakRecvRate, maxRecvRate))

peakSendRate := clientConn.sendMonitor.Status().PeakRate
maxSendRate := clientConn.maxSendRate()

assert.True(t, peakSendRate <= maxSendRate, fmt.Sprintf("peakSendRate %d > maxSendRate %d", peakSendRate, maxSendRate))
}

// maxRecvRate returns the maximum receive rate in bytes per second based on
// the MConnection's RecvRate and other configs.
// It is used to calculate the highest expected value for the peak receive rate.
// Note that the returned value is slightly higher than the configured RecvRate.
func (c *MConnection) maxRecvRate() int64 {
// the sample rate is set when creating the MConnection and setting up its receive monitor i.e., `c.recvMonitor`
// it defaults to 100ms which is what we use here
sampleRate := 100 * time.Millisecond
recvRate := round(float64(c.config.RecvRate) * sampleRate.Seconds())
batchSizeBytes := int64(c._maxPacketMsgSize)
effectiveRecvRatePerSample := int64(math.Ceil(float64(recvRate)/float64(batchSizeBytes))) * batchSizeBytes
effectiveRecvRate := 10 * effectiveRecvRatePerSample
sampleRate := c.recvMonitor.GetSampleRate().Seconds()
numberOfSamplePerSeccond := 1 / sampleRate
recvRate := float64(round(float64(c.config.RecvRate) * sampleRate))
batchSizeBytes := float64(c._maxPacketMsgSize)
effectiveRecvRatePerSample := math.Ceil(recvRate/batchSizeBytes) * batchSizeBytes
effectiveRecvRate := round(numberOfSamplePerSeccond * effectiveRecvRatePerSample)

return effectiveRecvRate
}
Expand Down
Loading