Skip to content

Commit

Permalink
Added redis example (#599)
Browse files Browse the repository at this point in the history
* Added redis example

* added missed config
  • Loading branch information
OlenaKostash authored and AntyaDev committed Jul 3, 2023
1 parent 7b99218 commit 5ef84a3
Show file tree
Hide file tree
Showing 9 changed files with 2,463 additions and 1 deletion.
10 changes: 9 additions & 1 deletion examples/CSharpProd/CSharpProd.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@
<PackageReference Include="Bogus" Version="34.0.2" />
<PackageReference Include="Dapper.Contrib" Version="2.0.78" />
<PackageReference Include="LiteDB" Version="5.0.15" />
<PackageReference Include="NBomber" Version="5.0.1" />
<PackageReference Include="NBomber" Version="5.1.0" />
<PackageReference Include="NBomber.Data" Version="5.0.0" />
<PackageReference Include="NBomber.Http" Version="5.0.0" />
<PackageReference Include="NBomber.Sinks.InfluxDB" Version="5.0.0" />
<PackageReference Include="MQTTnet" Version="3.1.2" />
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="9.0.0" />
<PackageReference Include="StackExchange.Redis" Version="2.6.116" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.117" />
</ItemGroup>

<ItemGroup>
<None Update="Db\LiteDB\config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="DB\Redis\config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="DB\SQLiteDB\config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand All @@ -46,4 +50,8 @@
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="DB\Redis\" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions examples/CSharpProd/DB/Redis/RedisExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using NBomber.CSharp;

namespace CSharpProd.DB.Redis;

public class RedisExample
{
public void Run()
{
NBomberRunner.RegisterScenarios(
new RedisInitScenario().Create(),
new RedisReadScenario().Create(),
new RedisWriteScenario().Create()
)
.LoadConfig("./DB/Redis/config.json")
.Run();
}
}
37 changes: 37 additions & 0 deletions examples/CSharpProd/DB/Redis/RedisInitScenario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.Extensions.Configuration;
using NBomber.Contracts;
using NBomber.CSharp;
using NBomber.Data;
using StackExchange.Redis;

namespace CSharpProd.DB.Redis;

public class RedisDbConfig
{
public string ConnectionString { get; set; }
public int RecordsCount { get; set; }
public int RecordSize { get; set; }
}

public class RedisInitScenario
{
public ScenarioProps Create()
{
return Scenario.Empty("redis_init")
.WithInit(context =>
{
var dbConfig = context.GlobalCustomSettings.Get<RedisDbConfig>();
var redis = ConnectionMultiplexer.Connect(dbConfig.ConnectionString);
var db = redis.GetDatabase();
var payload = Data.GenerateRandomBytes(dbConfig.RecordSize);
foreach (var i in Enumerable.Range(0, dbConfig.RecordsCount))
{
db.StringSet($"user-{i}", payload);
}
return Task.CompletedTask;
});
}
}
33 changes: 33 additions & 0 deletions examples/CSharpProd/DB/Redis/RedisReadScenario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.Extensions.Configuration;
using NBomber.Contracts;
using NBomber.CSharp;
using StackExchange.Redis;

namespace CSharpProd.DB.Redis;

public class RedisReadScenario
{
private RedisDbConfig _dbConfig;
private ConnectionMultiplexer _redis;
private IDatabase _db;
private readonly Random _random = new();

public ScenarioProps Create()
{
return Scenario
.Create("redis_read", async context =>
{
var randomId = _random.Next(_dbConfig.RecordsCount);
byte[] data = await _db.StringGetAsync($"user-{randomId}");
return Response.Ok(sizeBytes: data.Length);
})
.WithInit(context =>
{
_dbConfig = context.GlobalCustomSettings.Get<RedisDbConfig>();
_redis = ConnectionMultiplexer.Connect(_dbConfig.ConnectionString);
_db = _redis.GetDatabase();
return Task.CompletedTask;
});
}
}
36 changes: 36 additions & 0 deletions examples/CSharpProd/DB/Redis/RedisWriteScenario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.Extensions.Configuration;
using NBomber.Contracts;
using NBomber.CSharp;
using NBomber.Data;
using StackExchange.Redis;

namespace CSharpProd.DB.Redis;

public class RedisWriteScenario
{
private RedisDbConfig _dbConfig;
private ConnectionMultiplexer _redis;
private IDatabase _db;
private readonly Random _random = new();
private byte[] _payload;

public ScenarioProps Create()
{
return Scenario
.Create("redis_write", async context =>
{
var randomId = _random.Next(_dbConfig.RecordsCount);
await _db.StringSetAsync($"user-{randomId}", _payload);
return Response.Ok(sizeBytes: _payload.Length);
})
.WithInit(context =>
{
_dbConfig = context.GlobalCustomSettings.Get<RedisDbConfig>();
_redis = ConnectionMultiplexer.Connect(_dbConfig.ConnectionString);
_db = _redis.GetDatabase();
_payload = Data.GenerateRandomBytes(_dbConfig.RecordSize);
return Task.CompletedTask;
});
}
}
34 changes: 34 additions & 0 deletions examples/CSharpProd/DB/Redis/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"TargetScenarios": ["redis_init", "redis_read", "redis_write"],

"GlobalSettings": {

"ScenariosSettings": [
{
"ScenarioName": "redis_read",

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

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

{
"ScenarioName": "redis_write",

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

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

"GlobalCustomSettings": {
"ConnectionString": "localhost:6379",
"RecordsCount": 100000,
"RecordSize": 2000
}
}
}
18 changes: 18 additions & 0 deletions examples/CSharpProd/DB/Redis/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '3.4'
services:

redis:
image: 'redis:7.0.2'
# command: 'redis-server --save "30 1000" --appendonly yes --requirepass "" '
command: 'redis-server --save "" --appendonly no --requirepass "" '
# command: 'redis-server /usr/local/etc/redis/redis.conf'
volumes:
- './redis-data:/data'
# - './redis-config/redis.conf:/usr/local/etc/redis/redis.conf'
ports:
- '6379:6379'

# to fix redis.conf you should set:
# bind 0.0.0.0
# protected-mode no
# redis.conf should have ASCII encoding
Loading

0 comments on commit 5ef84a3

Please sign in to comment.