Skip to content

Commit fd67745

Browse files
committed
reference benchmark of goroutine ID parsing
1 parent 6df73f3 commit fd67745

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

assert/goid_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package assert
22

33
import (
4+
"bytes"
5+
"fmt"
46
"testing"
57
)
68

@@ -15,6 +17,17 @@ func TestGoid(t *testing.T) {
1517
}
1618
}
1719

20+
func Test_oldGoid(t *testing.T) {
21+
t.Parallel()
22+
stackBytes := []byte(`goroutine 518 [running]:
23+
`)
24+
25+
id := oldGoid(stackBytes)
26+
if id != 518 {
27+
t.Fail()
28+
}
29+
}
30+
1831
func BenchmarkGoid(b *testing.B) {
1932
_ = []byte(`goroutine 518 [running]:
2033
`)
@@ -32,3 +45,20 @@ func BenchmarkGoid_MyByteToInt(b *testing.B) {
3245
_ = myByteToInt(stackBytes[10:])
3346
}
3447
}
48+
49+
func oldGoid(buf []byte) (id int) {
50+
_, err := fmt.Fscanf(bytes.NewReader(buf), "goroutine %d", &id)
51+
if err != nil {
52+
panic("cannot get goroutine id: " + err.Error())
53+
}
54+
return id
55+
}
56+
57+
func BenchmarkGoid_Old(b *testing.B) {
58+
stackBytes := []byte(`goroutine 518 [running]:
59+
`)
60+
61+
for n := 0; n < b.N; n++ {
62+
_ = oldGoid(stackBytes)
63+
}
64+
}

0 commit comments

Comments
 (0)