GoFrame基于性能測試得知grpool使用場景
前言摘要
之前寫了一篇 grpool goroutine池詳解 | 協(xié)程管理 收到了大家積極的反饋,今天這篇來做一下grpool的性能測試分析,讓大家更好的了解什么場景下使用grpool比較好。
先說結(jié)論
grpool相比于goroutine更節(jié)省內(nèi)存,但是耗時更長;
原因也很簡單:grpool復(fù)用了協(xié)程,減少了協(xié)程的創(chuàng)建和銷毀,減少了內(nèi)存消耗;也因為協(xié)程的復(fù)用,總的goroutine數(shù)量更少,導(dǎo)致耗時更多。
測試性能代碼
開啟for循環(huán),開啟一萬個協(xié)程,分別使用原生goroutine和grpool執(zhí)行。
看兩者在內(nèi)存占用和耗時方面的差別。
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("運行中占用內(nèi)存:%d Kb\n", m.Alloc/1024)
time.Sleep(time.Millisecond)
wg.Done()
})
fmt.Printf("運行的協(xié)程:", grpool.Size())
}
wg.Wait()
fmt.Printf("運行的時間:%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("運行中占用內(nèi)存:%d Kb\n", m.Alloc/1024)
time.Sleep(time.Millisecond)
wg.Done()
}()
}
wg.Wait()
//fmt.Printf("運行的時間:%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 { // 寫入內(nèi)存信息
log.Fatal("could not write memory profile: ", err)
}
f.Close()
}
}
運行結(jié)果
| 組件 | 占用內(nèi)存 | 耗時 |
|---|---|---|
| grpool | 2229 Kb | 1679 ms |
| goroutine | 5835 Kb | 1258 ms |
總結(jié)
goframe的grpool節(jié)省內(nèi)存,如果機器的內(nèi)存不高或者業(yè)務(wù)場景對內(nèi)存占用的要求更高,則使用grpool。
如果機器的內(nèi)存足夠,但是對應(yīng)用的執(zhí)行時間有更高的追求,就用原生的goroutine。
更多關(guān)于GoFrame性能測試grpool使用場景的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
go項目實現(xiàn)mysql接入及web?api的操作方法
這篇文章主要介紹了go項目實現(xiàn)mysql接入以及web api,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08
輕松構(gòu)建Go應(yīng)用的Dockerfile
本文介紹了如何制作一個用于構(gòu)建和運行Go應(yīng)用程序的Docker鏡像的Dockerfile的相關(guān)資料,需要的朋友可以參考下2023-10-10
Go語言使用Timeout Context取消任務(wù)的實現(xiàn)
本文主要介紹了Go語言使用Timeout Context取消任務(wù)的實現(xiàn),包括基本的任務(wù)取消和控制HTTP客戶端請求的超時,具有一定的參考價值,感興趣的可以了解一下2024-01-01
使用golang引入外部包的三種方式:go get, go module, ve
這篇文章主要介紹了使用golang引入外部包的三種方式:go get, go module, vendor目錄,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

