mirror of https://github.com/Squidex/squidex.git
40 changed files with 405 additions and 215 deletions
@ -0,0 +1,76 @@ |
|||
// ==========================================================================
|
|||
// AppendToEventStoreParallel.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Benchmarks.Utils; |
|||
using MongoDB.Driver; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.CQRS.Events; |
|||
using Squidex.Infrastructure.MongoDb.EventStore; |
|||
|
|||
namespace Benchmarks.Tests |
|||
{ |
|||
public sealed class AppendToEventStoreParallel : IBenchmark |
|||
{ |
|||
private IMongoClient mongoClient; |
|||
private IMongoDatabase mongoDatabase; |
|||
|
|||
public string Id |
|||
{ |
|||
get { return "appendToEventStoreParallel"; } |
|||
} |
|||
|
|||
public string Name |
|||
{ |
|||
get { return "Append Events to EventStore Parallel"; } |
|||
} |
|||
|
|||
public void Initialize() |
|||
{ |
|||
mongoClient = new MongoClient("mongodb://localhost"); |
|||
} |
|||
|
|||
public void RunInitialize() |
|||
{ |
|||
mongoDatabase = mongoClient.GetDatabase(Guid.NewGuid().ToString()); |
|||
} |
|||
|
|||
public long Run() |
|||
{ |
|||
const long numCommits = 200; |
|||
const long eventStreams = 10; |
|||
|
|||
var eventStore = new MongoEventStore(mongoDatabase, new DefaultEventNotifier(new InMemoryPubSub())); |
|||
|
|||
Parallel.For(0, eventStreams, streamId => |
|||
{ |
|||
var eventOffset = -1; |
|||
var streamName = streamId.ToString(); |
|||
|
|||
for (var commitId = 0; commitId < numCommits; commitId++) |
|||
{ |
|||
eventStore.AppendEventsAsync(Guid.NewGuid(), streamName, eventOffset, new[] { Helper.CreateEventData() }).Wait(); |
|||
|
|||
eventOffset++; |
|||
} |
|||
}); |
|||
|
|||
return numCommits * eventStreams; |
|||
} |
|||
|
|||
public void RunCleanup() |
|||
{ |
|||
mongoClient.DropDatabase(mongoDatabase.DatabaseNamespace.DatabaseName); |
|||
} |
|||
|
|||
public void Cleanup() |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
// ==========================================================================
|
|||
// Helper.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Squidex.Infrastructure.CQRS.Events; |
|||
|
|||
namespace Benchmarks.Utils |
|||
{ |
|||
public static class Helper |
|||
{ |
|||
public static EventData CreateEventData() |
|||
{ |
|||
return new EventData { EventId = Guid.NewGuid(), Metadata = "EventMetdata", Payload = "EventPayload", Type = "MyEvent" }; |
|||
|
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
<PropertyGroup> |
|||
<OutputType>Exe</OutputType> |
|||
<TargetFramework>netcoreapp1.1</TargetFramework> |
|||
</PropertyGroup> |
|||
<ItemGroup> |
|||
<PackageReference Include="MongoDB.Driver" Version="2.4.4" /> |
|||
</ItemGroup> |
|||
</Project> |
|||
@ -0,0 +1,94 @@ |
|||
// ==========================================================================
|
|||
// Program.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using MongoDB.Bson; |
|||
using MongoDB.Driver; |
|||
|
|||
namespace Migrate_01 |
|||
{ |
|||
public class Program |
|||
{ |
|||
public static void Main(string[] args) |
|||
{ |
|||
Console.WriteLine("Migrate EventStore"); |
|||
|
|||
var mongoClient = new MongoClient(GetMongoConnectionValue()); |
|||
var mongoDatabase = mongoClient.GetDatabase(GetMongoDatabaseValue()); |
|||
|
|||
var collection = mongoDatabase.GetCollection<BsonDocument>("Events"); |
|||
|
|||
Console.Write("Migrate Indices....."); |
|||
|
|||
collection.Indexes.DropAll(); |
|||
|
|||
Console.WriteLine("DONE"); |
|||
|
|||
var query = |
|||
collection.Find(new BsonDocument()) |
|||
.Project<BsonDocument>( |
|||
Builders<BsonDocument>.Projection.Include(Field("EventsOffset"))) |
|||
.ToList(); |
|||
|
|||
Console.Write("Migrate Documents..."); |
|||
|
|||
foreach (var eventCommit in query) |
|||
{ |
|||
var eventsOffset = (int)eventCommit["EventsOffset"].AsInt64; |
|||
|
|||
var ts = new BsonTimestamp(eventsOffset + 10, 1); |
|||
|
|||
collection.UpdateOne( |
|||
Builders<BsonDocument>.Filter |
|||
.Eq(Field<string>("_id"), eventCommit["_id"].AsString), |
|||
Builders<BsonDocument>.Update |
|||
.Set(Field<BsonTimestamp>("Timestamp"), ts).Unset(Field("EventsOffset"))); |
|||
} |
|||
|
|||
Console.WriteLine("DONE"); |
|||
} |
|||
|
|||
private static StringFieldDefinition<BsonDocument, T> Field<T>(string fieldName) |
|||
{ |
|||
return new StringFieldDefinition<BsonDocument, T>(fieldName); |
|||
} |
|||
|
|||
private static StringFieldDefinition<BsonDocument> Field(string fieldName) |
|||
{ |
|||
return new StringFieldDefinition<BsonDocument>(fieldName); |
|||
} |
|||
|
|||
private static string GetMongoConnectionValue() |
|||
{ |
|||
Console.Write("Mongo Connection (ENTER for 'mongodb://localhost'): "); |
|||
|
|||
var mongoConnection = Console.ReadLine(); |
|||
|
|||
if (string.IsNullOrWhiteSpace(mongoConnection)) |
|||
{ |
|||
mongoConnection = "mongodb://localhost"; |
|||
} |
|||
|
|||
return mongoConnection; |
|||
} |
|||
|
|||
private static string GetMongoDatabaseValue() |
|||
{ |
|||
Console.Write("Mongo Database (ENTER for 'Squidex'): "); |
|||
|
|||
var mongoDatabase = Console.ReadLine(); |
|||
|
|||
if (string.IsNullOrWhiteSpace(mongoDatabase)) |
|||
{ |
|||
mongoDatabase = "Squidex"; |
|||
} |
|||
|
|||
return mongoDatabase; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue