Improve syserror_test.

- It's very difficult to prevent returnErrnoAsError and returnError from being
  optimized out. Instead, replace BenchmarkReturn* with BenchmarkAssign*, which
  store to globalError.

- Compare to a non-nil globalError in BenchmarkCompare* and BenchmarkSwitch*.

New results:
BenchmarkAssignErrno
BenchmarkAssignErrno-12     	1000000000	         0.615 ns/op
BenchmarkAssignError
BenchmarkAssignError-12     	1000000000	         0.626 ns/op
BenchmarkCompareErrno
BenchmarkCompareErrno-12    	1000000000	         0.522 ns/op
BenchmarkCompareError
BenchmarkCompareError-12    	1000000000	         3.54 ns/op
BenchmarkSwitchErrno
BenchmarkSwitchErrno-12     	1000000000	         1.45 ns/op
BenchmarkSwitchError
BenchmarkSwitchError-12     	536315757	        10.9 ns/op

PiperOrigin-RevId: 331875387
This commit is contained in:
Jamie Liu 2020-09-15 15:57:38 -07:00 committed by gVisor bot
parent 456c6c33e1
commit 8b15effd9e
1 changed files with 8 additions and 12 deletions

View File

@ -24,27 +24,20 @@ import (
var globalError error
func returnErrnoAsError() error {
return syscall.EINVAL
}
func returnError() error {
return syserror.EINVAL
}
func BenchmarkReturnErrnoAsError(b *testing.B) {
func BenchmarkAssignErrno(b *testing.B) {
for i := b.N; i > 0; i-- {
returnErrnoAsError()
globalError = syscall.EINVAL
}
}
func BenchmarkReturnError(b *testing.B) {
func BenchmarkAssignError(b *testing.B) {
for i := b.N; i > 0; i-- {
returnError()
globalError = syserror.EINVAL
}
}
func BenchmarkCompareErrno(b *testing.B) {
globalError = syscall.EAGAIN
j := 0
for i := b.N; i > 0; i-- {
if globalError == syscall.EINVAL {
@ -54,6 +47,7 @@ func BenchmarkCompareErrno(b *testing.B) {
}
func BenchmarkCompareError(b *testing.B) {
globalError = syserror.EAGAIN
j := 0
for i := b.N; i > 0; i-- {
if globalError == syserror.EINVAL {
@ -63,6 +57,7 @@ func BenchmarkCompareError(b *testing.B) {
}
func BenchmarkSwitchErrno(b *testing.B) {
globalError = syscall.EPERM
j := 0
for i := b.N; i > 0; i-- {
switch globalError {
@ -77,6 +72,7 @@ func BenchmarkSwitchErrno(b *testing.B) {
}
func BenchmarkSwitchError(b *testing.B) {
globalError = syserror.EPERM
j := 0
for i := b.N; i > 0; i-- {
switch globalError {