Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
102 lines (87 loc) · 2.83 KB
/
Copy pathmain.go
File metadata and controls
102 lines (87 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"flag"
"fmt"
"log"
"os"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
var (
threads int
docCount int
uri string
certificatePath string
testType string
duration int
runAll bool
largeDocs bool
dropDb bool
)
flag.IntVar(&threads, "threads", 10, "Number of threads for inserting, updating, upserting, or deleting documents")
flag.IntVar(&docCount, "docs", 1000, "Total number of documents to insert, update, upsert, or delete")
flag.StringVar(&uri, "uri", "mongodb://localhost:27017", "MongoDB URI")
flag.StringVar(&certificatePath, "tlsCert", "", "Path to TLS certificate")
flag.StringVar(&testType, "type", "insert", "Test type: insert, update, upsert, or delete")
flag.BoolVar(&runAll, "runAll", false, "Run all tests in order: insert, update, delete, upsert")
flag.IntVar(&duration, "duration", 0, "Duration in seconds to run the test")
flag.BoolVar(&largeDocs, "largeDocs", false, "Use large documents for testing")
flag.BoolVar(&dropDb, "dropDb", true, "Drop the database before running the test")
flag.Parse()
var strategy TestingStrategy
var config TestingConfig
clientOptions := options.Client().ApplyURI(uri).SetMaxPoolSize(uint64(threads))
if certificatePath != "" {
tlsConfig, err := createTLSConfigFromFile(certificatePath)
if err != nil {
log.Fatalf("Failed to create TLS config from %s: %v", certificatePath, err)
}
clientOptions = clientOptions.SetTLSConfig(tlsConfig)
}
client, err := mongo.Connect(context.Background(), clientOptions)
if err != nil {
log.Fatalf("Failed to connect to MongoDB: %v", err)
}
defer func(client *mongo.Client, ctx context.Context) {
err := client.Disconnect(ctx)
if err != nil {
log.Fatalf("Failed to disconnect from MongoDB: %v", err)
}
}(client, context.Background())
collection := client.Database("benchmarking").Collection("testdata")
mongoCollection := &MongoDBCollection{Collection: collection}
config = TestingConfig{
Threads: threads,
Duration: duration,
DocCount: docCount,
LargeDocs: largeDocs,
DropDb: dropDb,
}
if duration > 0 {
strategy = DurationTestingStrategy{}
} else {
strategy = DocCountTestingStrategy{}
}
if runAll {
strategy.runTestSequence(mongoCollection, config)
} else {
strategy.runTest(mongoCollection, testType, config, fetchDocumentIDs)
}
}
func createTLSConfigFromFile(tlsCertificate string) (*tls.Config, error) {
caCert, err := os.ReadFile(tlsCertificate)
if err != nil {
return nil, err
}
caCertPool := x509.NewCertPool()
if ok := caCertPool.AppendCertsFromPEM(caCert); !ok {
return nil, fmt.Errorf("failed to parse TLS certificate from %s", tlsCertificate)
}
return &tls.Config{
RootCAs: caCertPool,
}, nil
}