-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added redis example * added missed config
- Loading branch information
1 parent
7b99218
commit 5ef84a3
Showing
9 changed files
with
2,463 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,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(); | ||
} | ||
} |
This file contains 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 @@ | ||
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; | ||
}); | ||
} | ||
} |
This file contains 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,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; | ||
}); | ||
} | ||
} |
This file contains 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,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; | ||
}); | ||
} | ||
} |
This file contains 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,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 | ||
} | ||
} | ||
} |
This file contains 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,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 |
Oops, something went wrong.