GoFrame基于性能测试得知grpool使用场景


Posted in Golang onJune 21, 2022

前言摘要

之前写了一篇 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。

更多关于GoFrame性能测试grpool使用场景的资料请关注三水点靠木其它相关文章!


Tags in this post...

Golang 相关文章推荐
golang 生成对应的数据表struct定义操作
Apr 28 Golang
Golang 空map和未初始化map的注意事项说明
Apr 29 Golang
goland 设置project gopath的操作
May 06 Golang
Go 在 MongoDB 中常用查询与修改的操作
May 07 Golang
Go语言设计模式之结构型模式
Jun 22 Golang
Golang的继承模拟实例
Jun 30 Golang
go goroutine 怎样进行错误处理
Jul 16 Golang
golang中的struct操作
Nov 11 Golang
Golang 并发下的问题定位及解决方案
Mar 16 Golang
Go归并排序算法的实现方法
Apr 06 Golang
golang语言指针操作
Apr 14 Golang
Golang gRPC HTTP协议转换示例
Go Grpc Gateway兼容HTTP协议文档自动生成网关
Jun 16 #Golang
Go gRPC进阶教程gRPC转换HTTP
Jun 16 #Golang
GoFrame gredis缓存DoVar Conn连接对象 自动序列化GoFrame gredisDo/DoVar方法Conn连接对象自动序列化/反序列化总结
Jun 14 #Golang
Go调用Rust方法及外部函数接口前置
详解Go语言中配置文件使用与日志配置
Jun 01 #Golang
详解Go语言中Get/Post请求测试
You might like
一些花式咖啡的配方
2021/03/03 冲泡冲煮
使用php+Ajax实现唯一校验实现代码[简单应用]
2011/11/29 PHP
PHP获取数组最后一个值的2种方法
2015/01/21 PHP
PHP简单实现图片格式转换(jpg转png,gif转png等)
2019/10/30 PHP
漂亮的widgets,支持换肤和后期开发新皮肤(2007-4-27已更新1.7alpha)
2007/04/27 Javascript
NodeJS与Mysql的交互示例代码
2013/08/18 NodeJs
jquery中对于批量deferred的处理方法
2014/01/22 Javascript
JS实现消息来时让网页标题闪动效果的方法
2016/04/20 Javascript
js控制div层的叠加简单方法
2016/10/15 Javascript
通过扫描二维码打开app的实现代码
2016/11/10 Javascript
详解Javascript中DOM的范围
2017/02/13 Javascript
jQuery+Ajax实现用户名重名实时检测
2017/06/01 jQuery
Vue 滚动行为的具体使用方法
2017/09/13 Javascript
微信小程序MUI侧滑导航菜单示例(Popup弹出式,左侧滑动,右侧不动)
2019/01/23 Javascript
JS实现处理时间,年月日,星期的公共方法示例
2019/05/31 Javascript
javascript中的数据类型检测方法详解
2019/08/07 Javascript
vue实现禁止浏览器记住密码功能的示例代码
2021/02/03 Vue.js
Python实现识别手写数字 Python图片读入与处理
2020/03/23 Python
Ubuntu下使用python读取doc和docx文档的内容方法
2018/05/08 Python
python中验证码连通域分割的方法详解
2018/06/04 Python
Python subprocess模块常见用法分析
2018/06/12 Python
TensorFlow卷积神经网络之使用训练好的模型识别猫狗图片
2019/03/14 Python
python opencv根据颜色进行目标检测的方法示例
2020/01/15 Python
Python pip配置国内源的方法
2020/02/14 Python
python中加背景音乐如何操作
2020/07/19 Python
django使用graphql的实例
2020/09/02 Python
matplotlib阶梯图的实现(step())
2021/03/02 Python
编程用JAVA解析XML的方式
2013/07/07 面试题
检察官就职演讲稿
2014/01/13 职场文书
称象教学反思
2014/02/03 职场文书
创建文明学校实施方案
2014/03/11 职场文书
工作说明书范文
2014/05/07 职场文书
企业理念标语
2014/06/09 职场文书
地下停车场租赁协议范本
2014/10/07 职场文书
入党申请书怎么写?
2019/06/21 职场文书
Nginx四层负载均衡的配置指南
2021/06/11 Servers