|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "ts-benchmark/dbs/data" |
| 10 | + "ts-benchmark/dbs/mongodb" |
| 11 | + "ts-benchmark/dbs/postgres" |
| 12 | + |
| 13 | + "go.mongodb.org/mongo-driver/bson" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + TABLE = "timeseries" |
| 18 | +) |
| 19 | + |
| 20 | +func BenchmarkDbs(b *testing.B) { |
| 21 | + |
| 22 | + pgdb, err := postgres.NewConn("localhost", 5432, "postgres", "postgres", "test") |
| 23 | + if err != nil { |
| 24 | + panic(err) |
| 25 | + } |
| 26 | + defer pgdb.Close(context.Background()) |
| 27 | + |
| 28 | + const NUM_RECORDS = 10000 |
| 29 | + |
| 30 | + // Generate fake data which is reused for both MongoDB and Postgres |
| 31 | + fake := data.CreateFakeData(NUM_RECORDS) |
| 32 | + |
| 33 | + var kbMongo, kbPostgresNative, kbPostgresTimescale int32 |
| 34 | + |
| 35 | + b.Run("MongoDB-compound-single-upsert", func(b *testing.B) { |
| 36 | + db, err := mongodb.NewConn("localhost", 27017, "", "") |
| 37 | + if err != nil { |
| 38 | + b.Fatalf("Failed to connect to MongoDB: %v", err) |
| 39 | + } |
| 40 | + defer db.Disconnect(context.Background()) |
| 41 | + |
| 42 | + coll := db.Database("test").Collection(TABLE) |
| 43 | + if err := mongodb.Setup(coll); err != nil { |
| 44 | + b.Fatalf("Failed to setup MongoDB: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + b.ResetTimer() |
| 48 | + for i := 0; i < b.N; i++ { |
| 49 | + t, err := mongodb.Upsert(coll, fake) |
| 50 | + if err != nil { |
| 51 | + b.Fatalf("Failed to Upsert in MongoDB: %v", err) |
| 52 | + } |
| 53 | + _ = t |
| 54 | + } |
| 55 | + b.StopTimer() |
| 56 | + |
| 57 | + // Get the statistics of the collection |
| 58 | + var stats bson.M |
| 59 | + command := bson.D{{Key: "collStats", Value: "timeseries"}} |
| 60 | + err = db.Database("test").RunCommand(context.TODO(), command).Decode(&stats) |
| 61 | + if err != nil { |
| 62 | + log.Fatal(err) |
| 63 | + } |
| 64 | + |
| 65 | + bytes, ok := stats["totalSize"].(int32) |
| 66 | + if !ok { |
| 67 | + log.Fatal("Failed to convert to int32") |
| 68 | + } |
| 69 | + |
| 70 | + kbMongo = bytes / 1024 |
| 71 | + }) |
| 72 | + |
| 73 | + b.Run("Postgres-native-single", func(b *testing.B) { |
| 74 | + |
| 75 | + if err := postgres.SetupVanilla(pgdb); err != nil { |
| 76 | + b.Fatalf("Failed to setup Postgres: %v", err) |
| 77 | + } |
| 78 | + |
| 79 | + b.ResetTimer() |
| 80 | + for i := 0; i < b.N; i++ { |
| 81 | + t, err := postgres.UpsertVanilla(pgdb, fake) |
| 82 | + if err != nil { |
| 83 | + b.Fatalf("Failed to Upsert in Postgres: %v", err) |
| 84 | + } |
| 85 | + _ = t |
| 86 | + } |
| 87 | + b.StopTimer() |
| 88 | + |
| 89 | + tableSize, err := postgres.GetTableSize(pgdb, "timeseries") |
| 90 | + if err != nil { |
| 91 | + log.Fatal(err) |
| 92 | + } |
| 93 | + |
| 94 | + kbPostgresNative = int32(tableSize) |
| 95 | + }) |
| 96 | + |
| 97 | + b.Run("Postgres-timescale-single uspert", func(b *testing.B) { |
| 98 | + if err := postgres.SetupTimescaleDB(pgdb); err != nil { |
| 99 | + b.Fatalf("Failed to setup Postgres: %v", err) |
| 100 | + } |
| 101 | + |
| 102 | + b.ResetTimer() |
| 103 | + for i := 0; i < b.N; i++ { |
| 104 | + t, err := postgres.UpsertTimescaleDB(pgdb, fake) |
| 105 | + if err != nil { |
| 106 | + b.Fatalf("Failed to Upsert in Postgres: %v", err) |
| 107 | + } |
| 108 | + |
| 109 | + _ = t |
| 110 | + } |
| 111 | + b.StopTimer() |
| 112 | + |
| 113 | + tableSize, err := postgres.GetTableSize(pgdb, "timeseries_timescale") |
| 114 | + if err != nil { |
| 115 | + log.Fatal(err) |
| 116 | + } |
| 117 | + kbPostgresTimescale = int32(tableSize) |
| 118 | + }) |
| 119 | + |
| 120 | + b.Run("Postgres-timescale-batch upsert", func(b *testing.B) { |
| 121 | + if err := postgres.SetupTimescaleDB(pgdb); err != nil { |
| 122 | + b.Fatalf("Failed to setup Postgres: %v", err) |
| 123 | + } |
| 124 | + |
| 125 | + b.ResetTimer() |
| 126 | + for i := 0; i < b.N; i++ { |
| 127 | + t, err := postgres.UpsertBatchTimescaleDB(pgdb, fake) |
| 128 | + if err != nil { |
| 129 | + b.Fatalf("Failed to Upsert in Postgres: %v", err) |
| 130 | + } |
| 131 | + _ = t |
| 132 | + } |
| 133 | + b.StopTimer() |
| 134 | + |
| 135 | + }) |
| 136 | + |
| 137 | + fmt.Printf(` |
| 138 | +Table size summary for %v records: |
| 139 | +------------------------ |
| 140 | +MongoDB: %v kb |
| 141 | +Postgres (native): %v kb |
| 142 | +Postgres (timescale): %v kb |
| 143 | +`, NUM_RECORDS, kbMongo, kbPostgresNative, kbPostgresTimescale) |
| 144 | +} |
0 commit comments