Go语言如何实现Benchmark函数

 更新时间:2024年12月25日 11:09:23   作者:AnthonyDong  
go想要在main函数中测试benchmark会麻烦一些,所以这篇文章主要为大家介绍了如何实现了一个简单的且没有开销的benchmark函数,希望对大家有所帮助

背景

go 必须要 test 才能跑benchmark,导致一些情况下想要在main函数中测试benchmark会麻烦一些,因此我实现了一个简单的且没有开销的benchmark函数,方便使用!其次也方便大家学习下如何实现一个零开销的benchmark框架!

benchamrk 实现

对于有timeout的benchamrk,每次都去比较 time.Before(timeout) 开销非常的大,而且 benchmark 的对外函数也不能是一个空函数一定要带 count ,因为函数调用大概会劣化 ns 级别!

所以一般的benchmark算法都是梯度benchmark,即 1,10,100,1000,10000,100000,1000000 ... 的数量级去benchmark,好处就是避免了大量的time.Since 计算开销,因为time.Since单次在 30ns 左右(Linux环境下),开销非常大的!

package pprof

import (
    "fmt"
    "sync"
    "sync/atomic"
    "time"
)

func ParallelBenchmark(name string, thread int, duration time.Duration, execute func(count int)) {
    wg := sync.WaitGroup{}
    wg.Add(thread)
    totalCount := uint64(0)
    totalSpend := uint64(0)
    for i := 0; i < thread; i++ {
       go func() {
          defer wg.Done()
          spend, count := Benchmark(duration, execute)
          atomic.AddUint64(&totalSpend, uint64(spend))
          atomic.AddUint64(&totalCount, uint64(count))
       }()
    }
    wg.Wait()
    fmt.Printf("name=%s thread=%d duration=%s total=%d avg=%s\n", name, thread, duration, totalCount, Avg(time.Duration(totalSpend), int(totalCount)))
}

func Avg(spend time.Duration, count int) string {
    avg := float64(spend) / float64(count)
    if avg > 100 {
       return time.Duration(avg).String()
    }
    return fmt.Sprintf("%.4fns", avg)
}

func Benchmark(duration time.Duration, bench func(count int)) (time.Duration, int) {
    const maxTotalCount = 1000000000 // 10E
    count := 1
    totalSpend := time.Duration(0)
    totalCount := 0
    for {
       start := time.Now()
       bench(count)
       spend := time.Since(start)

       totalSpend = totalSpend + spend
       totalCount = totalCount + count

       if totalCount >= maxTotalCount {
          break
       }
       subSpend := duration - totalSpend
       if subSpend <= 0 {
          break
       }
       count = totalCount*10 - totalCount
       if subCount := int(float64(subSpend) / (float64(totalSpend) / float64(totalCount))); count > subCount {
          count = subCount
       }
    }
    return totalSpend, totalCount
}

profile 实现

package pprof

import (
    "net/http"
    _ "net/http/pprof"
    "os"
    "runtime"
    "runtime/pprof"
)

// InitPProf
// go InitPProf()
func InitPProf() {
    err := http.ListenAndServe(":12345", http.DefaultServeMux)
    if err != nil {
       panic(err)
    }
}

func StartCPUProfile(fileName string) (stop func()) {
    f, err := os.Create(fileName)
    if err != nil {
       panic(err)
    }
    if err := pprof.StartCPUProfile(f); err != nil {
       if err := f.Close(); err != nil {
          panic(err)
       }
       panic(err)
    }
    return func() {
       pprof.StopCPUProfile()
       if err := f.Close(); err != nil {
          panic(err)
       }
    }
}

func StartMemProfile(fileName string) (stop func()) {
    f, err := os.Create(fileName)
    if err != nil {
       panic(err)
    }
    return func() {
       defer func() {
          if err := f.Close(); err != nil {
             panic(err)
          }
       }()
       runtime.GC() // get up-to-date statistics
       if err := pprof.WriteHeapProfile(f); err != nil {
          panic(err)
       }
    }
}

例子

package main

import (
	"github.com/anthony-dong/golang/pkg/pprof"
	"sync"
	"time"
)

func main() {
	// 记录 cup pprof
	//stop := pprof.StartCPUProfile("cpu.out")
	//defer stop()

	// 并发测试 sync map的性能
	mm := sync.Map{}
	pprof.ParallelBenchmark("test1", 64, time.Second, func(count int) {
		for i := 0; i < count; i++ {
			mm.Store(i%10000, 1)
		}
	})
	// name=test1 thread=32 duration=1s total=6708009 avg=4.772µs
	// name=test1 thread=64 duration=1s total=6883456 avg=9.3µs
}

到此这篇关于Go语言如何实现Benchmark函数的文章就介绍到这了,更多相关Go Benchmark内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Golang常见错误之值拷贝和for循环中的单一变量详解

    Golang常见错误之值拷贝和for循环中的单一变量详解

    这篇文章主要给大家介绍了关于Golang常见错误之值拷贝和for循环中单一变量的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-11-11
  • docker中部署golang项目的步骤详解

    docker中部署golang项目的步骤详解

    这篇文章主要给大家介绍了关于在docker中部署golang项目的步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-11-11
  • 获取Golang环境变量的三种方式小结

    获取Golang环境变量的三种方式小结

    本文介绍了Golang中获取环境变量的三种方式,包含使用Viper包、GoDotEnv包和os包,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-11-11
  • 在goland中读取tpl文件的图文操作

    在goland中读取tpl文件的图文操作

    这篇文章主要介绍了在goland中读取tpl文件的图文操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • Golang使用WebSocket通信的实现

    Golang使用WebSocket通信的实现

    这篇文章主要介绍了Golang使用WebSocket通信的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • Golang中的Unicode与字符串示例详解

    Golang中的Unicode与字符串示例详解

    这篇文章主要给大家介绍了关于Golang中Unicode与字符串的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Golang具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-05-05
  • go语言的sql包原理与用法分析

    go语言的sql包原理与用法分析

    这篇文章主要介绍了go语言的sql包原理与用法,较为详细的分析了Go语言里sql包的结构、相关函数与使用方法,需要的朋友可以参考下
    2016-07-07
  • Go语言设计模式之实现观察者模式解决代码臃肿

    Go语言设计模式之实现观察者模式解决代码臃肿

    今天学习一下用 Go 实现观察者模式,观察者模式主要是用来实现事件驱动编程。事件驱动编程的应用还是挺广的,除了我们都知道的能够用来解耦:用户修改密码后,给用户发短信进行风险提示之类的典型场景,在微服务架构实现最终一致性、实现事件源A + ES
    2022-08-08
  • Golang+Vue轻松构建Web应用的方法步骤

    Golang+Vue轻松构建Web应用的方法步骤

    本文主要介绍了Golang+Vue轻松构建Web应用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • Golang分布式应用之Redis示例详解

    Golang分布式应用之Redis示例详解

    这篇文章主要为大家介绍了Golang分布式应用之Redis示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07

最新评论