Skip to content

Commit 2c31c67

Browse files
committed
Add Nbomber example using MongoDB
1 parent 9b7e92d commit 2c31c67

File tree

9 files changed

+210
-0
lines changed

9 files changed

+210
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using NBomber.CSharp;
2+
3+
namespace Demo.DB.MongoDB
4+
{
5+
public class MongoDBExample
6+
{
7+
public void Run()
8+
{
9+
var initScenario = new MongoInitScenario().Create();
10+
var readScenario = new MongoReadScenario().Create();
11+
var writeScenario = new MongoWriteScenario().Create();
12+
13+
NBomberRunner.RegisterScenarios(initScenario, readScenario, writeScenario)
14+
.WithoutReports()
15+
.LoadConfig("./DB/MongoDB/config.json")
16+
.Run();
17+
}
18+
}
19+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Microsoft.Extensions.Configuration;
2+
using MongoDB.Driver;
3+
using NBomber.Contracts;
4+
using NBomber.CSharp;
5+
using NBomber.Data;
6+
7+
namespace Demo.DB.MongoDB
8+
{
9+
public class MongoConfig
10+
{
11+
public string ConnectionString { get; set; }
12+
public int DataSize { get; set; }
13+
public int UsersCount { get; set; }
14+
}
15+
16+
public class MongoInitScenario
17+
{
18+
public ScenarioProps Create()
19+
{
20+
return Scenario.Empty("mongo_init")
21+
.WithInit(async context =>
22+
{
23+
var config = context.GlobalCustomSettings.Get<MongoConfig>();
24+
var client = new MongoClient(config.ConnectionString);
25+
var db = client.GetDatabase("MongoDb");
26+
var usersCollection = db.GetCollection<User>("Users");
27+
var data = Data.GenerateRandomBytes(config.DataSize);
28+
29+
var users = new List<User>();
30+
31+
foreach (var i in Enumerable.Range(0, config.UsersCount))
32+
{
33+
users.Add(new User() { Id = i, Data = data });
34+
}
35+
36+
await usersCollection.DeleteManyAsync(x => true);
37+
await usersCollection.InsertManyAsync(users);
38+
});
39+
}
40+
}
41+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Microsoft.Extensions.Configuration;
2+
using MongoDB.Driver;
3+
using NBomber.Contracts;
4+
using NBomber.CSharp;
5+
6+
namespace Demo.DB.MongoDB
7+
{
8+
public class MongoReadScenario
9+
{
10+
private MongoConfig _config;
11+
private IMongoCollection<User> _users;
12+
13+
public ScenarioProps Create()
14+
{
15+
return Scenario.Create("mongo_read", async context =>
16+
{
17+
var id = context.Random.Next(_config.UsersCount);
18+
var user = (await _users.FindAsync(x => x.Id == id)).FirstOrDefault();
19+
20+
return user != null
21+
? Response.Ok(sizeBytes: user.Data.Length)
22+
: Response.Fail(message: "Not found");
23+
})
24+
.WithInit(context =>
25+
{
26+
_config = context.GlobalCustomSettings.Get<MongoConfig>();
27+
var connection = new MongoClient(_config.ConnectionString);
28+
var db = connection.GetDatabase("MongoDb");
29+
_users = db.GetCollection<User>("Users");
30+
31+
return Task.CompletedTask;
32+
});
33+
}
34+
}
35+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Microsoft.Extensions.Configuration;
2+
using MongoDB.Driver;
3+
using NBomber.Contracts;
4+
using NBomber.CSharp;
5+
using NBomber.Data;
6+
7+
namespace Demo.DB.MongoDB
8+
{
9+
public class MongoWriteScenario
10+
{
11+
private MongoConfig _config;
12+
private IMongoCollection<User> _users;
13+
private byte[] _data;
14+
15+
public ScenarioProps Create()
16+
{
17+
return Scenario.Create("mongo_write", async context =>
18+
{
19+
var id = context.Random.Next(_config.UsersCount);
20+
var user = new User() { Id = id, Data = _data };
21+
var result = await _users.ReplaceOneAsync(x => x.Id == id, user, new ReplaceOptions() { IsUpsert = true });
22+
23+
return result.IsAcknowledged
24+
? Response.Ok(sizeBytes: _data.Length)
25+
: Response.Fail();
26+
})
27+
.WithInit(context =>
28+
{
29+
_config = context.GlobalCustomSettings.Get<MongoConfig>();
30+
_data = Data.GenerateRandomBytes(_config.DataSize);
31+
32+
var settings = MongoClientSettings.FromConnectionString(_config.ConnectionString);
33+
settings.ConnectTimeout = TimeSpan.FromSeconds(5);
34+
var connection = new MongoClient(settings);
35+
var db = connection.GetDatabase("MongoDb");
36+
_users = db.GetCollection<User>("Users");
37+
38+
return Task.CompletedTask;
39+
});
40+
}
41+
}
42+
}

examples/Demo/DB/MongoDB/User.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Demo.DB.MongoDB
2+
{
3+
public class User
4+
{
5+
public int Id { get; set; }
6+
public byte[] Data { get; set; }
7+
}
8+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"TargetScenarios": [ "mongo_init", "mongo_read", "mongo_write" ],
3+
4+
"GlobalSettings": {
5+
6+
"ScenariosSettings": [
7+
{
8+
"ScenarioName": "mongo_read",
9+
10+
"WarmUpDuration": "00:00:05",
11+
12+
"LoadSimulationsSettings": [
13+
{ "KeepConstant": [ 100, "00:03:30" ] }
14+
]
15+
},
16+
17+
{
18+
"ScenarioName": "mongo_write",
19+
20+
"WarmUpDuration": "00:00:05",
21+
22+
"LoadSimulationsSettings": [
23+
{ "KeepConstant": [ 100, "00:03:30" ] }
24+
]
25+
}
26+
],
27+
28+
"GlobalCustomSettings": {
29+
30+
"ConnectionString": "mongodb://localhost:27017",
31+
32+
"UsersCount": 100000,
33+
34+
"DataSize": 1000
35+
}
36+
}
37+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
services:
2+
3+
mongodb:
4+
image: mongo
5+
ports:
6+
- "27017:27017"
7+
volumes:
8+
- mongo_data:/data/db
9+
10+
mongo-express:
11+
image: mongo-express
12+
ports:
13+
- "8081:8081"
14+
environment:
15+
ME_CONFIG_MONGODB_URL: mongodb://mongodb:27017/
16+
ME_CONFIG_BASICAUTH_ENABLED: true
17+
ME_CONFIG_BASICAUTH_USERNAME: mongoexpressuser
18+
ME_CONFIG_BASICAUTH_PASSWORD: mongoexpressuser
19+
20+
volumes:
21+
mongo_data:
22+

examples/Demo/Demo.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<None Update="Cluster\ManualCluster\manual-cluster-config.json">
2424
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2525
</None>
26+
<None Update="DB\MongoDB\config.json">
27+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
28+
</None>
2629
<None Update="Features\RealtimeReporting\InfluxDB\influx_v2\infra-config.json">
2730
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2831
</None>
@@ -85,6 +88,7 @@
8588
<PackageReference Include="Dapper.Contrib" Version="2.0.78" />
8689
<PackageReference Include="Grpc.Net.Client" Version="2.71.0" />
8790
<PackageReference Include="LiteDB" Version="5.0.15" />
91+
<PackageReference Include="MongoDB.Driver" Version="3.5.0" />
8892

8993
<PackageReference Include="NBomber" Version="6.1.2" />
9094

examples/Demo/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
using Demo.Features.Timeouts;
3737
using Demo.HTTP.HttpApiSimulator;
3838
using Demo.HTTP.RestSharpDemo;
39+
using Demo.DB.MongoDB;
3940

4041
// -------------------------------
4142
// ----- Hello World examples -----
@@ -144,6 +145,7 @@
144145
// new LiteDBExample().Run();
145146
// new SQLiteDBExample().Run();
146147
// new RedisExample().Run();
148+
// new MongoDBExample().Run();
147149

148150
// ---------------------
149151
// ----- Cluster -------

0 commit comments

Comments
 (0)