Files
commander/examples/basic/service_test.go
2024-08-01 23:42:30 +03:00

135 lines
2.7 KiB
Go

package main
import (
"context"
"testing"
"time"
"github.com/google/uuid"
"git.derfenix.pro/fenix/commander"
"git.derfenix.pro/fenix/commander/inmemorycache"
)
func BenchmarkServiceNoop(b *testing.B) {
ctx := context.Background()
cmd := commander.New(10)
c1 := GetName{UID: uuid.NewString()}
c2 := GetAddress{Input: &c1.Result}
commands := []commander.Command{&c1, &c2}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := cmd.Execute(ctx, "", commands...); err != nil {
_ = err
}
}
}
func BenchmarkServiceWithDelay(b *testing.B) {
ctx := context.Background()
cmd := commander.New(10)
c1 := GetName{UID: uuid.NewString(), Sleep: time.Microsecond * 100}
c2 := GetAddress{Input: &c1.Result}
commands := []commander.Command{&c1, &c2}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := cmd.Execute(ctx, "", commands...); err != nil {
_ = err
}
}
}
func BenchmarkServiceWithRollbackNoop(b *testing.B) {
ctx := context.Background()
cmd := commander.New(10)
c1 := GetName{UID: uuid.NewString()}
c2 := GetAddress{Input: &c1.Result}
commands := []commander.Command{&c1, &c2}
commands = append(commands, &GetAddress{Input: &c2.Result})
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := cmd.Execute(ctx, "", commands...); err != nil {
_ = err
}
}
}
func BenchmarkServiceWithRollbackWithDelay(b *testing.B) {
ctx := context.Background()
cmd := commander.New(10)
c1 := GetName{UID: uuid.NewString(), Sleep: time.Microsecond * 100}
c2 := GetAddress{Input: &c1.Result}
commands := []commander.Command{&c1, &c2}
commands = append(commands, &GetAddress{Input: &c2.Result})
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := cmd.Execute(ctx, "", commands...); err != nil {
_ = err
}
}
}
func BenchmarkServiceWithCacheNoop(b *testing.B) {
ctx := context.Background()
cmd := commander.New(10)
cmd = cmd.WithCache(inmemorycache.NewInMemoryCache())
c1 := GetName{UID: uuid.NewString()}
c2 := GetAddress{Input: &c1.Result}
commands := []commander.Command{&c1, &c2}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := cmd.Execute(ctx, "", commands...); err != nil {
_ = err
}
}
}
func BenchmarkServiceWithCacheWithDelay(b *testing.B) {
ctx := context.Background()
cmd := commander.New(10)
cmd = cmd.WithCache(inmemorycache.NewInMemoryCache())
c1 := GetName{UID: uuid.NewString(), Sleep: time.Microsecond * 100}
c2 := GetAddress{Input: &c1.Result}
commands := []commander.Command{&c1, &c2}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := cmd.Execute(ctx, "", commands...); err != nil {
_ = err
}
}
}