(ctx context.Context, b *testing.B, driverWithPool func(ctx context.Context, b *testing.B) (riverdriver.Driver[TTx], string), executorWithTx func(ctx context.Context, b *testing.B) riverdriver.Executor, )
| 15 | ) |
| 16 | |
| 17 | func Benchmark[TTx any](ctx context.Context, b *testing.B, |
| 18 | driverWithPool func(ctx context.Context, b *testing.B) (riverdriver.Driver[TTx], string), |
| 19 | executorWithTx func(ctx context.Context, b *testing.B) riverdriver.Executor, |
| 20 | ) { |
| 21 | b.Helper() |
| 22 | |
| 23 | setup := func(ctx context.Context, b *testing.B) (riverdriver.Executor, string) { |
| 24 | b.Helper() |
| 25 | _, schema := driverWithPool(ctx, b) |
| 26 | return executorWithTx(ctx, b), schema |
| 27 | } |
| 28 | |
| 29 | b.Run("JobSetStateIfRunningMany", func(b *testing.B) { |
| 30 | exec, schema := setup(ctx, b) |
| 31 | |
| 32 | const ( |
| 33 | totalJobs = 100000 |
| 34 | batchSize = 2000 |
| 35 | numBatches = totalJobs / batchSize |
| 36 | ) |
| 37 | |
| 38 | // Build a single batch of job parameters to be reused |
| 39 | insertParams := make([]*riverdriver.JobInsertFullParams, batchSize) |
| 40 | for i := range batchSize { |
| 41 | // Create a mix of jobs with different states and metadata |
| 42 | var ( |
| 43 | metadata []byte |
| 44 | state rivertype.JobState |
| 45 | ) |
| 46 | |
| 47 | switch i % 10 { |
| 48 | case 0, 1, 2, 3, 4, 5: |
| 49 | // Most jobs are running |
| 50 | state = rivertype.JobStateRunning |
| 51 | if i%20 == 0 { |
| 52 | // Every 20th job has cancel_attempted_at |
| 53 | metadata = []byte(`{"cancel_attempted_at": "2024-01-01T00:00:00Z"}`) |
| 54 | } |
| 55 | case 6, 7: |
| 56 | // Some jobs are completed (to test no-op path) |
| 57 | state = rivertype.JobStateCompleted |
| 58 | case 8, 9: |
| 59 | // Some jobs are available (to test no-op path) |
| 60 | state = rivertype.JobStateAvailable |
| 61 | if i%15 == 0 { |
| 62 | // Every 15th job has metadata to merge |
| 63 | metadata = []byte(`{"key": "value"}`) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | insertParams[i] = testfactory.Job_Build(b, &testfactory.JobOpts{ |
| 68 | Metadata: metadata, |
| 69 | State: &state, |
| 70 | }) |
| 71 | } |
| 72 | |
| 73 | // Insert the batch multiple times to reach our total |
| 74 | var batchJobs []*rivertype.JobRow |
searching dependent graphs…