aboutsummaryrefslogtreecommitdiff
path: root/stackless/func_timing_test.go
blob: 8335e4d288547f957fd37275cd221a965863386e (plain)
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
package stackless

import (
	"sync/atomic"
	"testing"
)

func BenchmarkFuncOverhead(b *testing.B) {
	var n uint64
	f := NewFunc(func(ctx any) {
		atomic.AddUint64(&n, *(ctx.(*uint64)))
	})
	b.RunParallel(func(pb *testing.PB) {
		x := uint64(1)
		for pb.Next() {
			if !f(&x) {
				b.Fatalf("f mustn't return false")
			}
		}
	})
	if n != uint64(b.N) {
		b.Fatalf("unexpected n: %d. Expecting %d", n, b.N)
	}
}

func BenchmarkFuncPure(b *testing.B) {
	var n uint64
	f := func(x *uint64) {
		atomic.AddUint64(&n, *x)
	}
	b.RunParallel(func(pb *testing.PB) {
		x := uint64(1)
		for pb.Next() {
			f(&x)
		}
	})
	if n != uint64(b.N) {
		b.Fatalf("unexpected n: %d. Expecting %d", n, b.N)
	}
}