From 2b9f46b63ec4be652ef58316046c3ab651722797 Mon Sep 17 00:00:00 2001 From: hexbabe Date: Wed, 20 Nov 2024 16:37:55 -0500 Subject: [PATCH 1/3] Init --- components/camera/collector-diff.diff | 51 +++++++++++++++++++++++++++ data/collector.go | 14 ++++++++ web/cmd/server/main.go | 9 +++++ 3 files changed, 74 insertions(+) create mode 100644 components/camera/collector-diff.diff diff --git a/components/camera/collector-diff.diff b/components/camera/collector-diff.diff new file mode 100644 index 00000000000..556b8c36291 --- /dev/null +++ b/components/camera/collector-diff.diff @@ -0,0 +1,51 @@ +diff --git a/data/collector.go b/data/collector.go +index 707014114..a7a2544ec 100644 +--- a/data/collector.go ++++ b/data/collector.go +@@ -78,6 +78,12 @@ type collector struct { + captureFunc CaptureFunc + target CaptureBufferedWriter + lastLoggedErrors map[string]int64 ++ ++ lastCaptureTime time.Time ++ captureCount int64 ++ captureMutex sync.Mutex ++ runningFrequency float64 ++ alpha float64 + } + + // Close closes the channels backing the Collector. It should always be called before disposing of a Collector to avoid +@@ -196,6 +202,25 @@ func (c *collector) getAndPushNextReading() { + return + } + ++ // debug freq calculation ++ c.captureMutex.Lock() ++ defer c.captureMutex.Unlock() ++ ++ if !c.lastCaptureTime.IsZero() { ++ elapsed := timeReceived.AsTime().Sub(c.lastCaptureTime).Seconds() ++ if elapsed > 0 { ++ frequency := 1.0 / elapsed ++ if c.runningFrequency == 0 { ++ c.runningFrequency = frequency ++ } else { ++ c.runningFrequency = c.alpha*frequency + (1-c.alpha)*c.runningFrequency ++ } ++ c.logger.Infow("capture frequency", "frequency_hz", frequency, "running_average_hz", c.runningFrequency) ++ } ++ } ++ c.lastCaptureTime = timeReceived.AsTime() ++ c.captureCount++ ++ + var msg v1.SensorData + switch v := reading.(type) { + case []byte: +@@ -279,6 +304,7 @@ func NewCollector(captureFunc CaptureFunc, params CollectorParams) (Collector, e + target: params.Target, + clock: c, + lastLoggedErrors: make(map[string]int64, 0), ++ alpha: 0.1, + }, nil + } + diff --git a/data/collector.go b/data/collector.go index 7070141147e..cb710c4686a 100644 --- a/data/collector.go +++ b/data/collector.go @@ -78,6 +78,8 @@ type collector struct { captureFunc CaptureFunc target CaptureBufferedWriter lastLoggedErrors map[string]int64 + + lastCaptureTime time.Time } // Close closes the channels backing the Collector. It should always be called before disposing of a Collector to avoid @@ -196,6 +198,18 @@ func (c *collector) getAndPushNextReading() { return } + // debug freq calculation + if !c.lastCaptureTime.IsZero() { + elapsed := timeReceived.AsTime().Sub(c.lastCaptureTime).Seconds() + if elapsed > 0 { + frequency := 1.0 / elapsed + c.logger.Infow("capture frequency", "frequency_hz", frequency) + } else { + panic("oh no") + } + } + c.lastCaptureTime = timeReceived.AsTime() + var msg v1.SensorData switch v := reading.(type) { case []byte: diff --git a/web/cmd/server/main.go b/web/cmd/server/main.go index e8d3816d498..87f0266f471 100644 --- a/web/cmd/server/main.go +++ b/web/cmd/server/main.go @@ -3,12 +3,18 @@ package main import ( + "log" + "net/http" + "go.viam.com/utils" // registers all components. _ "go.viam.com/rdk/components/register" "go.viam.com/rdk/logging" + // registers all services. + _ "net/http/pprof" + _ "go.viam.com/rdk/services/register" "go.viam.com/rdk/web/server" ) @@ -16,5 +22,8 @@ import ( var logger = logging.NewDebugLogger("entrypoint") func main() { + go func() { + log.Println(http.ListenAndServe("localhost:6061", nil)) + }() utils.ContextualMain(server.RunServer, logger) } From 4e9317a1761bd24080057cb865705d25771cfe4a Mon Sep 17 00:00:00 2001 From: hexbabe Date: Wed, 20 Nov 2024 17:09:02 -0500 Subject: [PATCH 2/3] Change host --- web/cmd/server/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/cmd/server/main.go b/web/cmd/server/main.go index 87f0266f471..d17f178e0e1 100644 --- a/web/cmd/server/main.go +++ b/web/cmd/server/main.go @@ -23,7 +23,7 @@ var logger = logging.NewDebugLogger("entrypoint") func main() { go func() { - log.Println(http.ListenAndServe("localhost:6061", nil)) + log.Println(http.ListenAndServe("0.0.0.0:6061", nil)) }() utils.ContextualMain(server.RunServer, logger) } From 293db88841cd91b00627050a80f87afc96490b86 Mon Sep 17 00:00:00 2001 From: hexbabe Date: Thu, 21 Nov 2024 14:57:40 -0500 Subject: [PATCH 3/3] Add latency calc --- data/collector.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/data/collector.go b/data/collector.go index cb710c4686a..79c81197171 100644 --- a/data/collector.go +++ b/data/collector.go @@ -184,6 +184,8 @@ func (c *collector) getAndPushNextReading() { timeRequested := timestamppb.New(c.clock.Now().UTC()) reading, err := c.captureFunc(c.cancelCtx, c.params) timeReceived := timestamppb.New(c.clock.Now().UTC()) + latency := timeReceived.AsTime().Sub(timeRequested.AsTime()) + elapsed := timeReceived.AsTime().Sub(c.lastCaptureTime).Seconds() if c.cancelCtx.Err() != nil { return @@ -198,12 +200,15 @@ func (c *collector) getAndPushNextReading() { return } - // debug freq calculation + // debug on success if !c.lastCaptureTime.IsZero() { - elapsed := timeReceived.AsTime().Sub(c.lastCaptureTime).Seconds() if elapsed > 0 { frequency := 1.0 / elapsed - c.logger.Infow("capture frequency", "frequency_hz", frequency) + c.logger.Infow("capture metrics", + "frequency_hz", frequency, + "latency_ms", latency.Milliseconds(), + "time_received", timeReceived.AsTime(), + ) } else { panic("oh no") }