Created
August 21, 2024 23:21
-
-
Save zachmu/7cc06bd25114d7b67b5b1d8b06e2311b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package scratch3 | |
import ( | |
"errors" | |
"testing" | |
) | |
//go:noinline | |
func doWorkWithPanic(b *testing.B, n int) { | |
defer func() { | |
if r := recover(); r != nil { | |
b.ReportMetric(1.0, "panics") | |
} | |
}() | |
for range n { | |
doWorkPanic(n) | |
} | |
} | |
//go:noinline | |
func doWorkPanic(n int) { | |
if n < 0 { | |
panic("panic") | |
} | |
} | |
func BenchmarkPanic1(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithPanic(b, 1) | |
} | |
} | |
func BenchmarkPanic10(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithPanic(b, 10) | |
} | |
} | |
func BenchmarkPanic100(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithPanic(b, 100) | |
} | |
} | |
func BenchmarkPanic1_000(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithPanic(b, 1_000) | |
} | |
} | |
func BenchmarkPanic10_000(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithPanic(b, 10_000) | |
} | |
} | |
func BenchmarkPanic100_000(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithPanic(b, 100_000) | |
} | |
} | |
func BenchmarkPanic1_000_000(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithPanic(b, 1_000_000) | |
} | |
} | |
func BenchmarkError1(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithError(b, 1) | |
} | |
} | |
func BenchmarkError10(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithError(b, 10) | |
} | |
} | |
func BenchmarkError100(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithError(b, 100) | |
} | |
} | |
func BenchmarkError1_000(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithError(b, 1_000) | |
} | |
} | |
func BenchmarkError10_000(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithError(b, 10_000) | |
} | |
} | |
func BenchmarkError100_000(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithError(b, 100_000) | |
} | |
} | |
func BenchmarkError1_000_000(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithError(b, 1_000_000) | |
} | |
} | |
//go:noinline | |
func doWorkWithError(b *testing.B, n int) { | |
for range n { | |
err := doWorkError(n) | |
if err != nil { | |
b.ReportMetric(1, "errors") | |
return | |
} | |
} | |
} | |
var workError = errors.New("error") | |
//go:noinline | |
func doWorkError(n int) error { | |
if n < 0 { | |
return workError | |
} | |
return nil | |
} | |
func BenchmarkPanicNested1(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkPanicDeepNested(b, 1_000_000, 1) | |
} | |
} | |
func BenchmarkPanicNested2(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkPanicDeepNested(b, 1_000_000, 2) | |
} | |
} | |
func BenchmarkPanicNested3(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkPanicDeepNested(b, 1_000_000, 3) | |
} | |
} | |
func BenchmarkPanicNested4(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkPanicDeepNested(b, 1_000_000, 4) | |
} | |
} | |
func BenchmarkPanicNested5(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkPanicDeepNested(b, 1_000_000, 5) | |
} | |
} | |
func BenchmarkPanicNested6(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkPanicDeepNested(b, 1_000_000, 6) | |
} | |
} | |
func BenchmarkPanicNested7(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkPanicDeepNested(b, 1_000_000, 7) | |
} | |
} | |
func BenchmarkPanicNested8(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkPanicDeepNested(b, 1_000_000, 8) | |
} | |
} | |
func BenchmarkPanicNested9(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkPanicDeepNested(b, 1_000_000, 9) | |
} | |
} | |
func BenchmarkPanicNested10(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkPanicDeepNested(b, 1_000_000, 10) | |
} | |
} | |
func BenchmarkErrorNested1(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithErrorDeepNested(b, 1_000_000, 1) | |
} | |
} | |
func BenchmarkErrorNested2(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithErrorDeepNested(b, 1_000_000, 2) | |
} | |
} | |
func BenchmarkErrorNested3(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithErrorDeepNested(b, 1_000_000, 3) | |
} | |
} | |
func BenchmarkErrorNested4(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithErrorDeepNested(b, 1_000_000, 4) | |
} | |
} | |
func BenchmarkErrorNested5(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithErrorDeepNested(b, 1_000_000, 5) | |
} | |
} | |
func BenchmarkErrorNested6(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithErrorDeepNested(b, 1_000_000, 6) | |
} | |
} | |
func BenchmarkErrorNested7(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithErrorDeepNested(b, 1_000_000, 7) | |
} | |
} | |
func BenchmarkErrorNested8(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithErrorDeepNested(b, 1_000_000, 8) | |
} | |
} | |
func BenchmarkErrorNested9(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithErrorDeepNested(b, 1_000_000, 9) | |
} | |
} | |
func BenchmarkErrorNested10(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
doWorkWithErrorDeepNested(b, 1_000_000, 10) | |
} | |
} | |
//go:noinline | |
func doWorkWithErrorDeepNested(b *testing.B, n int, nesting int) { | |
var sum uint64 | |
var entryPoint func(int) (int, error) | |
switch nesting { | |
case 10: | |
entryPoint = doWorkErrorDeepNested10 | |
case 9: | |
entryPoint = doWorkErrorDeepNested9 | |
case 8: | |
entryPoint = doWorkErrorDeepNested8 | |
case 7: | |
entryPoint = doWorkErrorDeepNested7 | |
case 6: | |
entryPoint = doWorkErrorDeepNested6 | |
case 5: | |
entryPoint = doWorkErrorDeepNested5 | |
case 4: | |
entryPoint = doWorkErrorDeepNested4 | |
case 3: | |
entryPoint = doWorkErrorDeepNested3 | |
case 2: | |
entryPoint = doWorkErrorDeepNested2 | |
case 1: | |
entryPoint = doWorkErrorDeepNested1 | |
default: | |
panic("invalid nesting") | |
} | |
for range n { | |
res, err := entryPoint(n) | |
if err != nil { | |
b.ReportMetric(float64(n), "errors") | |
return | |
} | |
sum += uint64(res) | |
} | |
b.ReportMetric(float64(sum), "sum") | |
} | |
//go:noinline | |
func doWorkErrorDeepNested10(n int) (int, error) { | |
n, err := doWorkErrorDeepNested9(n) | |
if err != nil { | |
return 0, err | |
} | |
return n, nil | |
} | |
//go:noinline | |
func doWorkErrorDeepNested9(n int) (int, error) { | |
n, err := doWorkErrorDeepNested8(n) | |
if err != nil { | |
return 0, err | |
} | |
return n, nil | |
} | |
//go:noinline | |
func doWorkErrorDeepNested8(n int) (int, error) { | |
n, err := doWorkErrorDeepNested7(n) | |
if err != nil { | |
return 0, err | |
} | |
return n, nil | |
} | |
//go:noinline | |
func doWorkErrorDeepNested7(n int) (int, error) { | |
n, err := doWorkErrorDeepNested6(n) | |
if err != nil { | |
return 0, err | |
} | |
return n, nil | |
} | |
//go:noinline | |
func doWorkErrorDeepNested6(n int) (int, error) { | |
n, err := doWorkErrorDeepNested5(n) | |
if err != nil { | |
return 0, err | |
} | |
return n, nil | |
} | |
//go:noinline | |
func doWorkErrorDeepNested5(n int) (int, error) { | |
n, err := doWorkErrorDeepNested4(n) | |
if err != nil { | |
return 0, err | |
} | |
return n, nil | |
} | |
//go:noinline | |
func doWorkErrorDeepNested4(n int) (int, error) { | |
n, err := doWorkErrorDeepNested3(n) | |
if err != nil { | |
return 0, err | |
} | |
return n, nil | |
} | |
//go:noinline | |
func doWorkErrorDeepNested3(n int) (int, error) { | |
n, err := doWorkErrorDeepNested2(n) | |
if err != nil { | |
return 0, err | |
} | |
return n, nil | |
} | |
//go:noinline | |
func doWorkErrorDeepNested2(n int) (int, error) { | |
n, err := doWorkErrorDeepNested1(n) | |
if err != nil { | |
return 0, err | |
} | |
return n, nil | |
} | |
//go:noinline | |
func doWorkErrorDeepNested1(n int) (int, error) { | |
err := doWorkError(n) | |
if err != nil { | |
return 0, err | |
} | |
return n, nil | |
} | |
//go:noinline | |
func doWorkPanicDeepNested(b *testing.B, n int, nesting int) { | |
defer func() { | |
if r := recover(); r != nil { | |
b.ReportMetric(1, "panics") | |
} | |
}() | |
var entryPoint func(int) int | |
switch nesting { | |
case 10: | |
entryPoint = doWorkPanicDeepNested10 | |
case 9: | |
entryPoint = doWorkPanicDeepNested9 | |
case 8: | |
entryPoint = doWorkPanicDeepNested8 | |
case 7: | |
entryPoint = doWorkPanicDeepNested7 | |
case 6: | |
entryPoint = doWorkPanicDeepNested6 | |
case 5: | |
entryPoint = doWorkPanicDeepNested5 | |
case 4: | |
entryPoint = doWorkPanicDeepNested4 | |
case 3: | |
entryPoint = doWorkPanicDeepNested3 | |
case 2: | |
entryPoint = doWorkPanicDeepNested2 | |
case 1: | |
entryPoint = doWorkPanicDeepNested1 | |
default: | |
panic("invalid nesting") | |
} | |
var sum uint64 | |
for range n { | |
res := entryPoint(n) | |
sum += uint64(res) | |
} | |
b.ReportMetric(float64(sum), "sum") | |
} | |
//go:noinline | |
func doWorkPanicDeepNested10(n int) int { | |
return doWorkPanicDeepNested9(n) | |
} | |
//go:noinline | |
func doWorkPanicDeepNested9(n int) int { | |
return doWorkPanicDeepNested8(n) | |
} | |
//go:noinline | |
func doWorkPanicDeepNested8(n int) int { | |
return doWorkPanicDeepNested7(n) | |
} | |
//go:noinline | |
func doWorkPanicDeepNested7(n int) int { | |
return doWorkPanicDeepNested6(n) | |
} | |
//go:noinline | |
func doWorkPanicDeepNested6(n int) int { | |
return doWorkPanicDeepNested5(n) | |
} | |
//go:noinline | |
func doWorkPanicDeepNested5(n int) int { | |
return doWorkPanicDeepNested4(n) | |
} | |
//go:noinline | |
func doWorkPanicDeepNested4(n int) int { | |
return doWorkPanicDeepNested3(n) | |
} | |
//go:noinline | |
func doWorkPanicDeepNested3(n int) int { | |
return doWorkPanicDeepNested2(n) | |
} | |
//go:noinline | |
func doWorkPanicDeepNested2(n int) int { | |
return doWorkPanicDeepNested1(n) | |
} | |
//go:noinline | |
func doWorkPanicDeepNested1(n int) int { | |
doWorkPanic(n) | |
return n | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment