GoFrame基于性能測(cè)試得知grpool使用場(chǎng)景
前言摘要
之前寫了一篇 grpool goroutine池詳解 | 協(xié)程管理 收到了大家積極的反饋,今天這篇來做一下grpool的性能測(cè)試分析,讓大家更好的了解什么場(chǎng)景下使用grpool比較好。
先說結(jié)論
grpool相比于goroutine更節(jié)省內(nèi)存,但是耗時(shí)更長;
原因也很簡(jiǎn)單:grpool復(fù)用了協(xié)程,減少了協(xié)程的創(chuàng)建和銷毀,減少了內(nèi)存消耗;也因?yàn)閰f(xié)程的復(fù)用,總的goroutine數(shù)量更少,導(dǎo)致耗時(shí)更多。
測(cè)試性能代碼
開啟for循環(huá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 { // 寫入內(nèi)存信息
log.Fatal("could not write memory profile: ", err)
}
f.Close()
}
}
運(yùn)行結(jié)果
| 組件 | 占用內(nèi)存 | 耗時(shí) |
|---|---|---|
| grpool | 2229 Kb | 1679 ms |
| goroutine | 5835 Kb | 1258 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)文章!
- bug解決Failed_to_execute_goal_org.springframework
- Django Rest Framework框架構(gòu)建復(fù)雜API技能詳解
- Android FrameWork之Zygote啟動(dòng)示例詳解
- GoFrame實(shí)現(xiàn)順序性校驗(yàn)示例詳解
- 優(yōu)雅使用GoFrame共享變量Context示例詳解
- GoFrame錯(cuò)誤處理常用方法及錯(cuò)誤碼使用示例
- GoFrame框架數(shù)據(jù)校驗(yàn)之校驗(yàn)結(jié)果Error接口對(duì)象
- goframe重寫FastAdmin后端實(shí)現(xiàn)實(shí)例詳解
相關(guān)文章
go項(xiàng)目實(shí)現(xiàn)mysql接入及web?api的操作方法
這篇文章主要介紹了go項(xiàng)目實(shí)現(xiàn)mysql接入以及web api,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
輕松構(gòu)建Go應(yīng)用的Dockerfile
本文介紹了如何制作一個(gè)用于構(gòu)建和運(yùn)行Go應(yīng)用程序的Docker鏡像的Dockerfile的相關(guān)資料,需要的朋友可以參考下2023-10-10
詳解Golang中interface{}的注意事項(xiàng)
學(xué)習(xí)?golang?,對(duì)于?interface{}?接口類型,我們一定繞不過,這篇文章咱們就來一起來看看?使用?interface{}?的時(shí)候,都有哪些注意事項(xiàng)吧2023-03-03
Go語言使用Timeout Context取消任務(wù)的實(shí)現(xiàn)
本文主要介紹了Go語言使用Timeout Context取消任務(wù)的實(shí)現(xiàn),包括基本的任務(wù)取消和控制HTTP客戶端請(qǐng)求的超時(shí),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
使用golang引入外部包的三種方式:go get, go module, ve
這篇文章主要介紹了使用golang引入外部包的三種方式:go get, go module, vendor目錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01

