脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Golang - GoFrame基于性能测试得知grpool使用场景

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

2022-10-24 11:28王中阳Go Golang

这篇文章主要为大家介绍了GoFrame基于性能测试得知grpool使用场景示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

前言摘要

之前写了一篇 grpool goroutine池详解 | 协程管理 收到了大家积极的反馈,今天这篇来做一下grpool的性能测试分析,让大家更好的了解什么场景下使用grpool比较好。

先说结论

grpool相比于goroutine更节省内存,但是耗时更长;

原因也很简单:grpool复用了协程,减少了协程的创建和销毁,减少了内存消耗;也因为协程的复用,总的goroutine数量更少,导致耗时更多。

测试性能代码

开启for循环,开启一万个协程,分别使用原生goroutine和grpool执行。

看两者在内存占用和耗时方面的差别。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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使用场景的资料请关注服务器之家其它相关文章!

原文链接:https://juejin.cn/post/7110510747498594341

延伸 · 阅读

精彩推荐
  • GolangGo语言开发必知的一个内存模型细节

    Go语言开发必知的一个内存模型细节

    这篇文章主要为大家介绍了Go语言开发必知的一个内存模型细节详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...

    煎鱼3592022-07-14
  • GolangGolang获取当前时间代码

    Golang获取当前时间代码

    本文给大家汇总介绍了golang中的相关的时间的操作,有需要的小伙伴可以拿走参考下 ...

    ck_god2872020-05-20
  • GolangGo语言空结构体详解

    Go语言空结构体详解

    本文主要介绍了Go语言空结构体详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    漫漫Coding路9902022-08-31
  • Golanggolang 中的 nil的场景分析

    golang 中的 nil的场景分析

    这篇文章主要介绍了golang 中的 nil,本文通过多种场景分析给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...

    追风骚年8392021-04-19
  • GolangGo1.18新特性对泛型支持详解

    Go1.18新特性对泛型支持详解

    这篇文章主要为大家介绍了Go1.18新特性对泛型支持详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...

    不习惯的习惯变成习惯4632022-10-20
  • Golang1行Go代码实现反向代理的示例

    1行Go代码实现反向代理的示例

    这篇文章主要介绍了1行Go代码实现反向代理的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...

    alfred-zhong2192020-05-18
  • GolangGo语言声明一个多行字符串的变量

    Go语言声明一个多行字符串的变量

    这篇文章主要介绍了Go语言声明一个多行字符串的变量的方法和示例,非常简单实用,有需要的小伙伴可以参考下。 ...

    脚本之家9082020-04-25
  • GolangGo中time.RFC3339 时间格式化的实现

    Go中time.RFC3339 时间格式化的实现

    这篇文章主要介绍了Go中time.RFC3339 时间格式化的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们...

    新亮笔记6172021-03-27