go-workflows icon indicating copy to clipboard operation
go-workflows copied to clipboard

workflow.Go cubic performance degradation in workflow tester

Open jacobnowakowski1 opened this issue 2 months ago • 4 comments

Hi all, we encountered this issue when trying to spin up 256 "goroutines" in a workflow and saw significant performance degradation in our unit test.

Recreate

Using go-workflows version v0.19.0, create the following test file:

package foo

import (
	"context"
	"fmt"
	"testing"

	"github.com/cschleiden/go-workflows/tester"
	"github.com/cschleiden/go-workflows/workflow"
	"github.com/stretchr/testify/mock"
	"github.com/stretchr/testify/require"
)

func WF(ctx workflow.Context, n int) error {
	wg := workflow.NewWaitGroup()
	for range n {
		wg.Add(1)
		workflow.Go(ctx, func(ctx workflow.Context) {
			defer wg.Done()
			workflow.ExecuteActivity[any](ctx, workflow.DefaultActivityOptions, Act).Get(ctx)
		})
	}
	wg.Wait(ctx)
	return nil
}

func Act(ctx context.Context) error {
	return nil
}

func BenchmarkWorkflowGo(b *testing.B) {
	run := func(b *testing.B, n int) {
		b.Run("n="+fmt.Sprint(n), func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				wft := tester.NewWorkflowTester[any](WF)
				wft.OnActivity(Act, mock.Anything).Return(nil)
				wft.Execute(context.Background(), n)
				require.True(b, wft.WorkflowFinished())
				_, err := wft.WorkflowResult()
				require.NoError(b, err)
			}
		})
	}
	run(b, 1)
	run(b, 31)
	run(b, 51)
	run(b, 71)
	run(b, 91)
	run(b, 111)
	run(b, 131)
	run(b, 151)
	run(b, 171)
	run(b, 191)
	run(b, 211)
}

Issue

I observe that the performance of this simple unit test degrades significantly as the number of workflow.Go calls get made, as the graph below shows:

image

Out of curiosity, I took the log/log of the data and found it formed a linear function of slope close to 3, indicating a runtime performance of O(n^3).

image

Is this expected? With this type of degradation, the workflow.Go functionality has limited use cases for us. Thank you!

jacobnowakowski1 avatar Dec 11 '24 22:12 jacobnowakowski1