arl / statsviz

🚀 Visualise your Go program runtime metrics in real time in the browser
MIT License
3.19k stars 121 forks source link

statsviz.TimeSeries.GetValue is called multiple times per second #119

Open mzzsfy opened 8 months ago

mzzsfy commented 8 months ago

Open 2 or more pages and you will see the phenomenon

code like this:

statsviz.NewServer(statsviz.TimeseriesPlot(func() statsviz.TimeSeriesPlot {
        plot, _ := statsviz.TimeSeriesPlotConfig{
            Name:  "testCall",
            Title: "testCall",
            Type:  statsviz.Scatter,
            Series: []statsviz.TimeSeries{{
                Name:     "call",
                GetValue: func() float64 {
                    println("call",time.Now().Format(time.DateTime))
                    return float64(0)
                },
            }},
        }.Build()
        return plot
    }()))

Here's the log with 3 pages opened image

arl commented 8 months ago

Currently statsviz creates a new connection for each client/browser that connects to it. For each connection there's a once per second timer that gets the stats.

The orignal use case wasn't to have more than one client. Ok that's a bug. Thanks for reporting!

AlekSi commented 8 months ago

I was sure it was a feature and built our own locking around it 😆

arl commented 8 months ago

Bugs, features... That's just different point of views 😅😅

But yes I think that statsviz should be restructured so that there's a single timer for N client connections. Shouldn't be too hard