-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: add a performance benchmark tests
- Loading branch information
Showing
5 changed files
with
451 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2022-2024 Tochemey | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package bench | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/rand/v2" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/tochemey/goakt/v2/actors" | ||
"github.com/tochemey/goakt/v2/bench/benchmarkpb" | ||
"github.com/tochemey/goakt/v2/log" | ||
) | ||
|
||
const receivingTimeout = 100 * time.Millisecond | ||
|
||
var ( | ||
totalSent *atomic.Int64 | ||
totalRecv *atomic.Int64 | ||
) | ||
|
||
func init() { | ||
totalSent = new(atomic.Int64) | ||
totalRecv = new(atomic.Int64) | ||
} | ||
|
||
// Benchmarker is an actor that helps run benchmark tests | ||
type Benchmarker struct{} | ||
|
||
func (p *Benchmarker) PreStart(context.Context) error { | ||
return nil | ||
} | ||
|
||
func (p *Benchmarker) Receive(ctx actors.ReceiveContext) { | ||
switch ctx.Message().(type) { | ||
case *benchmarkpb.BenchTell: | ||
totalRecv.Add(1) | ||
case *benchmarkpb.BenchRequest: | ||
ctx.Response(&benchmarkpb.BenchResponse{}) | ||
default: | ||
ctx.Unhandled() | ||
} | ||
} | ||
|
||
func (p *Benchmarker) PostStop(context.Context) error { | ||
return nil | ||
} | ||
|
||
// Benchmark defines a load testing engine | ||
type Benchmark struct { | ||
// actorsCount defines the number of actors to create | ||
// on each actor system created by the loader | ||
actorsCount int | ||
// workersCount define the number of message senders | ||
workersCount int | ||
// duration specifies how long the load testing will run | ||
duration time.Duration | ||
pids []actors.PID | ||
system actors.ActorSystem | ||
} | ||
|
||
// NewBenchmark creates an instance of Loader | ||
func NewBenchmark(actorsCount, workersCount int, duration time.Duration) *Benchmark { | ||
return &Benchmark{ | ||
actorsCount: actorsCount, | ||
workersCount: workersCount, | ||
duration: duration, | ||
pids: make([]actors.PID, 0, actorsCount), | ||
} | ||
} | ||
|
||
// Start starts the Benchmark | ||
func (b *Benchmark) Start(ctx context.Context) error { | ||
// create the benchmark actor system | ||
name := "benchmark-system" | ||
b.system, _ = actors.NewActorSystem(name, | ||
actors.WithLogger(log.DiscardLogger), | ||
actors.WithActorInitMaxRetries(1), | ||
actors.WithSupervisorDirective(actors.NewStopDirective()), | ||
actors.WithReplyTimeout(receivingTimeout)) | ||
|
||
if err := b.system.Start(ctx); err != nil { | ||
return err | ||
} | ||
|
||
// wait for the actor system to properly start | ||
time.Sleep(time.Second) | ||
|
||
for i := 0; i < b.actorsCount; i++ { | ||
actorName := fmt.Sprintf("actor-%d", i) | ||
pid, err := b.system.Spawn(ctx, actorName, &Benchmarker{}) | ||
if err != nil { | ||
return err | ||
} | ||
b.pids = append(b.pids, pid) | ||
} | ||
// wait for the actors to properly start | ||
time.Sleep(time.Second) | ||
return nil | ||
} | ||
|
||
// Stop stops the benchmark | ||
func (b *Benchmark) Stop(ctx context.Context) error { | ||
return b.system.Stop(ctx) | ||
} | ||
|
||
// Bench sends messages to a random actor | ||
func (b *Benchmark) Bench(ctx context.Context) error { | ||
wg := sync.WaitGroup{} | ||
wg.Add(b.workersCount) | ||
deadline := time.Now().Add(b.duration) | ||
for i := 0; i < b.workersCount; i++ { | ||
go func() { | ||
defer wg.Done() | ||
for time.Now().Before(deadline) { | ||
// randomly pick and actor | ||
pid := b.pids[rand.IntN(len(b.pids))] //nolint:gosec | ||
// send a message | ||
_ = actors.Tell(ctx, pid, new(benchmarkpb.BenchTell)) | ||
// increase sent counter | ||
totalSent.Add(1) | ||
} | ||
}() | ||
} | ||
|
||
// wait for the messages to be delivered | ||
wg.Wait() | ||
time.Sleep(500 * time.Millisecond) | ||
if totalSent.Load() != totalRecv.Load() { | ||
return fmt.Errorf("send count and receive count does not match: %d != %d", totalSent.Load(), totalRecv.Load()) | ||
} | ||
return nil | ||
} |
This file contains 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
Oops, something went wrong.