目录
  • 前言摘要
  • 先说结论
  • 测试性能代码
    • 运行结果
  • 总结

    前言摘要

    之前写了一篇 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使用场景的资料请关注其它相关文章!

    声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。