Skip to content

Commit

Permalink
feat: Adds sample rate getter in Monitor & computes send/receive rate…
Browse files Browse the repository at this point in the history
…s using configured sample rate (#1116)

This PR replaces the hardcoded sample rate in traffic rate tests with
the configured rate for each connection. This ensures the tests remain
relevant if the sampling rate changes in the future.
Closes #1114
  • Loading branch information
staheri14 committed Oct 26, 2023
1 parent ffdb652 commit 4b925ca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
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

0 comments on commit 4b925ca

Please sign in to comment.