Skip to content

Commit

Permalink
add circuit breaker slot benchmark test for circuitbreaker module (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
binbin0325 authored Nov 20, 2020
1 parent c461291 commit 8d33c7c
Showing 1 changed file with 198 additions and 0 deletions.
198 changes: 198 additions & 0 deletions tests/benchmark/circuitbreaker/circuitbreaker_benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package circuitbreaker

import (
"log"
"testing"

"github.com/alibaba/sentinel-golang/api"
"github.com/alibaba/sentinel-golang/core/circuitbreaker"
)

func doCheck() {
if se, err := api.Entry("abc"); err == nil {
se.Exit()
} else {
log.Fatalf("Block err: %s", err.Error())
}
}

func loadSlowRequestRatioRule() {
rule := &circuitbreaker.Rule{
Resource: "abc",
Strategy: circuitbreaker.SlowRequestRatio,
RetryTimeoutMs: 1000,
MinRequestAmount: 5,
StatIntervalMs: 1000,
MaxAllowedRtMs: 20,
Threshold: 0.1,
}
circuitbreaker.LoadRules([]*circuitbreaker.Rule{rule})
}

func loadErrorRatioRule() {
rule := &circuitbreaker.Rule{
Resource: "abc",
Strategy: circuitbreaker.ErrorRatio,
RetryTimeoutMs: 1000,
MinRequestAmount: 5,
StatIntervalMs: 1000,
Threshold: 0.3,
}
circuitbreaker.LoadRules([]*circuitbreaker.Rule{rule})
}

func loadErrorCountRule() {
rule := &circuitbreaker.Rule{
Resource: "abc",
Strategy: circuitbreaker.ErrorCount,
RetryTimeoutMs: 1000,
MinRequestAmount: 5,
StatIntervalMs: 1000,
Threshold: 10.0,
}
circuitbreaker.LoadRules([]*circuitbreaker.Rule{rule})
}

func Benchmark_SlowRequestRatio_SlotCheck_4(b *testing.B) {
loadSlowRequestRatioRule()
b.ReportAllocs()
b.ResetTimer()
b.SetParallelism(4)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
doCheck()
}
})
}

func Benchmark_SlowRequestRatio_SlotCheck_8(b *testing.B) {
loadSlowRequestRatioRule()
b.ReportAllocs()
b.ResetTimer()
b.SetParallelism(8)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
doCheck()
}
})
}

func Benchmark_SlowRequestRatio_SlotCheck_16(b *testing.B) {
loadSlowRequestRatioRule()
b.ReportAllocs()
b.ResetTimer()
b.SetParallelism(16)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
doCheck()
}
})
}

func Benchmark_SlowRequestRatio_SlotCheck_32(b *testing.B) {
loadSlowRequestRatioRule()
b.ReportAllocs()
b.ResetTimer()
b.SetParallelism(32)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
doCheck()
}
})
}

func Benchmark_ErrorRatio_SlotCheck_4(b *testing.B) {
loadErrorRatioRule()
b.ReportAllocs()
b.ResetTimer()
b.SetParallelism(4)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
doCheck()
}
})
}

func Benchmark_ErrorRatio_SlotCheck_8(b *testing.B) {
loadErrorRatioRule()
b.ReportAllocs()
b.ResetTimer()
b.SetParallelism(8)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
doCheck()
}
})
}

func Benchmark_ErrorRatio_SlotCheck_16(b *testing.B) {
loadErrorRatioRule()
b.ReportAllocs()
b.ResetTimer()
b.SetParallelism(16)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
doCheck()
}
})
}

func Benchmark_ErrorRatio_SlotCheck_32(b *testing.B) {
loadErrorRatioRule()
b.ReportAllocs()
b.ResetTimer()
b.SetParallelism(32)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
doCheck()
}
})
}

func Benchmark_ErrorCount_SlotCheck_4(b *testing.B) {
loadErrorCountRule()
b.ReportAllocs()
b.ResetTimer()
b.SetParallelism(4)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
doCheck()
}
})
}

func Benchmark_ErrorCount_SlotCheck_8(b *testing.B) {
loadErrorCountRule()
b.ReportAllocs()
b.ResetTimer()
b.SetParallelism(8)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
doCheck()
}
})
}

func Benchmark_ErrorCount_SlotCheck_16(b *testing.B) {
loadErrorCountRule()
b.ReportAllocs()
b.ResetTimer()
b.SetParallelism(16)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
doCheck()
}
})
}

func Benchmark_ErrorCount_SlotCheck_32(b *testing.B) {
loadErrorCountRule()
b.ReportAllocs()
b.ResetTimer()
b.SetParallelism(32)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
doCheck()
}
})
}

0 comments on commit 8d33c7c

Please sign in to comment.