偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Go 程序運(yùn)行時(shí)數(shù)據(jù)統(tǒng)計(jì)的可視化工具 Statsviz

開發(fā) 后端 大數(shù)據(jù)
今天跟大家介紹一款實(shí)時(shí)可視化 Go 程序運(yùn)行時(shí)數(shù)據(jù)統(tǒng)計(jì)的工具 statsviz。

 [[424685]]

本文轉(zhuǎn)載自微信公眾號(hào)「 Go 夜讀」,作者 Go 夜讀 。轉(zhuǎn)載本文請(qǐng)聯(lián)系 Go 夜讀公眾號(hào)。

今天跟大家介紹一款實(shí)時(shí)可視化 Go 程序運(yùn)行時(shí)數(shù)據(jù)統(tǒng)計(jì)的工具 statsviz

https://github.com/arl/statsviz 

它的圖形化展現(xiàn)對(duì)于我們了解 Go 程序的 GC 行為,以及內(nèi)存開銷等很有用!

使用也很簡(jiǎn)單:

1. go get github.com/arl/statsviz

2. 在你的 http.ServeMux 上注冊(cè)

  1. mux := http.NewServeMux() 
  2. statsviz.Register(mux) 

或者使用默認(rèn) http 注冊(cè):

  1. statsviz.RegisterDefault() 

如果你的程序不是一個(gè) http 應(yīng)用程序,那么你可以添加以下代碼來(lái)啟動(dòng)

  1. go func() { 
  2.     log.Println(http.ListenAndServe("localhost:6060", nil)) 
  3. }() 

這段代碼,我相信大家都非常熟悉了吧~

當(dāng)我們將啟動(dòng)之后,我們可以直接在瀏覽器中打開:

http://localhost:6060/debug/statsviz/

看起來(lái)酷炫高大上,其實(shí)主要還是依賴于 Go 為我們提供的 runtime stats

具體我們來(lái)看看它的一些代碼:

1. websocket handler

  1. // NewWsHandler returns a handler that upgrades the HTTP server connection to the WebSocket 
  2. // protocol and sends application statistics at the given frequency. 
  3. // 
  4. // If the upgrade fails, an HTTP error response is sent to the client. 
  5. func NewWsHandler(frequency time.Duration) http.HandlerFunc { 
  6.  return func(w http.ResponseWriter, r *http.Request) { 
  7.   var upgrader = websocket.Upgrader{ 
  8.    ReadBufferSize:  1024, 
  9.    WriteBufferSize: 1024, 
  10.   } 
  11.  
  12.   ws, err := upgrader.Upgrade(w, r, nil) 
  13.   if err != nil { 
  14.    return 
  15.   } 
  16.   defer ws.Close() 
  17.  
  18.   // Explicitly ignore this error. We don't want to spam standard output 
  19.   // each time the other end of the websocket connection closes. 
  20.   _ = sendStats(ws, frequency) 
  21.  } 

2. sendStats

  1. // sendStats indefinitely send runtime statistics on the websocket connection
  2. func sendStats(conn *websocket.Conn, frequency time.Duration) error { 
  3.  tick := time.NewTicker(frequency) 
  4.  defer tick.Stop() 
  5.  
  6.  var ( 
  7.   stats stats 
  8.   err   error 
  9.  ) 
  10.  for range tick.C { 
  11.   runtime.ReadMemStats(&stats.Mem) 
  12.   stats.NumGoroutine = runtime.NumGoroutine() 
  13.   if err = conn.WriteJSON(stats); err != nil { 
  14.    break 
  15.   } 
  16.  } 
  17.  
  18.  return err 

3. 其實(shí)這個(gè)項(xiàng)目比較核心的代碼是前端 JavaScript 代碼:

  1. m.pushData = function (ts, allStats) { 
  2.         data.times.push(ts); // timestamp 
  3.  
  4.         const memStats = allStats.Mem; 
  5.  
  6.         data.gcfraction.push(memStats.GCCPUFraction); 
  7.         data.goroutines.push(allStats.NumGoroutine); 
  8.  
  9.         data.heap[idxHeapAlloc].push(memStats.HeapAlloc); 
  10.         data.heap[idxHeapSys].push(memStats.HeapSys); 
  11.         data.heap[idxHeapIdle].push(memStats.HeapIdle); 
  12.         data.heap[idxHeapInuse].push(memStats.HeapInuse); 
  13.         data.heap[idxHeapNextGC].push(memStats.NextGC); 
  14.  
  15.         data.mspanMCache[idxMSpanMCacheMSpanInUse].push(memStats.MSpanInuse); 
  16.         data.mspanMCache[idxMSpanMCacheMSpanSys].push(memStats.MSpanSys); 
  17.         data.mspanMCache[idxMSpanMSpanMSCacheInUse].push(memStats.MCacheInuse); 
  18.         data.mspanMCache[idxMSpanMSpanMSCacheSys].push(memStats.MCacheSys); 
  19.  
  20.         data.objects[idxObjectsLive].push(memStats.Mallocs - memStats.Frees); 
  21.         data.objects[idxObjectsLookups].push(memStats.Lookups); 
  22.         data.objects[idxObjectsHeap].push(memStats.HeapObjects); 
  23.  
  24.         for (let i = 0; i < memStats.BySize.length; i++) { 
  25.             const size = memStats.BySize[i]; 
  26.             data.bySize[i].push(size.Mallocs - size.Frees); 
  27.         } 
  28.  
  29.         updateLastGC(memStats); 
  30.     } 

渲染效果是通過(guò) https://github.com/arl/statsviz/blob/master/static/plotly-basic.min.js 渲染所得。

Plotly.js 簡(jiǎn)介:它是一款開源的 JavaScript 圖表庫(kù),它基于 d3.js 和 stack.gl 。是一個(gè)高層次的、描述性的圖表庫(kù)。plotly.js 帶來(lái) 20 種圖表類型,包括 3D 圖表,統(tǒng)計(jì)圖表,和 SVG 地圖。

我們來(lái)看一個(gè)官網(wǎng)示例,很酷炫

我們?cè)賮?lái)看看 statsviz 在 GitHub 倉(cāng)庫(kù)上提供的一些 demo 示意圖(方便大家來(lái)直觀的感受 statsviz):

如果你已經(jīng)用過(guò)它或者有其他可視化工具,歡迎你來(lái)評(píng)論。

 

責(zé)任編輯:武曉燕 來(lái)源: Go夜讀
相關(guān)推薦

2020-12-07 13:31:43

GoMutex開發(fā)者

2022-05-07 09:02:27

數(shù)據(jù)可視化工具庫(kù)

2022-11-15 15:14:05

2025-03-28 01:00:00

Go語(yǔ)言版本

2022-08-15 08:02:09

Go程序函數(shù)

2018-11-22 12:07:37

Java虛擬機(jī)結(jié)構(gòu)

2022-09-22 15:42:02

機(jī)器學(xué)習(xí)異常值工具

2020-07-16 15:10:46

工具可視化Python

2019-12-23 14:17:46

數(shù)據(jù)可視化工具庫(kù)

2019-09-27 09:12:18

開源數(shù)據(jù)可視化大數(shù)據(jù)

2017-07-25 13:42:00

大數(shù)據(jù)可視化工具

2019-10-14 15:51:40

可視化技術(shù)微軟數(shù)據(jù)庫(kù)

2021-03-30 10:10:37

PyTorch可視化工具命令

2017-07-03 16:44:10

數(shù)據(jù)庫(kù)MongoDBNoSQL

2022-03-21 11:07:43

JVM內(nèi)存字節(jié)碼

2018-09-28 17:16:18

數(shù)據(jù)可視化工具發(fā)展趨勢(shì)

2021-06-11 17:45:57

大數(shù)據(jù)可視化工具

2021-07-14 07:21:57

JVM運(yùn)行數(shù)據(jù)

2018-05-31 08:25:13

誤區(qū)工具可視化

2017-07-04 16:00:16

PythonMatplotlib可視化工具
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)