Example #1
0
        public static IUnityContainer ResolvePersistence(IUnityContainer container, PersistencePlugin selectedPlugin, string microserviceName,
                                                         string connectionString, // Sql Based persistence
                                                         Func <InMemoryEventStore <TStream>, InMemoryEventStore <TStream> > setupInMemoryPersistence)
        {
            switch (selectedPlugin)
            {
            case PersistencePlugin.InMemory:
                var persitence = new InMemoryEventStore <TStream>(
                    microserviceName,
                    container.Resolve <IUtcTimeProvider>(),
                    container.Resolve <ITextSerializer>(),
                    container.Resolve <IGuidProvider>(),
                    container.Resolve <ILogger>());

                setupInMemoryPersistence.Invoke(persitence);

                container.RegisterInstance <ISubscriptionRepository>(persitence);
                container.RegisterInstance <IEventDao>(persitence);
                container.RegisterInstance <IEventStore <TStream> >(persitence);
                break;

            case PersistencePlugin.SqlServer:
                ResolveForSqlServer(container, microserviceName, connectionString);
                break;

            case PersistencePlugin.SqlServerCe:
                ResolveForSqlServerCe(container, microserviceName, connectionString);
                break;

            default:
                throw new InvalidOperationException("The selected plug in is not available");
            }

            return(container);
        }
        public override void SetUp()
        {
            var moduleName = Guid.NewGuid().ToString();

            var             plugin = new PersistencePlugin();
            PluginInspector pluginInspector;

            plugin.Configure(new PluginConfiguration(moduleName, null, null, null), out pluginInspector);
            PluginInstance = plugin.GetInstance();
        }
Example #3
0
        public static void DropDb(PersistencePlugin plugin)
        {
            if (plugin != PersistencePlugin.SqlServer)
            {
                return;
            }
            Console.WriteLine("Drop db started...");

            SqlClientLite.DropDatabase(FixedConnectionstring);

            Console.WriteLine("Db was droped!");
        }
Example #4
0
        public static void ResetDbs(PersistencePlugin plugin)
        {
            if (plugin == PersistencePlugin.InMemory)
            {
                return;
            }

            using (var context = new EventStoreDbContext(FixedConnectionstring))
            {
                if (context.Database.Exists())
                {
                    Console.WriteLine("Truncating current db...");
                    switch (SelectedPlugin)
                    {
                    case PersistencePlugin.InMemory:
                        break;

                    case PersistencePlugin.SqlServer:
                        context.Database.ExecuteSqlCommand
                            (@"
                                truncate table eventstore.events;
                                truncate table eventstore.inbox;
                                truncate table eventstore.snapshots;
                                truncate table eventstore.subscriptions;"
                            );
                        break;

                    case PersistencePlugin.SqlServerCe:
                        context.Events.ForEach(e => context.Events.Remove(e));
                        context.Inbox.ForEach(e => context.Inbox.Remove(e));
                        context.Subscriptions.ForEach(e => context.Subscriptions.Remove(e));
                        context.Snapshots.ForEach(e => context.Snapshots.Remove(e));
                        break;
                    }

                    Console.WriteLine("Db truncated!");
                    Console.WriteLine("Adding subscriptons");
                    AddSubscriptions(context);

                    return;
                }
            }

            DropDb(plugin);
            CreateDb();
        }
Example #5
0
        private static void PrintWelcomeMessage(PersistencePlugin plugin)
        {
            string pluginSelectedName = "undefined";
            switch (plugin)
            {
                case PersistencePlugin.InMemory:
                    pluginSelectedName = "In-Memory";
                    break;
                case PersistencePlugin.SqlServer:
                    pluginSelectedName = "Sql Server";
                    break;
                case PersistencePlugin.SqlServerCe:
                    pluginSelectedName = "Sql Server Compact Edition";
                    break;
            }

            Console.WriteLine($"Welcome to persistence bench for {pluginSelectedName}");
        }
Example #6
0
        private static void RunBenchmark(PersistencePlugin plugin)
        {
            var user1App = EventSystem.ResolveProcessor <UserManagement>("user1") as UserManagementHandler;
            var user2App = EventSystem.ResolveProcessor <UserManagement>("user2") as UserManagementHandler;

            // SQL SERVER ADO.NET --------------------------------------------
            // 100 througput,   completes in 20 s   7.498 messages      374 m/s without index
            // 100 througput,   completes in 20 s 11.687/9500 messages  584/475 m/s with index
            // 100 througput,   completes in 40 s   12.650  messages    316 m/s
            // 100 througput,   completes in 15:00  70.066  messages    77 m/s
            if (plugin == PersistencePlugin.SqlServer)
            {
                user1App.StressWithWavesOfConcurrentUsers(wavesCount: 10, concurrentUsers: 1000);
                user2App.StressWithWavesOfConcurrentUsers(wavesCount: 10, concurrentUsers: 1000);

                //user1App.StressWithWavesOfConcurrentUsers(wavesCount: 1, concurrentUsers: 1);
            }

            // IN-MEMORY-------------------------------------------------------
            // 100 througput,   completes in 0:20 s 18.225 messgaes    911 m/s without index
            // 100 througput,   completes in 0:20 s 28.053 messgaes    1.402 m/s with index
            // 100 througput,   completes in 0:20 s 29.798 messgaes    1.489 m/s with index
            if (plugin == PersistencePlugin.InMemory)
            {
                user1App.StressWithWavesOfConcurrentUsers(wavesCount: 10, concurrentUsers: 1000);
                user2App.StressWithWavesOfConcurrentUsers(wavesCount: 10, concurrentUsers: 1000);

                //user1App.StressWithWavesOfConcurrentUsers(wavesCount: 1, concurrentUsers: 1);
            }

            if (plugin == PersistencePlugin.InMemory)
            {
                Console.WriteLine("");
                Console.WriteLine("Press enter to see results...");
                Console.ReadLine();
                UnityConfig.StatsMonitor.PrintStats();
            }

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
Example #7
0
        private static void RunBenchmark(PersistencePlugin plugin)
        {
            var user1App = EventSystem.ResolveProcessor<UserManagement>("user1") as UserManagementHandler;
            var user2App = EventSystem.ResolveProcessor<UserManagement>("user2") as UserManagementHandler;

            // SQL SERVER ADO.NET --------------------------------------------
            // 100 througput,   completes in 20 s   7.498 messages      374 m/s without index
            // 100 througput,   completes in 20 s 11.687/9500 messages  584/475 m/s with index
            // 100 througput,   completes in 40 s   12.650  messages    316 m/s
            // 100 througput,   completes in 15:00  70.066  messages    77 m/s
            if (plugin == PersistencePlugin.SqlServer)
            {
                user1App.StressWithWavesOfConcurrentUsers(wavesCount: 10, concurrentUsers: 1000);
                user2App.StressWithWavesOfConcurrentUsers(wavesCount: 10, concurrentUsers: 1000);

                //user1App.StressWithWavesOfConcurrentUsers(wavesCount: 1, concurrentUsers: 1);
            }

            // IN-MEMORY-------------------------------------------------------
            // 100 througput,   completes in 0:20 s 18.225 messgaes    911 m/s without index
            // 100 througput,   completes in 0:20 s 28.053 messgaes    1.402 m/s with index
            // 100 througput,   completes in 0:20 s 29.798 messgaes    1.489 m/s with index
            if (plugin == PersistencePlugin.InMemory)
            {
                user1App.StressWithWavesOfConcurrentUsers(wavesCount: 10, concurrentUsers: 1000);
                user2App.StressWithWavesOfConcurrentUsers(wavesCount: 10, concurrentUsers: 1000);

                //user1App.StressWithWavesOfConcurrentUsers(wavesCount: 1, concurrentUsers: 1);
            }

            if (plugin == PersistencePlugin.InMemory)
            {
                Console.WriteLine("");
                Console.WriteLine("Press enter to see results...");
                Console.ReadLine();
                UnityConfig.StatsMonitor.PrintStats();
            }

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
Example #8
0
        private static void PrintWelcomeMessage(PersistencePlugin plugin)
        {
            string pluginSelectedName = "undefined";

            switch (plugin)
            {
            case PersistencePlugin.InMemory:
                pluginSelectedName = "In-Memory";
                break;

            case PersistencePlugin.SqlServer:
                pluginSelectedName = "Sql Server";
                break;

            case PersistencePlugin.SqlServerCe:
                pluginSelectedName = "Sql Server Compact Edition";
                break;
            }

            Console.WriteLine($"Welcome to persistence bench for {pluginSelectedName}");
        }
Example #9
0
 public static PersistencePlugin SetPlugin(PersistencePlugin plugin)
 {
     selected = plugin;
     return(selected);
 }
Example #10
0
 /// <summary>
 /// Gets the configured Unity container.
 /// </summary>
 public static IUnityContainer GetConfiguredContainer(PersistencePlugin plugin)
 {
     UnityConfig.plugin = plugin;
     return(container.Value);
 }
        public static Func <IMicroservice> CreateEventProcessor(
            string uniqueName,
            IEventStoreConfig eventStoreConfig  = null,
            IPollerConfig pollerConfig          = null,
            PersistencePlugin persistencePlugin = PersistencePlugin.SqlServer,
            bool persistIncomingPayloads        = false,
            bool persistSnapshots = true,
            Func <InMemoryEventStore <TStream>, InMemoryEventStore <TStream> > setupInMemoryPersistence = null,
            Func <string, ITextSerializer, string, bool> consumerFilter = null,
            bool isSubscriptor = true,
            Func <IBus, ILogger, IEventStore <TStream>, THandler> processorFactory = null,
            Func <string, IEventStore, IBus, ILogger, int, TimeSpan, IPollableEventSource> publisherFactory = null)
        {
            return(() =>
            {
                var streamFullName = EnsureStreamCategoryNameIsValid(uniqueName);

                var container = EventSystem.ResolveNewChildContainerAndRegisterInMemorySubscriptions(uniqueName);
                var inMemoryPublisher = container.Resolve <IInMemoryEventPublisher>();

                eventStoreConfig = ConfigResolver.ResolveConfig(eventStoreConfig);

                var connectionString = eventStoreConfig.ConnectionString;

                AuthorizationFactory.SetToken(eventStoreConfig);

                PersistencePluginResolver <TStream> .ResolvePersistence(
                    container, persistencePlugin, streamFullName, connectionString, persistIncomingPayloads, persistSnapshots, setupInMemoryPersistence, consumerFilter);

                var log = container.Resolve <ILogger>();

                var eventStore = container.Resolve <IEventStore <TStream> >();

                var bus = new Bus();
                container.RegisterInstance <IBus>(bus);

                IPollableEventSource publisher;
                if (publisherFactory == null)
                {
                    publisher = new Publisher(streamFullName, eventStore, bus, log, eventStoreConfig.PushMaxCount, TimeSpan.FromMilliseconds(eventStoreConfig.LongPollingTimeout));
                    container.RegisterInstance <IPollableEventSource>(publisher);
                }
                else
                {
                    publisher = publisherFactory.Invoke(streamFullName, eventStore, bus, log, eventStoreConfig.PushMaxCount, TimeSpan.FromMilliseconds(eventStoreConfig.LongPollingTimeout));
                }

                var fsm = new MicroserviceHost(streamFullName, bus, log, isSubscriptor);
                container.RegisterInstance <IMicroservice>(fsm);

                // Processor factory
                THandler processor;
                if (processorFactory == null)
                {
                    var constructor = typeof(THandler).GetConstructor(new[] { typeof(IBus), typeof(ILogger), typeof(IEventStore <TStream>) });
                    Ensure.CastIsValid(constructor, "Type TProcessor must have a valid constructor with the following signature: .ctor(IBus, ILogger, IEventStore<T>)");
                    processor = (THandler)constructor.Invoke(new object[] { bus, log, eventStore });
                }
                else
                {
                    processor = processorFactory.Invoke(bus, log, eventStore);
                }
                container.RegisterInstance <IProcessor <TStream> >(processor);

                // For nodes that polls events from subscribed sources
                if (isSubscriptor)
                {
                    pollerConfig = ConfigResolver.ResolveConfig(pollerConfig);
                    var receiver = new LongPoller(bus, log, TimeSpan.FromMilliseconds(pollerConfig.Timeout), streamFullName, inMemoryPublisher);
                    var poller = new Poller(bus, log, inMemoryPublisher, container.Resolve <ISubscriptionRepository>(), receiver, container.Resolve <ITextSerializer>(), pollerConfig.BufferQueueMaxCount, pollerConfig.EventsToFlushMaxCount);
                    container.RegisterInstance <IMonitoredSubscriber>(poller);

                    //var heartbeatEmitter = new HeartbeatEmitter(fsm, log, poller);
                    var heartbeatEmitter = new HeartbeatEmitter(streamFullName);
                    container.RegisterInstance <HeartbeatEmitter>(heartbeatEmitter);
                }

                inMemoryPublisher.Register(publisher);
                return fsm;
            });
        }
        public static IMicroservice CreateEventProcessorWithApp <TApp>(
            string uniqueName,
            string appUniqueName,
            IUnityContainer container,
            IEventStoreConfig eventStoreConfig,
            PersistencePlugin selectedPlugin = PersistencePlugin.SqlServer,
            Func <InMemoryEventStore <TStream>, InMemoryEventStore <TStream> > setupInMemoryPersistence = null,
            bool isSubscriptor = true,
            Func <IGuidProvider, ILogger, string, int, TApp> appFactory            = null,
            Func <IBus, ILogger, IEventStore <TStream>, THandler> processorFactory = null,
            Func <string, IBus, ILogger, IEventDao, int, TimeSpan, IPollableEventSource> publisherFactory = null)
            where TApp : ApplicationService
        {
            var inMemoryPublisher = container.Resolve <IInMemoryEventPublisher>();

            var streamFullName = EnsureStreamCategoryNameIsValid(uniqueName);
            var appFullName    = EnsureStreamCategoryNameIsValid(appUniqueName);

            var connectionString = eventStoreConfig.ConnectionString;

            AuthorizationFactory.SetToken(eventStoreConfig);

            PersistencePluginResolver <TStream> .ResolvePersistence(
                container, selectedPlugin, streamFullName, connectionString, setupInMemoryPersistence);

            var log = container.Resolve <ILogger>();

            var eventStore = container.Resolve <IEventStore <TStream> >();
            var eventDao   = container.Resolve <IEventDao>();

            var bus = new Bus();

            container.RegisterInstance <IBus>(bus);

            IPollableEventSource publisher;

            if (publisherFactory == null)
            {
                publisher = new Publisher(streamFullName, bus, log, eventDao, eventStoreConfig.PushMaxCount, TimeSpan.FromMilliseconds(eventStoreConfig.LongPollingTimeout));
                container.RegisterInstance <IPollableEventSource>(publisher);
            }
            else
            {
                publisher = publisherFactory.Invoke(streamFullName, bus, log, eventDao, eventStoreConfig.PushMaxCount, TimeSpan.FromMilliseconds(eventStoreConfig.LongPollingTimeout));
            }

            var fsm = new MicroserviceHost(streamFullName, bus, log, isSubscriptor);

            container.RegisterInstance <IMicroservice>(fsm);

            if (processorFactory == null)
            {
                var constructor = typeof(THandler).GetConstructor(new[] { typeof(IBus), typeof(ILogger), typeof(IEventStore <TStream>) });
                Ensure.CastIsValid(constructor, "Type TProcessor must have a valid constructor with the following signature: .ctor(IBus, ILogger, IEventStore<T>)");
                var processor = (THandler)constructor.Invoke(new object[] { bus, log, eventStore });
            }
            else
            {
                var processor = processorFactory.Invoke(bus, log, eventStore);
            }

            // For nodes that polls events from subscribed sources

            if (isSubscriptor)
            {
                var pollerConfig = PollerConfig.GetConfig();
                var pollerPool   = new LongPoller(bus, log, TimeSpan.FromMilliseconds(pollerConfig.Timeout), streamFullName, inMemoryPublisher);
                var poller       = new Poller(bus, log, container.Resolve <ISubscriptionRepository>(), pollerPool, container.Resolve <ITextSerializer>(), pollerConfig.BufferQueueMaxCount, pollerConfig.EventsToFlushMaxCount);
                container.RegisterInstance <IMonitoredSubscriber>(poller);

                var heartbeatEmitter = new HeartbeatEmitter(fsm, log, poller);
                container.RegisterInstance <HeartbeatEmitter>(heartbeatEmitter);
            }

            var guid = container.Resolve <IGuidProvider>();

            if (appFactory == null)
            {
                var constructor = typeof(TApp).GetConstructor(new[] { typeof(IGuidProvider), typeof(ILogger), typeof(string), typeof(int) });
                Ensure.CastIsValid(constructor, "Type TApp must have a valid constructor with the following signature: .ctor(IGuidProvider, ILogger, string, int)");
                container.RegisterInstance <TApp>((TApp)constructor.Invoke(new object[] { guid, log, appFullName, eventStoreConfig.PushMaxCount }));
            }
            else
            {
                container.RegisterInstance <TApp>(appFactory.Invoke(guid, log, appFullName, eventStoreConfig.PushMaxCount));
            }

            inMemoryPublisher.Register(publisher);
            inMemoryPublisher.Register(container.Resolve <TApp>());
            return(fsm);
        }
Example #13
0
 /// <summary>
 /// Gets the configured Unity container.
 /// </summary>
 public static IUnityContainer InitializeMainContainer(PersistencePlugin plugin)
 {
     UnityConfig.plugin = plugin;
     return(container.Value);
 }