-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
74b5a35
commit d564800
Showing
4 changed files
with
106 additions
and
3 deletions.
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
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
91 changes: 91 additions & 0 deletions
91
src/cqrs/cqrs-sqlserver/CqrsSqlServer.Shared/FakeDataGenerator.cs
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,91 @@ | ||
using Akka.Actor; | ||
using Akka.Event; | ||
using Akka.Hosting; | ||
using Bogus; | ||
using CqrsSqlServer.Shared.Commands; | ||
using CqrsSqlServer.Shared.Sharding; | ||
|
||
namespace CqrsSqlServer.Shared; | ||
|
||
public class FakeDataGenerator | ||
{ | ||
private readonly Faker<CreateProduct> _productGenerator; | ||
|
||
public FakeDataGenerator() | ||
{ | ||
Randomizer.Seed = new Random(123456); | ||
_productGenerator = new Faker<CreateProduct>() | ||
.CustomInstantiator(f => | ||
{ | ||
var count = f.Random.Int(0, 3); | ||
return new CreateProduct( | ||
f.Random.Guid().ToString(), | ||
f.Commerce.ProductName(), | ||
f.Finance.Amount(min: 1, max: 1000), | ||
f.Random.Int(1, 1000), | ||
count == 0 ? Array.Empty<string>() : f.Commerce.Categories(count) | ||
); | ||
}); | ||
} | ||
|
||
public void Generate(ActorSystem system, IActorRegistry registry, int count) | ||
{ | ||
var log = Logging.GetLogger(system, nameof(FakeDataGenerator)); | ||
var shardRegion = registry.Get<ProductMarker>(); | ||
|
||
GenerateAsync(count, shardRegion, log) | ||
.ContinueWith(t => | ||
{ | ||
if (!t.IsCompletedSuccessfully) | ||
{ | ||
log.Error(t.Exception, "Failed to generate fake data"); | ||
} | ||
}) | ||
.ConfigureAwait(false); | ||
} | ||
|
||
private async Task GenerateAsync(int count, IActorRef shardRegion, ILoggingAdapter log) | ||
{ | ||
log.Info($"Generating {count} fake data"); | ||
var items = new List<CreateProduct>(); | ||
var total = 0; | ||
foreach (var _ in Enumerable.Range(0, count)) | ||
{ | ||
var item = _productGenerator.Generate(); | ||
items.Add(item); | ||
var response = await shardRegion.Ask<ProductCommandResponse>(item, TimeSpan.FromSeconds(3)); | ||
if (!response.Success) | ||
{ | ||
log.Error(response.Message); | ||
} | ||
else | ||
{ | ||
total++; | ||
} | ||
} | ||
|
||
var purchaseCount = count * 5; | ||
var rnd = Randomizer.Seed; | ||
var purchaseTotal = 0; | ||
var twoMinutes = TimeSpan.FromMinutes(2); | ||
var rollBackMinutes = twoMinutes * (purchaseCount + 1); | ||
var now = DateTime.UtcNow - rollBackMinutes; | ||
foreach (var _ in Enumerable.Range(0, purchaseCount)) | ||
{ | ||
var item = items[rnd.Next(0, items.Count)]; | ||
var newOrder = new ProductOrder(Guid.NewGuid().ToString(), item.ProductId, rnd.Next(1, 100), now); | ||
now += twoMinutes; | ||
var createOrderCommand = new PurchaseProduct(newOrder); | ||
var response = await shardRegion.Ask<ProductCommandResponse>(createOrderCommand, TimeSpan.FromSeconds(3)); | ||
if (!response.Success) | ||
{ | ||
log.Error(response.Message); | ||
} | ||
else | ||
{ | ||
purchaseTotal++; | ||
} | ||
} | ||
log.Info($"Generated {total} fake data and {purchaseTotal} fake purchases"); | ||
} | ||
} |