-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
170 lines (158 loc) · 6.22 KB
/
Program.cs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// See https://aka.ms/new-console-template for more information
// get input
using Microsoft.Extensions.Configuration;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using MongoDBBenchmark;
using MongoDBBenchmark.Generator;
using static MongoDBBenchmark.InputManager;
class Program {
private const string ExitInput = "exit";
private const string BackInput = "back";
private Client _client = new Client();
static void Main(string[] args)
{
if (args.Length == 1 && args[0] == "--config")
{
Console.WriteLine("Config mode on. No input will receive from user.");
var configPath = Environment.GetEnvironmentVariable("BENCHMARK_CONFIG_PATH");
configPath = configPath != null ? configPath : "";
IConfiguration config = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile(configPath + "/operations.json")
.Build();
new Program().RunUsingConfig(config);
return;
}
new Program().RunUsingCli();
}
void RunUsingConfig(IConfiguration config)
{
_client.Connect(Environment.GetEnvironmentVariable("BENCHMARK_CONNECTION_STRING"),
Environment.GetEnvironmentVariable("BENCHMARK_DATABASE_NAME"),
Environment.GetEnvironmentVariable("BENCHMARK_COLLECTION_NAME"));
BsonSerializer.RegisterSerializer(new ObjectSerializer(type => true));
foreach (var query in config.GetSection("Operations").GetChildren())
{
Console.WriteLine("Operation started: " + query.Key);
var operation = OperationsExtensions.FromString(query["OperationType"]);
if (operation == null)
{
Console.WriteLine("Invalid operation type: " + query["OperationType"]);
continue;
}
switch (operation)
{
case Operations.Insert:
_client.Insert(Int32.Parse(query["Amount"]),
new CustomGenerator(DocumentTemplate.FromDocument(Section2BsonDocument(query.GetSection("Template")))));
break;
case Operations.Read:
_client.Read(Section2BsonDocument(query.GetSection("Filter")));
break;
case Operations.Update:
_client.Update(Section2BsonDocument(query.GetSection("Filter")),
Section2BsonDocument(query.GetSection("Update")));
break;
case Operations.Delete:
_client.Delete(Section2BsonDocument(query.GetSection("Filter")));
break;
default:
throw new ArgumentOutOfRangeException("invalid operation type: " + operation);
}
}
}
BsonDocument Section2BsonDocument(IConfigurationSection section)
{
BsonDocument bsonDocument = new BsonDocument();
foreach (var child in section.GetChildren())
{
if (child.Value != null) bsonDocument.Add(new BsonElement(child.Key, child.Value));
else bsonDocument.Add(new BsonElement(child.Key, Section2BsonDocument(child)));
}
return bsonDocument;
}
void RunUsingCli()
{
Console.WriteLine("Welcome to Mongo DB Benchmark.");
Console.WriteLine("Write 'exit' to exit.");
_client.Connect(GetStringInput("Please enter connection string: "),
GetStringInput("Please enter database name: "),
GetStringInput("Please enter collection name: "));
while (true)
StartNewOperation();
}
void StartNewOperation()
{
Operations operation = GetOperationInput();
switch (operation)
{
case Operations.Insert:
InsertOperation(GetDocumentGeneratorInput());
break;
case Operations.Read:
ReadOperation();
break;
case Operations.Update:
_client.Update(GetFilterInput(), GetDocumentInput("Please enter update document: "));
break;
case Operations.Delete:
_client.Delete(GetFilterInput());
break;
default:
throw new ArgumentOutOfRangeException("invalid operation type: " + operation);
}
}
void ReadOperation()
{
BsonDocument filter = GetFilterInput();
List<BsonDocument> result = _client.Read(filter);
Console.WriteLine("Write 'back' to go back to main menu or 'exit' to exit.");
if (result.Count > 0)
{
Console.WriteLine("Write 'show(n)' to show first n documents.");
ShowResults(result);
}
}
void InsertOperation(IDocumentGenerator documentGenerator)
{
int numOfDocuments = GetIntInput("Please enter number of documents to generate: ");
Console.WriteLine("Starting benchmark.");
_client.Insert(numOfDocuments, documentGenerator);
Console.WriteLine("Write 'back' to go back to main menu or 'exit' to exit.");
Console.WriteLine("Write anything else to insert again.");
var input = Console.ReadLine();
if (input == ExitInput)
Environment.Exit(0);
if (input == BackInput)
return;
InsertOperation(documentGenerator);
}
void ShowResults(List<BsonDocument> result)
{
while (true)
{
var input = Console.ReadLine();
if (input == ExitInput)
Environment.Exit(0);
if (input == BackInput)
return;
if (input == null)
continue;
if (input.StartsWith("show(") && input.EndsWith(")"))
{
int n;
if (!Int32.TryParse(input.Substring(5, input.Length - 6), out n))
{
Console.WriteLine("Error! Invalid number.");
continue;
}
for (int i = 0; i < Math.Min(n, result.Count); i++)
{
Console.WriteLine(i + ":" + result[i]);
}
}
}
}
}