-
Notifications
You must be signed in to change notification settings - Fork 137
Add Nbomber example using MongoDB #903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
| } | ||
|
|
||
| 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); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| }); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| }); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add enter