-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cs
More file actions
83 lines (72 loc) · 3.32 KB
/
Client.cs
File metadata and controls
83 lines (72 loc) · 3.32 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
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDBBenchmark.Generator;
namespace MongoDBBenchmark;
public class Client
{
private IMongoCollection<BsonDocument> _collection;
public void Connect(string? connectionString, string? databaseName, string? collectionName)
{
if (connectionString == null || databaseName == null || collectionName == null)
{
Console.WriteLine("Connection string, database name and collection name must be provided.");
Console.WriteLine("Please ensure that you have provided these environment variables:");
Console.WriteLine("BENCHMARK_DATABASE_NAME, BENCHMARK_COLLECTION_NAME, BENCHMARK_CONNECTION_STRING");
Environment.Exit(0);
}
MongoClient client;
try {
client = new MongoClient(connectionString);
} catch (Exception e) {
Console.WriteLine("Could not connected to the database.");
Console.WriteLine(e.Message);
return;
}
var database = client.GetDatabase(databaseName);
_collection = database.GetCollection<BsonDocument>(collectionName);
}
public List<BsonDocument> Read(BsonDocument filter)
{
Console.WriteLine("Starting benchmark.");
var benchmark = new Benchmark<List<BsonDocument>>(
() => _collection.Find(filter).ToList(),
filter.ToString());
List<BsonDocument> result = benchmark.Run(res => "READ - Count: " + res.Count+ ", Time: {time}, Filter: {input}");
Console.WriteLine("Benchmark finished. Time elapsed: " + benchmark.GetElapsedTime());
Console.WriteLine("Totally " + result.Count + " documents found.");
return result;
}
public void Delete(BsonDocument filter)
{
Console.WriteLine("Starting benchmark.");
var benchmark = new Benchmark<DeleteResult>(
() => _collection.DeleteMany(filter),
filter.ToString());
benchmark.Run(res => "DELETE - Count: " + res.DeletedCount + ", Time {time}, Filter: {input}");
Console.WriteLine("Benchmark finished. Time elapsed: " + benchmark.GetElapsedTime());
}
public void Update(BsonDocument filter, BsonDocument update)
{
var benchmark = new Benchmark<UpdateResult>(
() => _collection.UpdateMany(filter, update),
filter.ToString());
benchmark.Run(res => "UPDATE - Count: " + res.ModifiedCount + ", Time {time}, Filter: {input}, Updated: " + update);
Console.WriteLine("Benchmark finished. Time elapsed: " + benchmark.GetElapsedTime());
}
public void Insert(int numberOfDocuments, IDocumentGenerator documentGenerator)
{
Console.WriteLine("Documents Generating...");
BsonDocument[] documents = documentGenerator.Generate(numberOfDocuments);
Console.WriteLine("Bulk Inserting...");
var benchmark = new Benchmark<object>(
() =>
{
_collection.InsertMany(documents);
return null;
},
documentGenerator.GetRawTemplate());
benchmark.Run(x => "INSERT - Count: " + numberOfDocuments + ", Time: {time}, Template: {input}");
Console.WriteLine("Inserted " + numberOfDocuments + " documents.");
Console.WriteLine("Benchmark finished. Time elapsed: " + benchmark.GetElapsedTime());
}
}