您现在的位置是:热点 >>正文
Go 程序运行时数据统计的可视化工具 Statsviz
热点0921人已围观
简介...
本文转载自微信公众号「 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 connection& 程nbsp;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.HandlerFunc&序运nbsp;{ return func(w http.ResponseWriter, r *http.Request) { var upgrader = websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, } ws, err := upgrader.Upgrade(w, 行时;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) error 统计{ tick := time.NewTicker(frequency) defer tick.Stop() var ( stats stats err error ) for range tick.C { runtime.ReadMemStats(&stats.Mem) stats.NumGoroutine = runtime.NumGor的可outine() if err = conn.WriteJSON(stats); err != nil { break } } return err }
3. 其实这个项目比较核心的代码是前端 JavaScript 代码:
m.pushData = function (ts, allStats) { data.time视化s.push(ts); // timestamp const memStats = allStats.Mem; data.gcfraction.push(memStats.GCCPUFraction); data.goroutines.push(allStats.NumGoroutine); &nb工具sp; data.heap[idxHeapAlloc].push(memStats.HeapAlloc); data.heap[idxHeapSys].push(memStats.HeapSys); data.heap[idxHeapIdle].push(memStats.HeapIdle); data.heap[idxHeapInuse].push(memStats.HeapInuse); &nb Ssp; data.heap[idxHeapNextGC].push(memStats.NextGC); data.mspanMCache[idxMSpanMCacheMSpanInUse].push(memStats.MSpanInuse); data.mspanMCache[idxMSpanMCacheMSpanSys].push(memStats.MSpanSys); &nbtasp; 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++) { vi const size = memStats.BySize[i]; data.bySize[i].push(size.Mallocs - size.Frees); } updateLastGC(memStats); &nGobsp; }
渲染效果是通过 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):
Tags:
相关文章
Meta推出AI视频生成平台Vibes,轻松创作和分享 AI 短视频
热点Meta 公司最新推出的 AI 应用程序版本引入了 Vibes,一个创新的平台,让用户可以轻松创建和分享 AI 生成的短视频。Vibes 旨在为用户提供创意灵感,并借助 Meta AI 的强大工具,激...
【热点】
阅读更多标签: 福特 F-150 电动皮卡 特朗普关税
热点云梦级2月11日云梦级消息,福特公布第四季度净亏损为111亿美元,此前曾披露其电动汽车项目出现大幅减值。本季度调整后每股收益为13美分,低于分析师预测的每股19美分。 福特股价在盘后交易中上涨近2%。...
【热点】
阅读更多灏忕背闆嗗洟娓�偂娑ㄨ秴5锛� 闆峰啗绉板皬绫虫墜鏈洪攢閲忚秴鑻规灉鏅嬪崌鍏ㄧ悆绗�簩
热点鏌ョ湅鏈€鏂拌�鎯� 鏂版氮绉戞妧璁� 7鏈�16鏃ヤ笂鍗堟秷鎭�紝鎴�嚦鐩�墠锛屽皬绫抽泦鍥㈡腐鑲℃定瓒�5%锛屾姤28.35娓�厓姣忚偂锛屽競鍊肩害7111.14浜挎腐鍏冦€備粖鏃ユ棭浜涙椂鍊欙紝...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.temu.com/search_result.html?search_key=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】B1T
- https://www.lindseymedicalsupply.com/search/谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,0qk
- https://www.dba.dk/soeg/?soeg=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】3KS
- https://www.synonyms.com/antonyms/谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,kZv
- https://www.pierreherme.com/catalogsearch/result/?q=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,uXc
- https://www.rotherham.gov.uk/site-search/results/?q=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,XZS
- https://www.flickr.com/photos/tags/谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】B1T
- https://wydden.com/?ID=r8w9WMSKWkOcmiaFWaUv0dzMdqZWfKtF&s=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,MlH
- https://www.tmtpost.com/search?q=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,ArT
- https://www.youtube.com/hashtag/谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,h5H
- https://www.rostaing.com/catalogsearch/result/?cat=22&q=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,ZTy
- https://www.yidian51.com/taobao/谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,jOd
- https://www.laoairlines.com/?action=QUERY&s=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,dPc
- https://www.sdcard.org/ja/?s=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,uLr
- https://www.zutari.com/?s=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,ZV5
- https://www.resilienz-akademie.com/?cat=71&s=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,b81
- https://www.youtube.com/results?search_query=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,34F
- https://www.roberthalf.com/us/en/jobs/all/谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】ZUc
- https://www.remit.co.jp/vi/?s=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】mpf
- https://www.tullygroup.us/?ID=hbkq8jFjrQlVRrwzEmvoUJJAY4e1Lfo5&s=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,X1D
- https://www.abbreviations.com/谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】Qp4
- https://www.varian.com/en-au/search?cat=113&search=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,J5w
- https://www2.ual.es/ProyectoLates/?s=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,DL0
- https://www.madebyoranges.com/nl/search/谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,1DP
- https://www.maafushidive.com?collections?vendors?q=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,Ut4
- https://www.lgnewsroom.com/search/谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,VCW
- https://www.simcoemuskokahealth.org/SearchResults.aspx?SearchQuery=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,7Ks
- https://www.mingwai.edu.hk/?langzh-hant&s=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,qrf
- 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】,688
- https://www.krisinformation.se/en/search?q=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,Pqn
- https://www.paxnet.co.kr/search/jongmok?kwd=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】lrR
- https://www.nextavenue.org/?action=QUERY&s=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,xUB
- https://www.terramania.nl/catalogsearch/result/index/?q=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,w9U
- https://www.tarleton.edu/?s=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,tyS
- https://www.rea.govt.nz/search?searchterm=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,4Ce
- https://www.remit.co.jp/vi/?s=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】Fh0
- https://www.prattlibrary.org/search?q=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,H0i
- https://www.udc.co.uk/index.php?route=product/search&sort=p.price&order=DESC&tag=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,P8z
- https://www.sic-info.org/?s=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,h5q
- https://www.goodreads.com/quotes/tag/谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】nSQ
- https://www.roberthalf.com/us/en/jobs/all/谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】YTe
- https://www.universal-music.co.jp/?ID=1sIcckCTeBS8FjzCshAW9w53s8gr691o&s=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,a0j
- https://www.routesonline.com/search/?query=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,0eS
- https://www.teamlab.art/pt/?submit=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,A8k
- https://www.stonerealestate.com.au/?cat=100&s=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,xim
- https://www.rhymezone.com/r/rhyme.cgi?typeofrhyme=oldrel&loc=nojs&Word=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,OsE
- https://www.sonepar.ch/fr/search?q=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,7gq
- https://www.oikos-tecnics.com/?s=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,4Zz
- https://www.kaskus.co.id/search?q=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,uSa
- https://www.so.com/s?ie=utf-8&src=360sou_home&_re=0&q=谷歌留痕技术教程-教程要点与实践【tg:seogoogle987】,NAF