猿问

如何对通道/互斥内存消耗/分配进行基准测试?

我尝试比较使用通道获取一个值与使用互斥锁。频道示例:


func BenchmarkNewDummy(b *testing.B) {

    one := make(chan string, 1)

    two := make(chan string, 1)

    var wg sync.WaitGroup

    wg.Add(2)

    go doWork(&wg, one)

    go doWork(&wg, two)

    wg.Wait()

    fmt.Println(<-one)

    fmt.Println(<-two)

}


func doWork(wg *sync.WaitGroup, one chan string) {

    defer wg.Done()

    one <- "hell0"

}

命令:


go test -bench=. -benchmem -run BenchmarkNewDummy -cpuprofile cpuCh.out -memprofile memCh.prof 

输出没有提供任何有用的信息


goos: darwin

goarch: amd64

BenchmarkNewDummy-8     hell0

hell0

hell0

hell0

hell0

hell0

hell0

hell0

hell0

hell0

2000000000               0.00 ns/op            0 B/op          0 allocs/op

PASS

ok        0.508s

与互斥量情况几乎相同:


func BenchmarkNewDummy(b *testing.B) {

    one := ""

    two := ""

    var wg sync.WaitGroup

    wg.Add(2)

    var mu sync.Mutex

    go func() {

        mu.Lock()

        defer mu.Unlock()

        defer wg.Done()

        one = "hello"

    }()

    go func() {

        mu.Lock()

        defer mu.Unlock()

        defer wg.Done()

        two = "hello"

    }()

    wg.Wait()

    fmt.Println(one)

    fmt.Println(two)

}

输出:


goos: darwin

goarch: 

BenchmarkNewDummy-8     hello

hello

hello

hello

hello

hello

hello

hello

hello

hello

2000000000               0.00 ns/op            0 B/op          0 allocs/op

PASS

ok        0.521s

内存图看起来几乎相同,但使用 mutext 更大的内存分配,但也没有提供信息:

http://img.mukewang.com/627a1a5a0001de7607420699.jpg

是否可以比较通道和互斥体内存消耗?



猛跑小猪
浏览 131回答 1
1回答

红糖糍粑

你做的基准测试是错误的。引用包文档testing:示例基准函数如下所示:func BenchmarkHello(b *testing.B) {&nbsp; &nbsp; for i := 0; i < b.N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Sprintf("hello")&nbsp; &nbsp; }}基准函数必须运行目标代码 bN 次。 在基准执行期间,调整 bN 直到基准函数持续足够长的时间以可靠地计时。也不要fmt.PrintXX()在基准代码中包含调用,您会扭曲结果。而是对这些函数进行基准测试:func newDummy() {&nbsp; &nbsp; one := make(chan string, 1)&nbsp; &nbsp; two := make(chan string, 1)&nbsp; &nbsp; var wg sync.WaitGroup&nbsp; &nbsp; wg.Add(2)&nbsp; &nbsp; go doWork(&wg, one)&nbsp; &nbsp; go doWork(&wg, two)&nbsp; &nbsp; wg.Wait()&nbsp; &nbsp; <-one&nbsp; &nbsp; <-two}func doWork(wg *sync.WaitGroup, one chan string) {&nbsp; &nbsp; defer wg.Done()&nbsp; &nbsp; one <- "hell0"}func newDummy2() {&nbsp; &nbsp; one, two := "", ""&nbsp; &nbsp; var wg sync.WaitGroup&nbsp; &nbsp; wg.Add(2)&nbsp; &nbsp; var mu sync.Mutex&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; mu.Lock()&nbsp; &nbsp; &nbsp; &nbsp; defer mu.Unlock()&nbsp; &nbsp; &nbsp; &nbsp; defer wg.Done()&nbsp; &nbsp; &nbsp; &nbsp; one = "hello"&nbsp; &nbsp; }()&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; mu.Lock()&nbsp; &nbsp; &nbsp; &nbsp; defer mu.Unlock()&nbsp; &nbsp; &nbsp; &nbsp; defer wg.Done()&nbsp; &nbsp; &nbsp; &nbsp; two = "hello"&nbsp; &nbsp; }()&nbsp; &nbsp; wg.Wait()&nbsp; &nbsp; _, _ = one, two}像这样:func BenchmarkNewDummy(b *testing.B) {&nbsp; &nbsp; for i := 0; i < b.N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; newDummy()&nbsp; &nbsp; }}func BenchmarkNewDummy2(b *testing.B) {&nbsp; &nbsp; for i := 0; i < b.N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; newDummy2()&nbsp; &nbsp; }}对其进行基准测试:go test -bench . -benchmem我得到这样的输出:BenchmarkNewDummy-4&nbsp; &nbsp; 605662&nbsp; &nbsp; &nbsp; 1976 ns/op&nbsp; &nbsp; &nbsp;240 B/op&nbsp; &nbsp; &nbsp; 5 allocs/opBenchmarkNewDummy2-4&nbsp; &nbsp;927031&nbsp; &nbsp; &nbsp; 1627 ns/op&nbsp; &nbsp; &nbsp; 56 B/op&nbsp; &nbsp; &nbsp; 4 allocs/op从结果来看,newDummy()平均执行 5 次分配,总计 250 字节。newDummy2()执行 4 次分配,总共 56 个字节。
随时随地看视频慕课网APP

相关分类

Go
我要回答