Add start/end time to AnalyticsStream. (#2618)

* Add start/end time to AnalyticsStream.

* fix test
This commit is contained in:
Raja Subramanian
2024-04-03 12:23:18 +05:30
committed by GitHub
parent 1caa6ff6d0
commit 63b1fba082
9 changed files with 72 additions and 55 deletions
+10 -8
View File
@@ -793,14 +793,16 @@ func (b *Buffer) mayGrowBucket() {
return
}
oldCap := cap
deltaInfo := b.rtpStats.DeltaInfo(b.ppsSnapshotId)
if deltaInfo != nil && deltaInfo.Duration > 500*time.Millisecond {
pps := int(time.Duration(deltaInfo.Packets) * time.Second / deltaInfo.Duration)
for pps > cap && cap < maxPkts {
cap = b.bucket.Grow()
}
if cap > oldCap {
b.logger.Debugw("grow bucket", "from", oldCap, "to", cap, "pps", pps)
if deltaInfo := b.rtpStats.DeltaInfo(b.ppsSnapshotId); deltaInfo != nil {
duration := deltaInfo.EndTime.Sub(deltaInfo.StartTime)
if duration > 500*time.Millisecond {
pps := int(time.Duration(deltaInfo.Packets) * time.Second / duration)
for pps > cap && cap < maxPkts {
cap = b.bucket.Grow()
}
if cap > oldCap {
b.logger.Debugw("grow bucket", "from", oldCap, "to", cap, "pps", pps)
}
}
}
}
+6 -7
View File
@@ -55,7 +55,7 @@ func RTPDriftToString(r *livekit.RTPDrift) string {
type RTPDeltaInfo struct {
StartTime time.Time
Duration time.Duration
EndTime time.Time
Packets uint32
Bytes uint64
HeaderBytes uint64
@@ -572,7 +572,7 @@ func (r *rtpStatsBase) deltaInfo(snapshotID uint32, extStartSN uint64, extHighes
if packetsExpected == 0 {
return &RTPDeltaInfo{
StartTime: startTime,
Duration: endTime.Sub(startTime),
EndTime: endTime,
}
}
@@ -592,7 +592,7 @@ func (r *rtpStatsBase) deltaInfo(snapshotID uint32, extStartSN uint64, extHighes
return &RTPDeltaInfo{
StartTime: startTime,
Duration: endTime.Sub(startTime),
EndTime: endTime,
Packets: uint32(packetsExpected),
Bytes: now.bytes - then.bytes,
HeaderBytes: now.headerBytes - then.headerBytes,
@@ -997,9 +997,8 @@ func AggregateRTPDeltaInfo(deltaInfoList []*RTPDeltaInfo) *RTPDeltaInfo {
startTime = deltaInfo.StartTime
}
endedAt := deltaInfo.StartTime.Add(deltaInfo.Duration)
if endTime.IsZero() || endTime.Before(endedAt) {
endTime = endedAt
if endTime.IsZero() || endTime.Before(deltaInfo.EndTime) {
endTime = deltaInfo.EndTime
}
packets += deltaInfo.Packets
@@ -1038,7 +1037,7 @@ func AggregateRTPDeltaInfo(deltaInfoList []*RTPDeltaInfo) *RTPDeltaInfo {
return &RTPDeltaInfo{
StartTime: startTime,
Duration: endTime.Sub(startTime),
EndTime: endTime,
Packets: packets,
Bytes: bytes,
HeaderBytes: headerBytes,
+1 -1
View File
@@ -776,7 +776,7 @@ func (r *RTPStatsSender) DeltaInfoSender(senderSnapshotID uint32) *RTPDeltaInfo
return &RTPDeltaInfo{
StartTime: startTime,
Duration: endTime.Sub(startTime),
EndTime: endTime,
Packets: packetsExpected - uint32(now.packetsPadding-then.packetsPadding),
Bytes: now.bytes - then.bytes,
HeaderBytes: now.headerBytes - then.headerBytes,
+4 -2
View File
@@ -22,6 +22,7 @@ import (
"github.com/frostbyte73/core"
"github.com/pion/webrtc/v3"
"go.uber.org/atomic"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
@@ -205,7 +206,7 @@ func (cs *ConnectionStats) updateScoreWithAggregate(agg *buffer.RTPDeltaInfo, at
var stat windowStat
if agg != nil {
stat.startedAt = agg.StartTime
stat.duration = agg.Duration
stat.duration = agg.EndTime.Sub(agg.StartTime)
stat.packetsExpected = agg.Packets + agg.PacketsPadding
stat.packetsLost = agg.PacketsLost
stat.packetsMissing = agg.PacketsMissing
@@ -263,7 +264,6 @@ func (cs *ConnectionStats) updateScoreFromReceiverReport(at time.Time) (float32,
}
if streamingStartedAt.After(agg.StartTime) {
agg.Duration = agg.StartTime.Add(agg.Duration).Sub(streamingStartedAt)
agg.StartTime = streamingStartedAt
}
return cs.updateScoreWithAggregate(agg, at), streams
@@ -428,6 +428,8 @@ func toAnalyticsStream(ssrc uint32, deltaStats *buffer.RTPDeltaInfo) *livekit.An
packetsLost -= deltaStats.PacketsMissing
}
return &livekit.AnalyticsStream{
StartTime: timestamppb.New(deltaStats.StartTime),
EndTime: timestamppb.New(deltaStats.EndTime),
Ssrc: ssrc,
PrimaryPackets: deltaStats.Packets,
PrimaryBytes: deltaStats.Bytes,
@@ -75,7 +75,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 250,
},
},
@@ -91,7 +91,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 120,
PacketsLost: 30,
},
@@ -99,7 +99,7 @@ func TestConnectionQuality(t *testing.T) {
2: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 130,
PacketsLost: 0,
},
@@ -118,7 +118,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 250,
},
},
@@ -134,7 +134,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 250,
},
},
@@ -150,7 +150,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 250,
},
},
@@ -166,7 +166,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 250,
PacketsLost: 13,
},
@@ -183,7 +183,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 250,
},
},
@@ -199,7 +199,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 250,
},
},
@@ -215,7 +215,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 250,
PacketsLost: 30,
},
@@ -240,7 +240,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 0,
},
},
@@ -256,7 +256,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 0,
},
},
@@ -281,7 +281,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 250,
PacketsLost: 0,
},
@@ -301,7 +301,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 50,
PacketsLost: 5,
},
@@ -323,7 +323,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 250,
PacketsLost: 5,
RttMax: 400,
@@ -349,7 +349,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 250,
Bytes: 8_000_000 / 8 / 5,
},
@@ -368,7 +368,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 250,
Bytes: 8_000_000 / 8 / 5,
},
@@ -392,7 +392,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 250,
Bytes: 8_000_000 / 8 / 5,
},
@@ -419,7 +419,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 250,
Bytes: 8_000_000 / 8 / 5,
},
@@ -453,7 +453,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 250,
PacketsLost: 5,
RttMax: 700,
@@ -488,7 +488,7 @@ func TestConnectionQuality(t *testing.T) {
1: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 250,
PacketsLost: 5,
JitterMax: 200,
@@ -657,7 +657,7 @@ func TestConnectionQuality(t *testing.T) {
123: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: tc.packetsExpected,
PacketsLost: uint32(math.Ceil(eq.packetLossPercentage * float64(tc.packetsExpected) / 100.0)),
},
@@ -761,7 +761,7 @@ func TestConnectionQuality(t *testing.T) {
123: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 100,
Bytes: tc.bytes,
},
@@ -855,7 +855,7 @@ func TestConnectionQuality(t *testing.T) {
123: {
RTPStats: &buffer.RTPDeltaInfo{
StartTime: now,
Duration: duration,
EndTime: now.Add(duration),
Packets: 200,
},
},
+1 -1
View File
@@ -505,7 +505,7 @@ func (q *qualityScorer) isPaused() bool {
}
func (q *qualityScorer) getPacketLossWeight(stat *windowStat) float64 {
if stat == nil || stat.duration == 0 {
if stat == nil || stat.duration <= 0 {
return q.params.PacketLossWeight
}
+14
View File
@@ -158,6 +158,8 @@ func coalesce(stats []*livekit.AnalyticsStat) *livekit.AnalyticsStat {
}
// find aggregates across streams
startTime := time.Time{}
endTime := time.Time{}
scoreSum := float32(0.0) // used for average
minScore := float32(0.0) // min score in batched stats
var scores []float32 // used for median
@@ -183,6 +185,16 @@ func coalesce(stats []*livekit.AnalyticsStat) *livekit.AnalyticsStat {
}
for _, analyticsStream := range stat.Streams {
start := analyticsStream.StartTime.AsTime()
if startTime.IsZero() || startTime.After(start) {
startTime = start
}
end := analyticsStream.EndTime.AsTime()
if endTime.IsZero() || endTime.Before(end) {
endTime = end
}
if analyticsStream.Rtt > maxRtt {
maxRtt = analyticsStream.Rtt
}
@@ -216,6 +228,8 @@ func coalesce(stats []*livekit.AnalyticsStat) *livekit.AnalyticsStat {
}
}
}
coalescedStream.StartTime = timestamppb.New(startTime)
coalescedStream.EndTime = timestamppb.New(endTime)
coalescedStream.Rtt = maxRtt
coalescedStream.Jitter = maxJitter