Skip to content

Commit 42b33e5

Browse files
committed
improved InitDBScenario and added checkpointDB for LiteDb
1 parent 3bfd820 commit 42b33e5

File tree

7 files changed

+66
-49
lines changed

7 files changed

+66
-49
lines changed

examples/CSharpProd/Db/LiteDB/InitDBScenario.cs examples/CSharpProd/DB/LiteDB/InitDBScenario.cs

+9-14
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@
33
using Microsoft.Extensions.Configuration;
44
using NBomber.Contracts;
55
using NBomber.CSharp;
6-
using System;
7-
using System.Collections.Generic;
8-
using System.Linq;
9-
using System.Text;
10-
using System.Threading.Tasks;
116

12-
namespace MyLoadTest
7+
namespace CSharpProd.DB.LiteDB
138
{
149
public class LiteDBCustomSettings
1510
{
@@ -19,10 +14,9 @@ public class LiteDBCustomSettings
1914

2015
internal class InitDBScenario
2116
{
22-
private LiteDatabase _db = null;
23-
17+
public LiteDatabase Db { get; private set; }
2418
public ILiteCollection<User> Collection { get; private set; }
25-
public int RecordBiteSize { get; private set; }
19+
public int RecordSizeBytes { get; private set; }
2620
public LiteDBCustomSettings DBSettings { get; private set;}
2721

2822
public ScenarioProps Create()
@@ -33,12 +27,12 @@ public ScenarioProps Create()
3327
{
3428
DBSettings = context.CustomSettings.Get<LiteDBCustomSettings>();
3529

36-
_db = new LiteDatabase("UsersRegister.db");
30+
Db = new LiteDatabase("UsersRegister.db");
3731

38-
Collection = _db.GetCollection<User>("users");
32+
Collection = Db.GetCollection<User>("users");
3933
var collectionCount = Collection.Count();
4034

41-
if (DBSettings.UserCount >= collectionCount)
35+
if (DBSettings.UserCount > collectionCount)
4236
{
4337
var lastUser = Collection.Query().OrderByDescending(c => c._id).FirstOrDefault();
4438
var maxId = 0;
@@ -95,13 +89,14 @@ public ScenarioProps Create()
9589
Collection.Insert(listOfUser);
9690
}
9791

98-
RecordBiteSize = CalculateRecordSize(Collection);
92+
RecordSizeBytes = CalculateRecordSize(Collection);
9993

10094
return Task.CompletedTask;
10195
})
10296
.WithClean(context =>
10397
{
104-
_db.Dispose();
98+
Db.Checkpoint();
99+
Db.Dispose();
105100
return Task.CompletedTask;
106101
});
107102
}

examples/CSharpProd/Db/LiteDB/LiteDBExample.cs examples/CSharpProd/DB/LiteDB/LiteDBExample.cs

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
using Bogus;
22
using LiteDB;
3-
using MyLoadTest;
43
using NBomber.CSharp;
5-
using System;
6-
using System.Collections.Generic;
7-
using System.Linq;
8-
using System.Text;
9-
using System.Threading.Tasks;
104

11-
namespace CSharpProd.Db.LiteDB
5+
namespace CSharpProd.DB.LiteDB
126
{
137
public class LiteDBExample
148
{
@@ -22,7 +16,7 @@ public void Run()
2216
var randomId = random.Next(1, initDBScn.DBSettings.UserCount);
2317
var randomUser = initDBScn.Collection.FindById(new BsonValue(randomId));
2418

25-
return Response.Ok(sizeBytes: initDBScn.RecordBiteSize);
19+
return Response.Ok(sizeBytes: initDBScn.RecordSizeBytes);
2620
});
2721

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

35-
return Response.Ok(sizeBytes: initDBScn.RecordBiteSize);
29+
return Response.Ok(sizeBytes: initDBScn.RecordSizeBytes);
3630
});
3731

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

4640
initDBScn.Collection.Upsert(randomUser);
4741

48-
return Response.Ok(sizeBytes: initDBScn.RecordBiteSize * 2);
42+
return Response.Ok(sizeBytes: initDBScn.RecordSizeBytes * 2);
4943
});
5044

5145
var faker = new Faker();
@@ -57,11 +51,18 @@ public void Run()
5751
.Limit(10)
5852
.ToList();
5953

60-
return Response.Ok(sizeBytes: initDBScn.RecordBiteSize * listOfRandomUser.Count);
54+
return Response.Ok(sizeBytes: initDBScn.RecordSizeBytes * listOfRandomUser.Count);
55+
});
56+
57+
var checkpointDB = Scenario.Create("checkpointDB", async context =>
58+
{
59+
await Task.Delay(5_000);
60+
initDBScn.Db.Checkpoint();
61+
return Response.Ok();
6162
});
6263

6364
NBomberRunner
64-
.RegisterScenarios(initDBScn.Create(), getById, update, readModifyWrite, conditionalQuery)
65+
.RegisterScenarios(initDBScn.Create(), checkpointDB, getById, update, readModifyWrite, conditionalQuery)
6566
.LoadConfig("Db/LiteDB/config.json")
6667
.Run();
6768
}

examples/CSharpProd/Db/LiteDB/User.cs examples/CSharpProd/DB/LiteDB/User.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
namespace MyLoadTest
1+
namespace CSharpProd.DB.LiteDB
32
{
43
public enum Gender
54
{

examples/CSharpProd/Db/LiteDB/config.json examples/CSharpProd/DB/LiteDB/config.json

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"TargetScenarios": [ "initDB", "get_by_id", "update", "read_modify_write", "conditional_query" ],
2+
"TargetScenarios": [ "initDB", "checkpointDB", "get_by_id", "read_modify_write" ],
33

44
"GlobalSettings": {
55

@@ -8,36 +8,43 @@
88
"ScenarioName": "initDB",
99

1010
"CustomSettings": {
11-
"UserCount": 10000,
12-
"InsertBulcSize": 500
11+
"UserCount": 500000,
12+
"InsertBulcSize": 2000
1313
}
1414
},
15+
{
16+
"ScenarioName": "checkpointDB",
17+
18+
"LoadSimulationsSettings": [
19+
{ "KeepConstant": [ 1, "00:03:00" ] }
20+
]
21+
},
1522
{
1623
"ScenarioName": "get_by_id",
1724

1825
"LoadSimulationsSettings": [
19-
{ "KeepConstant": [ 10, "00:00:30" ] }
26+
{ "KeepConstant": [ 10, "00:03:00" ] }
2027
]
2128
},
2229
{
2330
"ScenarioName": "update",
2431

2532
"LoadSimulationsSettings": [
26-
{ "KeepConstant": [ 10, "00:00:30" ] }
33+
{ "KeepConstant": [ 1, "00:03:00" ] }
2734
]
2835
},
2936
{
3037
"ScenarioName": "read_modify_write",
3138

3239
"LoadSimulationsSettings": [
33-
{ "KeepConstant": [ 10, "00:00:30" ] }
40+
{ "KeepConstant": [ 1, "00:03:00" ] }
3441
]
3542
},
3643
{
3744
"ScenarioName": "conditional_query",
3845

3946
"LoadSimulationsSettings": [
40-
{ "KeepConstant": [ 10, "00:00:30" ] }
47+
{ "KeepConstant": [ 1, "00:03:00" ] }
4148
]
4249
}
4350
]

examples/CSharpProd/Features/CustomSettings/CustomSettingsExample.cs

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using Microsoft.Extensions.Configuration;
22
using NBomber.Contracts;
3+
using NBomber.Contracts.Stats;
34
using NBomber.CSharp;
45

56
namespace CSharpProd.Features.CustomSettings;
67

78
public class CustomScenarioSettings
89
{
9-
public int TestField { get; set; }
10-
public int PauseMs { get; set; }
10+
public int MyTestField { get; set; }
11+
public int MyPauseMs { get; set; }
1112
}
1213

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

2122
initContext.Logger.Information(
22-
"test init received CustomSettings.TestField '{TestField}'",
23-
_customSettings.TestField
23+
"test init received CustomSettings.MyTestField '{0}'",
24+
_customSettings.MyTestField
2425
);
2526

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

3536
var step = await Step.Run("step", context, async () =>
3637
{
3738
await Task.Delay(1_000);
38-
context.Logger.Debug("step received CustomSettings.TestField '{0}'", _customSettings.TestField);
39+
context.Logger.Debug("step received CustomSettings.MyTestField '{0}'", _customSettings.MyTestField);
3940
return Response.Ok();
4041
});
4142

4243
return Response.Ok();
4344
})
4445
.WithInit(Init)
45-
.WithoutWarmUp();
46+
.WithLoadSimulations(Simulation.Inject(rate: 50, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromMinutes(1)))
47+
.WithWarmUpDuration(TimeSpan.FromSeconds(10))
48+
.WithMaxFailCount(1_000);
4649

4750
NBomberRunner
4851
.RegisterScenarios(scenario)
4952
.LoadConfig("./Features/CustomSettings/config.json")
53+
.WithTestSuite("my test suite")
54+
.WithTestName("my test name")
55+
.WithTargetScenarios("my_scenario")
56+
.WithReportFileName("my_report")
57+
.WithReportFolder("report_folder")
58+
.WithReportFormats(ReportFormat.Txt, ReportFormat.Html)
59+
.WithReportingInterval(TimeSpan.FromSeconds(10))
60+
.EnableHintsAnalyzer(true)
5061
.Run();
5162
}
5263
}

examples/CSharpProd/Features/CustomSettings/config.json

+10-6
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@
1818
],
1919

2020
"CustomSettings": {
21-
"TestField": 1,
22-
"PauseMs": 200
23-
}
21+
"MyTestField": 1,
22+
"MyPauseMs": 200
23+
},
24+
25+
"MaxFailCount": 500
2426
}
2527
],
2628

27-
"ReportFileName": "my_report_name",
28-
"ReportFolder": "./reports",
29-
"ReportFormats": [ "Html", "Md", "Txt", "Csv" ]
29+
"ReportFileName": "my_custom_report_name",
30+
"ReportFolder": "./my_reports",
31+
"ReportFormats": ["Html", "Txt"],
32+
"ReportingInterval": "00:00:30",
33+
"EnableHintsAnalyzer": false
3034
}
3135
}

examples/CSharpProd/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using CSharpProd.Db.LiteDB;
1+
using CSharpProd.DB.LiteDB;
22
using CSharpProd.Features;
33
using CSharpProd.Features.CliArgs;
44
using CSharpProd.Features.CustomSettings;

0 commit comments

Comments
 (0)