Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions examples/Demo/DB/MongoDB/MongoDBExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using NBomber.CSharp;

namespace Demo.DB.MongoDB
{
public class MongoDBExample
{
public void Run()
{
var initScenario = new MongoInitScenario().Create();
var readScenario = new MongoReadScenario().Create();
var writeScenario = new MongoWriteScenario().Create();

NBomberRunner.RegisterScenarios(initScenario, readScenario, writeScenario)
.WithoutReports()
.LoadConfig("./DB/MongoDB/config.json")
.Run();
}
}
}
41 changes: 41 additions & 0 deletions examples/Demo/DB/MongoDB/MongoInitScenario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Microsoft.Extensions.Configuration;
using MongoDB.Driver;
using NBomber.Contracts;
using NBomber.CSharp;
using NBomber.Data;

namespace Demo.DB.MongoDB
{
public class MongoConfig
{
public string ConnectionString { get; set; }
public int DataSize { get; set; }
public int UsersCount { get; set; }
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add enter


public class MongoInitScenario
{
public ScenarioProps Create()
{
return Scenario.Empty("mongo_init")
.WithInit(async context =>
{
var config = context.GlobalCustomSettings.Get<MongoConfig>();
var client = new MongoClient(config.ConnectionString);
var db = client.GetDatabase("MongoDb");
var usersCollection = db.GetCollection<User>("Users");
var data = Data.GenerateRandomBytes(config.DataSize);

var users = new List<User>();

foreach (var i in Enumerable.Range(0, config.UsersCount))
{
users.Add(new User() { Id = i, Data = data });
}

await usersCollection.DeleteManyAsync(x => true);
await usersCollection.InsertManyAsync(users);
});
}
}
}
35 changes: 35 additions & 0 deletions examples/Demo/DB/MongoDB/MongoReadScenario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.Extensions.Configuration;
using MongoDB.Driver;
using NBomber.Contracts;
using NBomber.CSharp;

namespace Demo.DB.MongoDB
{
public class MongoReadScenario
{
private MongoConfig _config;
private IMongoCollection<User> _users;

public ScenarioProps Create()
{
return Scenario.Create("mongo_read", async context =>
{
var id = context.Random.Next(_config.UsersCount);
var user = (await _users.FindAsync(x => x.Id == id)).FirstOrDefault();

return user != null
? Response.Ok(sizeBytes: user.Data.Length)
: Response.Fail(message: "Not found");
})
.WithInit(context =>
{
_config = context.GlobalCustomSettings.Get<MongoConfig>();
var connection = new MongoClient(_config.ConnectionString);
var db = connection.GetDatabase("MongoDb");
_users = db.GetCollection<User>("Users");

return Task.CompletedTask;
});
}
}
}
42 changes: 42 additions & 0 deletions examples/Demo/DB/MongoDB/MongoWriteScenario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.Extensions.Configuration;
using MongoDB.Driver;
using NBomber.Contracts;
using NBomber.CSharp;
using NBomber.Data;

namespace Demo.DB.MongoDB
{
public class MongoWriteScenario
{
private MongoConfig _config;
private IMongoCollection<User> _users;
private byte[] _data;

public ScenarioProps Create()
{
return Scenario.Create("mongo_write", async context =>
{
var id = context.Random.Next(_config.UsersCount);
var user = new User() { Id = id, Data = _data };
var result = await _users.ReplaceOneAsync(x => x.Id == id, user, new ReplaceOptions() { IsUpsert = true });

return result.IsAcknowledged
? Response.Ok(sizeBytes: _data.Length)
: Response.Fail();
})
.WithInit(context =>
{
_config = context.GlobalCustomSettings.Get<MongoConfig>();
_data = Data.GenerateRandomBytes(_config.DataSize);

var settings = MongoClientSettings.FromConnectionString(_config.ConnectionString);
settings.ConnectTimeout = TimeSpan.FromSeconds(5);
var connection = new MongoClient(settings);
var db = connection.GetDatabase("MongoDb");
_users = db.GetCollection<User>("Users");

return Task.CompletedTask;
});
}
}
}
8 changes: 8 additions & 0 deletions examples/Demo/DB/MongoDB/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Demo.DB.MongoDB
{
public class User
{
public int Id { get; set; }
public byte[] Data { get; set; }
}
}
37 changes: 37 additions & 0 deletions examples/Demo/DB/MongoDB/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"TargetScenarios": [ "mongo_init", "mongo_read", "mongo_write" ],

"GlobalSettings": {

"ScenariosSettings": [
{
"ScenarioName": "mongo_read",

"WarmUpDuration": "00:00:05",

"LoadSimulationsSettings": [
{ "KeepConstant": [ 100, "00:03:30" ] }
]
},

{
"ScenarioName": "mongo_write",

"WarmUpDuration": "00:00:05",

"LoadSimulationsSettings": [
{ "KeepConstant": [ 100, "00:03:30" ] }
]
}
],

"GlobalCustomSettings": {

"ConnectionString": "mongodb://localhost:27017",

"UsersCount": 100000,

"DataSize": 1000
}
}
}
22 changes: 22 additions & 0 deletions examples/Demo/DB/MongoDB/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
services:

mongodb:
image: mongo
ports:
- "27017:27017"
volumes:
- mongo_data:/data/db

mongo-express:
image: mongo-express
ports:
- "8081:8081"
environment:
ME_CONFIG_MONGODB_URL: mongodb://mongodb:27017/
ME_CONFIG_BASICAUTH_ENABLED: true
ME_CONFIG_BASICAUTH_USERNAME: mongoexpressuser
ME_CONFIG_BASICAUTH_PASSWORD: mongoexpressuser

volumes:
mongo_data:

4 changes: 4 additions & 0 deletions examples/Demo/Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<None Update="Cluster\ManualCluster\manual-cluster-config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="DB\MongoDB\config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Features\RealtimeReporting\InfluxDB\influx_v2\infra-config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -85,6 +88,7 @@
<PackageReference Include="Dapper.Contrib" Version="2.0.78" />
<PackageReference Include="Grpc.Net.Client" Version="2.71.0" />
<PackageReference Include="LiteDB" Version="5.0.15" />
<PackageReference Include="MongoDB.Driver" Version="3.5.0" />

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

Expand Down
2 changes: 2 additions & 0 deletions examples/Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
using Demo.Features.Timeouts;
using Demo.HTTP.HttpApiSimulator;
using Demo.HTTP.RestSharpDemo;
using Demo.DB.MongoDB;

// -------------------------------
// ----- Hello World examples -----
Expand Down Expand Up @@ -144,6 +145,7 @@
// new LiteDBExample().Run();
// new SQLiteDBExample().Run();
// new RedisExample().Run();
// new MongoDBExample().Run();

// ---------------------
// ----- Cluster -------
Expand Down