|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package benchmarks |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "database/sql" |
| 8 | + "strconv" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/lib/pq" |
| 12 | + "github.com/oapi-codegen/nullable" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "github.com/xataio/pgroll/internal/testutils" |
| 16 | + "github.com/xataio/pgroll/pkg/migrations" |
| 17 | + "github.com/xataio/pgroll/pkg/roll" |
| 18 | +) |
| 19 | + |
| 20 | +const unitRowsPerSecond = "rows/s" |
| 21 | + |
| 22 | +var rowCounts = []int{10_000, 100_000, 300_000} |
| 23 | + |
| 24 | +func TestMain(m *testing.M) { |
| 25 | + testutils.SharedTestMain(m) |
| 26 | +} |
| 27 | + |
| 28 | +func BenchmarkBackfill(b *testing.B) { |
| 29 | + ctx := context.Background() |
| 30 | + testSchema := testutils.TestSchema() |
| 31 | + var opts []roll.Option |
| 32 | + |
| 33 | + for _, rowCount := range rowCounts { |
| 34 | + b.Run(strconv.Itoa(rowCount), func(b *testing.B) { |
| 35 | + testutils.WithMigratorInSchemaAndConnectionToContainerWithOptions(b, testSchema, opts, func(mig *roll.Roll, db *sql.DB) { |
| 36 | + b.Cleanup(func() { |
| 37 | + require.NoError(b, mig.Close()) |
| 38 | + }) |
| 39 | + |
| 40 | + setupInitialTable(b, ctx, testSchema, mig, db, rowCount) |
| 41 | + b.ResetTimer() |
| 42 | + |
| 43 | + // Backfill |
| 44 | + b.StartTimer() |
| 45 | + require.NoError(b, mig.Start(ctx, &migAlterColumn)) |
| 46 | + require.NoError(b, mig.Complete(ctx)) |
| 47 | + b.StopTimer() |
| 48 | + b.Logf("Backfilled %d rows in %s", rowCount, b.Elapsed()) |
| 49 | + rowsPerSecond := float64(rowCount) / b.Elapsed().Seconds() |
| 50 | + b.ReportMetric(rowsPerSecond, unitRowsPerSecond) |
| 51 | + }) |
| 52 | + }) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// Benchmark the difference between updating all rows with and without an update trigger in place |
| 57 | +func BenchmarkWriteAmplification(b *testing.B) { |
| 58 | + ctx := context.Background() |
| 59 | + testSchema := testutils.TestSchema() |
| 60 | + var opts []roll.Option |
| 61 | + |
| 62 | + assertRowCount := func(tb testing.TB, db *sql.DB, rowCount int) { |
| 63 | + tb.Helper() |
| 64 | + |
| 65 | + var count int |
| 66 | + err := db.QueryRowContext(ctx, "SELECT COUNT(*) FROM users WHERE name = 'person'").Scan(&count) |
| 67 | + require.NoError(b, err) |
| 68 | + require.Equal(b, rowCount, count) |
| 69 | + } |
| 70 | + |
| 71 | + b.Run("NoTrigger", func(b *testing.B) { |
| 72 | + for _, rowCount := range rowCounts { |
| 73 | + b.Run(strconv.Itoa(rowCount), func(b *testing.B) { |
| 74 | + testutils.WithMigratorInSchemaAndConnectionToContainerWithOptions(b, testSchema, opts, func(mig *roll.Roll, db *sql.DB) { |
| 75 | + setupInitialTable(b, ctx, testSchema, mig, db, rowCount) |
| 76 | + b.Cleanup(func() { |
| 77 | + require.NoError(b, mig.Close()) |
| 78 | + assertRowCount(b, db, rowCount) |
| 79 | + }) |
| 80 | + |
| 81 | + b.ResetTimer() |
| 82 | + |
| 83 | + // Update the name in all rows |
| 84 | + b.StartTimer() |
| 85 | + _, err := db.ExecContext(ctx, `UPDATE users SET name = 'person'`) |
| 86 | + require.NoError(b, err) |
| 87 | + b.StopTimer() |
| 88 | + rowsPerSecond := float64(rowCount) / b.Elapsed().Seconds() |
| 89 | + b.ReportMetric(rowsPerSecond, unitRowsPerSecond) |
| 90 | + }) |
| 91 | + }) |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + b.Run("WithTrigger", func(b *testing.B) { |
| 96 | + for _, rowCount := range rowCounts { |
| 97 | + b.Run(strconv.Itoa(rowCount), func(b *testing.B) { |
| 98 | + testutils.WithMigratorInSchemaAndConnectionToContainerWithOptions(b, testSchema, opts, func(mig *roll.Roll, db *sql.DB) { |
| 99 | + setupInitialTable(b, ctx, testSchema, mig, db, rowCount) |
| 100 | + |
| 101 | + // Start the migration |
| 102 | + require.NoError(b, mig.Start(ctx, &migAlterColumn)) |
| 103 | + b.Cleanup(func() { |
| 104 | + // Finish the migration |
| 105 | + require.NoError(b, mig.Complete(ctx)) |
| 106 | + require.NoError(b, mig.Close()) |
| 107 | + assertRowCount(b, db, rowCount) |
| 108 | + }) |
| 109 | + |
| 110 | + b.ResetTimer() |
| 111 | + |
| 112 | + // Update the name in all rows |
| 113 | + b.StartTimer() |
| 114 | + _, err := db.ExecContext(ctx, `UPDATE users SET name = 'person'`) |
| 115 | + require.NoError(b, err) |
| 116 | + b.StopTimer() |
| 117 | + rowsPerSecond := float64(rowCount) / b.Elapsed().Seconds() |
| 118 | + b.ReportMetric(rowsPerSecond, unitRowsPerSecond) |
| 119 | + }) |
| 120 | + }) |
| 121 | + } |
| 122 | + }) |
| 123 | +} |
| 124 | + |
| 125 | +func setupInitialTable(tb testing.TB, ctx context.Context, testSchema string, mig *roll.Roll, db *sql.DB, rowCount int) { |
| 126 | + tb.Helper() |
| 127 | + |
| 128 | + seed := func(tb testing.TB, rowCount int, db *sql.DB) { |
| 129 | + tx, err := db.Begin() |
| 130 | + require.NoError(tb, err) |
| 131 | + defer tx.Rollback() |
| 132 | + |
| 133 | + stmt, err := tx.PrepareContext(ctx, pq.CopyInSchema(testSchema, "users", "name")) |
| 134 | + require.NoError(tb, err) |
| 135 | + |
| 136 | + for i := 0; i < rowCount; i++ { |
| 137 | + _, err = stmt.ExecContext(ctx, nil) |
| 138 | + require.NoError(tb, err) |
| 139 | + } |
| 140 | + |
| 141 | + _, err = stmt.ExecContext(ctx) |
| 142 | + require.NoError(tb, err) |
| 143 | + require.NoError(tb, tx.Commit()) |
| 144 | + } |
| 145 | + |
| 146 | + // Setup |
| 147 | + require.NoError(tb, mig.Start(ctx, &migCreateTable)) |
| 148 | + require.NoError(tb, mig.Complete(ctx)) |
| 149 | + seed(tb, rowCount, db) |
| 150 | +} |
| 151 | + |
| 152 | +// Simple table with a nullable `name` field. |
| 153 | +var migCreateTable = migrations.Migration{ |
| 154 | + Name: "01_create_table", |
| 155 | + Operations: migrations.Operations{ |
| 156 | + &migrations.OpCreateTable{ |
| 157 | + Name: "users", |
| 158 | + Columns: []migrations.Column{ |
| 159 | + { |
| 160 | + Name: "id", |
| 161 | + Type: "serial", |
| 162 | + Pk: ptr(true), |
| 163 | + }, |
| 164 | + { |
| 165 | + Name: "name", |
| 166 | + Type: "varchar(255)", |
| 167 | + Nullable: ptr(true), |
| 168 | + Unique: ptr(false), |
| 169 | + }, |
| 170 | + }, |
| 171 | + }, |
| 172 | + }, |
| 173 | +} |
| 174 | + |
| 175 | +// Alter the table to make the name field not null and backfill the old name fields with |
| 176 | +// `placeholder`. |
| 177 | +var migAlterColumn = migrations.Migration{ |
| 178 | + Name: "02_alter_column", |
| 179 | + Operations: migrations.Operations{ |
| 180 | + &migrations.OpAlterColumn{ |
| 181 | + Table: "users", |
| 182 | + Column: "name", |
| 183 | + Up: "(SELECT CASE WHEN name IS NULL THEN 'placeholder' ELSE name END)", |
| 184 | + Down: "user_name", |
| 185 | + Comment: nullable.NewNullableWithValue("the name of the user"), |
| 186 | + Nullable: ptr(false), |
| 187 | + }, |
| 188 | + }, |
| 189 | +} |
| 190 | + |
| 191 | +func ptr[T any](x T) *T { return &x } |
0 commit comments