Skip to content

Commit

Permalink
improved InitDBScenario and added checkpointDB for LiteDb
Browse files Browse the repository at this point in the history
  • Loading branch information
AntyaDev committed Feb 13, 2023
1 parent 3bfd820 commit 42b33e5
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@
using Microsoft.Extensions.Configuration;
using NBomber.Contracts;
using NBomber.CSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyLoadTest
namespace CSharpProd.DB.LiteDB
{
public class LiteDBCustomSettings
{
Expand All @@ -19,10 +14,9 @@ public class LiteDBCustomSettings

internal class InitDBScenario
{
private LiteDatabase _db = null;

public LiteDatabase Db { get; private set; }
public ILiteCollection<User> Collection { get; private set; }
public int RecordBiteSize { get; private set; }
public int RecordSizeBytes { get; private set; }
public LiteDBCustomSettings DBSettings { get; private set;}

public ScenarioProps Create()
Expand All @@ -33,12 +27,12 @@ public ScenarioProps Create()
{
DBSettings = context.CustomSettings.Get<LiteDBCustomSettings>();

_db = new LiteDatabase("UsersRegister.db");
Db = new LiteDatabase("UsersRegister.db");

Collection = _db.GetCollection<User>("users");
Collection = Db.GetCollection<User>("users");
var collectionCount = Collection.Count();

if (DBSettings.UserCount >= collectionCount)
if (DBSettings.UserCount > collectionCount)
{
var lastUser = Collection.Query().OrderByDescending(c => c._id).FirstOrDefault();
var maxId = 0;
Expand Down Expand Up @@ -95,13 +89,14 @@ public ScenarioProps Create()
Collection.Insert(listOfUser);
}

RecordBiteSize = CalculateRecordSize(Collection);
RecordSizeBytes = CalculateRecordSize(Collection);

return Task.CompletedTask;
})
.WithClean(context =>
{
_db.Dispose();
Db.Checkpoint();
Db.Dispose();
return Task.CompletedTask;
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
using Bogus;
using LiteDB;
using MyLoadTest;
using NBomber.CSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpProd.Db.LiteDB
namespace CSharpProd.DB.LiteDB
{
public class LiteDBExample
{
Expand All @@ -22,7 +16,7 @@ public void Run()
var randomId = random.Next(1, initDBScn.DBSettings.UserCount);
var randomUser = initDBScn.Collection.FindById(new BsonValue(randomId));

return Response.Ok(sizeBytes: initDBScn.RecordBiteSize);
return Response.Ok(sizeBytes: initDBScn.RecordSizeBytes);
});

var update = Scenario.Create("update", async context =>
Expand All @@ -32,7 +26,7 @@ public void Run()
var randomUser = initDBScn.Collection.FindById(new BsonValue(randomId));
var num = initDBScn.Collection.UpdateMany("{Age:$.Age+1, Updated:NOW_UTC()}", $"_id = {randomId}");

return Response.Ok(sizeBytes: initDBScn.RecordBiteSize);
return Response.Ok(sizeBytes: initDBScn.RecordSizeBytes);
});

var readModifyWrite = Scenario.Create("read_modify_write", async context =>
Expand All @@ -45,7 +39,7 @@ public void Run()

initDBScn.Collection.Upsert(randomUser);

return Response.Ok(sizeBytes: initDBScn.RecordBiteSize * 2);
return Response.Ok(sizeBytes: initDBScn.RecordSizeBytes * 2);
});

var faker = new Faker();
Expand All @@ -57,11 +51,18 @@ public void Run()
.Limit(10)
.ToList();

return Response.Ok(sizeBytes: initDBScn.RecordBiteSize * listOfRandomUser.Count);
return Response.Ok(sizeBytes: initDBScn.RecordSizeBytes * listOfRandomUser.Count);
});

var checkpointDB = Scenario.Create("checkpointDB", async context =>
{
await Task.Delay(5_000);
initDBScn.Db.Checkpoint();
return Response.Ok();
});

NBomberRunner
.RegisterScenarios(initDBScn.Create(), getById, update, readModifyWrite, conditionalQuery)
.RegisterScenarios(initDBScn.Create(), checkpointDB, getById, update, readModifyWrite, conditionalQuery)
.LoadConfig("Db/LiteDB/config.json")
.Run();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

namespace MyLoadTest
namespace CSharpProd.DB.LiteDB
{
public enum Gender
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"TargetScenarios": [ "initDB", "get_by_id", "update", "read_modify_write", "conditional_query" ],
"TargetScenarios": [ "initDB", "checkpointDB", "get_by_id", "read_modify_write" ],

"GlobalSettings": {

Expand All @@ -8,36 +8,43 @@
"ScenarioName": "initDB",

"CustomSettings": {
"UserCount": 10000,
"InsertBulcSize": 500
"UserCount": 500000,
"InsertBulcSize": 2000
}
},
{
"ScenarioName": "checkpointDB",

"LoadSimulationsSettings": [
{ "KeepConstant": [ 1, "00:03:00" ] }
]
},
{
"ScenarioName": "get_by_id",

"LoadSimulationsSettings": [
{ "KeepConstant": [ 10, "00:00:30" ] }
{ "KeepConstant": [ 10, "00:03:00" ] }
]
},
{
"ScenarioName": "update",

"LoadSimulationsSettings": [
{ "KeepConstant": [ 10, "00:00:30" ] }
{ "KeepConstant": [ 1, "00:03:00" ] }
]
},
{
"ScenarioName": "read_modify_write",

"LoadSimulationsSettings": [
{ "KeepConstant": [ 10, "00:00:30" ] }
{ "KeepConstant": [ 1, "00:03:00" ] }
]
},
{
"ScenarioName": "conditional_query",

"LoadSimulationsSettings": [
{ "KeepConstant": [ 10, "00:00:30" ] }
{ "KeepConstant": [ 1, "00:03:00" ] }
]
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using Microsoft.Extensions.Configuration;
using NBomber.Contracts;
using NBomber.Contracts.Stats;
using NBomber.CSharp;

namespace CSharpProd.Features.CustomSettings;

public class CustomScenarioSettings
{
public int TestField { get; set; }
public int PauseMs { get; set; }
public int MyTestField { get; set; }
public int MyPauseMs { get; set; }
}

public class CustomSettingsExample
Expand All @@ -19,8 +20,8 @@ Task Init(IScenarioInitContext initContext)
_customSettings = initContext.CustomSettings.Get<CustomScenarioSettings>();

initContext.Logger.Information(
"test init received CustomSettings.TestField '{TestField}'",
_customSettings.TestField
"test init received CustomSettings.MyTestField '{0}'",
_customSettings.MyTestField
);

return Task.CompletedTask;
Expand All @@ -30,23 +31,33 @@ public void Run()
{
var scenario = Scenario.Create("my_scenario", async context =>
{
await Task.Delay(_customSettings.PauseMs);
await Task.Delay(_customSettings.MyPauseMs);

var step = await Step.Run("step", context, async () =>
{
await Task.Delay(1_000);
context.Logger.Debug("step received CustomSettings.TestField '{0}'", _customSettings.TestField);
context.Logger.Debug("step received CustomSettings.MyTestField '{0}'", _customSettings.MyTestField);
return Response.Ok();
});

return Response.Ok();
})
.WithInit(Init)
.WithoutWarmUp();
.WithLoadSimulations(Simulation.Inject(rate: 50, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromMinutes(1)))
.WithWarmUpDuration(TimeSpan.FromSeconds(10))
.WithMaxFailCount(1_000);

NBomberRunner
.RegisterScenarios(scenario)
.LoadConfig("./Features/CustomSettings/config.json")
.WithTestSuite("my test suite")
.WithTestName("my test name")
.WithTargetScenarios("my_scenario")
.WithReportFileName("my_report")
.WithReportFolder("report_folder")
.WithReportFormats(ReportFormat.Txt, ReportFormat.Html)
.WithReportingInterval(TimeSpan.FromSeconds(10))
.EnableHintsAnalyzer(true)
.Run();
}
}
Expand Down
16 changes: 10 additions & 6 deletions examples/CSharpProd/Features/CustomSettings/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@
],

"CustomSettings": {
"TestField": 1,
"PauseMs": 200
}
"MyTestField": 1,
"MyPauseMs": 200
},

"MaxFailCount": 500
}
],

"ReportFileName": "my_report_name",
"ReportFolder": "./reports",
"ReportFormats": [ "Html", "Md", "Txt", "Csv" ]
"ReportFileName": "my_custom_report_name",
"ReportFolder": "./my_reports",
"ReportFormats": ["Html", "Txt"],
"ReportingInterval": "00:00:30",
"EnableHintsAnalyzer": false
}
}
2 changes: 1 addition & 1 deletion examples/CSharpProd/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CSharpProd.Db.LiteDB;
using CSharpProd.DB.LiteDB;
using CSharpProd.Features;
using CSharpProd.Features.CliArgs;
using CSharpProd.Features.CustomSettings;
Expand Down

0 comments on commit 42b33e5

Please sign in to comment.