您现在的位置是:貌合心离网 > 热点
Go 程序运行时数据统计的可视化工具 Statsviz
貌合心离网2026-11-14【热点】{固定数字1}人已围观
简介谷歌蜘蛛池租用【tg:seogoogle987】
本文转载自微信公众号「 Go 夜读」,作者 Go 夜读 。转载本文请联系 Go 夜读公众号。
今天跟大家介绍一款实时可视化 Go 程序运行时数据统计的工具 statsviz
https://github.com/arl/statsviz
它的图形化展现对于我们了解 Go 程序的 GC 行为,以及内存开销等很有用!
使用也很简单:
1. go get github.com/arl/statsviz
2. 在你的 http.ServeMux 上注册
mux := http.NewServeMux() statsviz.Register(mux)
或者使用默认 http 注册:
statsviz.RegisterDefault()
如果你的程序不是一个 http 应用程序,那么你可以添加以下代码来启动
go func() &Go#123; log.Println(http.ListenAndServe("localhost:6060", nil)) }()
这段代码,我相信大家都非常熟悉了吧~
当我们将启动之后,我们可以直接在浏览器中打开:
http://localhost:6060/debug/statsviz/
看起来酷炫高大上,其实主要还是依赖于 Go 为我们提供的 runtime stats
具体我们来看看它的一些代码:
1. websocket handler // NewWsHandler returns a handler that upgrades the HTTP server connect 程ion to the WebSocket // protocol and sends application statistics at the given frequency. // // If the upgrade fails, an HTTP error response is sent to the client. func NewWsHandler(frequency time.Duration) http.Handle序运rFunc { return func(w http.ResponseWriter, r *http.Request) { var upgrader = websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, } ws, err := upgrader.Upgrade(w,&nbs行时p;r, nil) if err != nil { return } defer ws.Close() // Explicitly ignore this error. We don't want to spam standard output // each time the other end数据 of the websocket connection closes. _ = sendStats(ws, frequency) } } 2. sendStats // sendStats indefinitely send runtime statistics on the websocket connection. func sendStats(conn *websocket.Conn, frequency time.Duration) erro统计r { tick := time.NewTicker(frequency) defer tick.Stop() var ( stats stats err error ) for range tick.C { runtime.ReadMemStats(&stats.Mem) stats.NumGoroutine = runtime的可.NumGoroutine() if err = conn.WriteJSON(stats); err != nil { break } } return err }
3. 其实这个项目比较核心的代码是前端 JavaScript 代码:
m.pushData = function (ts, allStats) { dat视化a.times.push(ts); // timestamp const memStats = allStats.Mem; data.gcfraction.push(memStats.GCCPUFraction); data.goroutines.push(allStats.NumGoroutine); &n工具bsp; data.heap[idxHeapAlloc].push(memStats.HeapAlloc); data.heap[idxHeapSys].push(memStats.HeapSys); data.heap[idxHeapIdle].push(memStats.HeapIdle); data.heap[idxHeapInuse].push(memStats.HeapInuse);&n Sbsp; data.heap[idxHeapNextGC].push(memStats.NextGC); data.mspanMCache[idxMSpanMCacheMSpanInUse].push(memStats.MSpanInuse); data.mspanMCache[idxMSpanMCacheMSpanSys].push(memStats.MSpanSys); ta data.mspanMCache[idxMSpanMSpanMSCacheInUse].push(memStats.MCacheInuse); data.mspanMCache[idxMSpanMSpanMSCacheSys].push(memStats.MCacheSys); data.objects[idxObjectsLive].push(memStats.Mallocs - memStats.Frees); &ntsbsp; data.objects[idxObjectsLookups].push(memStats.Lookups); data.objects[idxObjectsHeap].push(memStats.HeapObjects); for (let i = 0; i < memStats.BySize.length; i++) { &nvibsp; const size = memStats.BySize[i]; data.bySize[i].push(size.Mallocs - size.Frees); } updateLastGC(memStats); &Gonbsp; }
渲染效果是通过 https://github.com/arl/statsviz/blob/master/static/plotly-basic.min.js 渲染所得。
Plotly.js 简介:它是一款开源的 JavaScript 图表库,它基于 d3.js 和 stack.gl 。是一个高层次的、描述性的图表库。plotly.js 带来 20 种图表类型,包括 3D 图表,统计图表,和 SVG 地图。
我们来看一个官网示例,很酷炫
我们再来看看 statsviz 在 GitHub 仓库上提供的一些 demo 示意图(方便大家来直观的感受 statsviz):
很赞哦!(6677)
站长推荐
友情链接
- 标签: 康师傅
- https://www.maine.gov/pfr/professionallicensing/search/node?keys=谷歌蜘蛛池租用【tg:seogoogle987】,EkF
- https://hsmoa.com/search?query=谷歌蜘蛛池租用【tg:seogoogle987】yXP
- https://www.prattlibrary.org/search?q=谷歌蜘蛛池租用【tg:seogoogle987】,96s
- https://www.military-history.org/?s=谷歌蜘蛛池租用【tg:seogoogle987】,hYD
- https://www.thealexandrian.net/?s=谷歌蜘蛛池租用【tg:seogoogle987】,64P
- https://www.simcoemuskokahealth.org/SearchResults.aspx?SearchQuery=谷歌蜘蛛池租用【tg:seogoogle987】,aEA
- https://www.libros.unam.mx/catalogsearch/result/?cat=37&dir=asc&format=20&lit=20&manufacturer=3230&mode=list&order=year_edition&q=谷歌蜘蛛池租用【tg:seogoogle987】,o5i
- https://www.impactpool.org/search/?type=79&q=谷歌蜘蛛池租用【tg:seogoogle987】JCI
- https://jihoon471.cafe24.com/product/search.html?banner_action=&keyword=谷歌蜘蛛池租用【tg:seogoogle987】R9i
- https://www.rockngem.com/?action=CHECK&s=谷歌蜘蛛池租用【tg:seogoogle987】,LMJ
- https://dict.naver.com/dict.search?dicQuery&query=谷歌蜘蛛池租用【tg:seogoogle987】Kjv
- https://www.parl.ca/legisinfo/fr/projets-de-loi?parlsession=all&view=list&keywords=谷歌蜘蛛池租用【tg:seogoogle987】,hTO
- https://www.nyif.com/catalogsearch/result/?q=谷歌蜘蛛池租用【tg:seogoogle987】,otO
- https://www.pierreherme.com/catalogsearch/result/?q=谷歌蜘蛛池租用【tg:seogoogle987】,QHB
- https://www.qylaws.com/search/谷歌蜘蛛池租用【tg:seogoogle987】,j91
- https://www.teamlab.art/ko/?submit=谷歌蜘蛛池租用【tg:seogoogle987】,LZX
- https://www.wattpad.com/search/谷歌蜘蛛池租用【tg:seogoogle987】7rL
- https://www.kaskus.co.id/search?q=谷歌蜘蛛池租用【tg:seogoogle987】,YJm
- https://www.flickr.com/photos/tags/谷歌蜘蛛池租用【tg:seogoogle987】rts
- https://www.temu.com/search_result.html?search_key=谷歌蜘蛛池租用【tg:seogoogle987】Ner
- https://www.nytimes.com/search?dropmab=true&query=谷歌蜘蛛池租用【tg:seogoogle987】,TJA
- https://www.jusoen.com/addreng.asp?p1=谷歌蜘蛛池租用【tg:seogoogle987】MT2
- https://www.lenovo.com/gb/en/search?action=SEARCH&text=谷歌蜘蛛池租用【tg:seogoogle987】,O6r
- https://www.otakuninjahero.com/en/search/谷歌蜘蛛池租用【tg:seogoogle987】,91f
- https://www.wellbees.com/catalogsearch/result/?action=CHECK&q=谷歌蜘蛛池租用【tg:seogoogle987】,s1U
- https://www.polliwogs.com/search/谷歌蜘蛛池租用【tg:seogoogle987】,Iab
- https://www.usslexington.com/?action=QUERY&s=谷歌蜘蛛池租用【tg:seogoogle987】,WCj
- https://www.repetto.jp/item/search?action=QUERY&q=谷歌蜘蛛池租用【tg:seogoogle987】,ZW6
- https://www.mynextmove.org/find/search?s=谷歌蜘蛛池租用【tg:seogoogle987】,UaY
- https://www.krisinformation.se/en/search?q=谷歌蜘蛛池租用【tg:seogoogle987】,XfS
- https://www.paspaley.com/catalogsearch/result/?action=QUERY&q=谷歌蜘蛛池租用【tg:seogoogle987】,VQv
- https://www.teamlab.art/es/?submit=谷歌蜘蛛池租用【tg:seogoogle987】,2yq
- https://www.springfieldlibrary.org/?s=谷歌蜘蛛池租用【tg:seogoogle987】,PRG
- https://www.sic-info.org/en/?s=谷歌蜘蛛池租用【tg:seogoogle987】,0AN
- https://www.luxurysociety.com/en/search?q=谷歌蜘蛛池租用【tg:seogoogle987】,tYf
- https://www.umc.edu/search.aspx?ID=I4gnDKRS991MFcIDDzoYze4aWSAszAm7&q=谷歌蜘蛛池租用【tg:seogoogle987】,MKr
- https://m.shoppinghow.kakao.com/m/search/q/谷歌蜘蛛池租用【tg:seogoogle987】voB
- https://www.wou.edu.my/?s=谷歌蜘蛛池租用【tg:seogoogle987】,KCB
- https://www.remit.co.jp/zh/?s=谷歌蜘蛛池租用【tg:seogoogle987】,ho4
- https://www.livehindustan.com/tags/谷歌蜘蛛池租用【tg:seogoogle987】,KJN
- https://www.unclefossil.com/search/谷歌蜘蛛池租用【tg:seogoogle987】,CBB
- https://www.pixiv.net/tags/谷歌蜘蛛池租用【tg:seogoogle987】,PQe
- https://www.ncetm.org.uk/search?action=CHECK&q=谷歌蜘蛛池租用【tg:seogoogle987】,2KY
- https://www.selfsufficientish.com/main/?s=谷歌蜘蛛池租用【tg:seogoogle987】,swI
- https://www.teacherspayteachers.com/Browse/Search:谷歌蜘蛛池租用【tg:seogoogle987】,ohR
- https://www.xvideos.com/tags/谷歌蜘蛛池租用【tg:seogoogle987】,RgK
- https://www.lyrics.com/lyrics/谷歌蜘蛛池租用【tg:seogoogle987】,KXT
- https://www.remit.co.jp/ne/?s=谷歌蜘蛛池租用【tg:seogoogle987】,8Kz
- https://www.partilerdunyasi.com/index.php?route=product/search&tag=谷歌蜘蛛池租用【tg:seogoogle987】,q6S
- https://www.world-architects.com/en/search?term=谷歌蜘蛛池租用【tg:seogoogle987】,hMn







