欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

GoFrame基于性能測(cè)試得知grpool使用場(chǎng)景

 更新時(shí)間:2022年06月20日 17:35:14   作者:王中陽(yáng)Go  
這篇文章主要為大家介紹了GoFrame基于性能測(cè)試得知grpool使用場(chǎng)景示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

前言摘要

之前寫(xiě)了一篇 grpool goroutine池詳解 | 協(xié)程管理 收到了大家積極的反饋,今天這篇來(lái)做一下grpool的性能測(cè)試分析,讓大家更好的了解什么場(chǎng)景下使用grpool比較好。

先說(shuō)結(jié)論

grpool相比于goroutine更節(jié)省內(nèi)存,但是耗時(shí)更長(zhǎng);

原因也很簡(jiǎn)單:grpool復(fù)用了協(xié)程,減少了協(xié)程的創(chuàng)建和銷(xiāo)毀,減少了內(nèi)存消耗;也因?yàn)閰f(xié)程的復(fù)用,總的goroutine數(shù)量更少,導(dǎo)致耗時(shí)更多。

測(cè)試性能代碼

開(kāi)啟for循環(huán),開(kāi)啟一萬(wàn)個(gè)協(xié)程,分別使用原生goroutine和grpool執(zhí)行。

看兩者在內(nèi)存占用和耗時(shí)方面的差別。

package main
import (
   "flag"
   "fmt"
   "github.com/gogf/gf/os/grpool"
   "github.com/gogf/gf/os/gtime"
   "log"
   "os"
   "runtime"
   "runtime/pprof"
   "sync"
   "time"
)
func main() {
   //接收命令行參數(shù)
   flag.Parse()
   //cpu分析
   cpuProfile()
   //主邏輯
   //demoGrpool()
   demoGoroutine()
   //內(nèi)存分析
   memProfile()
}
func demoGrpool() {
   start := gtime.TimestampMilli()
   wg := sync.WaitGroup{}
   for i := 0; i < 10000; i++ {
      wg.Add(1)
      _ = grpool.Add(func() {
         var m runtime.MemStats
         runtime.ReadMemStats(&m)
         fmt.Printf("運(yùn)行中占用內(nèi)存:%d Kb\n", m.Alloc/1024)
         time.Sleep(time.Millisecond)
         wg.Done()
      })
      fmt.Printf("運(yùn)行的協(xié)程:", grpool.Size())
   }
   wg.Wait()
   fmt.Printf("運(yùn)行的時(shí)間:%v ms \n", gtime.TimestampMilli()-start)
   select {}
}
func demoGoroutine() {
   //start := gtime.TimestampMilli()
   wg := sync.WaitGroup{}
   for i := 0; i < 10000; i++ {
      wg.Add(1)
      go func() {
         //var m runtime.MemStats
         //runtime.ReadMemStats(&m)
         //fmt.Printf("運(yùn)行中占用內(nèi)存:%d Kb\n", m.Alloc/1024)
         time.Sleep(time.Millisecond)
         wg.Done()
      }()
   }
   wg.Wait()
   //fmt.Printf("運(yùn)行的時(shí)間:%v ms \n", gtime.TimestampMilli()-start)
}
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile `file`")
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
func cpuProfile() {
   if *cpuprofile != "" {
      f, err := os.Create(*cpuprofile)
      if err != nil {
         log.Fatal("could not create CPU profile: ", err)
      }
      if err := pprof.StartCPUProfile(f); err != nil { //監(jiān)控cpu
         log.Fatal("could not start CPU profile: ", err)
      }
      defer pprof.StopCPUProfile()
   }
}
func memProfile() {
   if *memprofile != "" {
      f, err := os.Create(*memprofile)
      if err != nil {
         log.Fatal("could not create memory profile: ", err)
      }
      runtime.GC()                                      // GC,獲取最新的數(shù)據(jù)信息
      if err := pprof.WriteHeapProfile(f); err != nil { // 寫(xiě)入內(nèi)存信息
         log.Fatal("could not write memory profile: ", err)
      }
      f.Close()
   }
}

運(yùn)行結(jié)果

組件占用內(nèi)存耗時(shí)
grpool2229 Kb1679 ms
goroutine5835 Kb1258 ms

總結(jié)

goframe的grpool節(jié)省內(nèi)存,如果機(jī)器的內(nèi)存不高或者業(yè)務(wù)場(chǎng)景對(duì)內(nèi)存占用的要求更高,則使用grpool。

如果機(jī)器的內(nèi)存足夠,但是對(duì)應(yīng)用的執(zhí)行時(shí)間有更高的追求,就用原生的goroutine。

更多關(guān)于GoFrame性能測(cè)試grpool使用場(chǎng)景的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論