網站首頁 編程語言 正文
前言摘要
之前寫了一篇 grpool goroutine池詳解 | 協程管理 收到了大家積極的反饋,今天這篇來做一下grpool的性能測試分析,讓大家更好的了解什么場景下使用grpool比較好。
先說結論
grpool相比于goroutine更節省內存,但是耗時更長;
原因也很簡單:grpool復用了協程,減少了協程的創建和銷毀,減少了內存消耗;也因為協程的復用,總的goroutine數量更少,導致耗時更多。
測試性能代碼
開啟for循環,開啟一萬個協程,分別使用原生goroutine和grpool執行。
看兩者在內存占用和耗時方面的差別。
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() { //接收命令行參數 flag.Parse() //cpu分析 cpuProfile() //主邏輯 //demoGrpool() demoGoroutine() //內存分析 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("運行中占用內存:%d Kb\n", m.Alloc/1024) time.Sleep(time.Millisecond) wg.Done() }) fmt.Printf("運行的協程:", 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("運行中占用內存:%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 { //監控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,獲取最新的數據信息 if err := pprof.WriteHeapProfile(f); err != nil { // 寫入內存信息 log.Fatal("could not write memory profile: ", err) } f.Close() } }
運行結果
組件 | 占用內存 | 耗時 |
---|---|---|
grpool | 2229 Kb | 1679 ms |
goroutine | 5835 Kb | 1258 ms |
總結
goframe的grpool節省內存,如果機器的內存不高或者業務場景對內存占用的要求更高,則使用grpool。
如果機器的內存足夠,但是對應用的執行時間有更高的追求,就用原生的goroutine。
原文鏈接:https://juejin.cn/post/7110510747498594341
相關推薦
- 2022-09-14 jQuery實現簡易計算器功能_jquery
- 2022-05-19 分享Python獲取本機IP地址的幾種方法_python
- 2022-12-11 c++將vector迭代器轉換為指針的實現方式_C 語言
- 2022-07-04 Python基礎之矩陣輸入的實例_python
- 2022-06-04 C#處理類型和二進制數據轉換并提高程序性能_C#教程
- 2022-11-23 linux下解決?git?clone每次都要輸入用戶名密碼問題(推薦)_linux shell
- 2022-04-25 mongodb?linux下集群搭建過程_MongoDB
- 2022-04-07 淺談C++11中=delete的巧妙用法_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支