/Framework/Lokad.Cqrs.Azure.Tests/Performance_tests_for_reaction.cs

https://github.com/xpando/lokad-cqrs · C# · 163 lines · 133 code · 27 blank · 3 comment · 1 complexity · 3d44ef37d768b954aad4a048186131cb MD5 · raw file

  1. #region (c) 2010-2011 Lokad - CQRS for Windows Azure - New BSD License
  2. // Copyright (c) Lokad 2010-2011, http://www.lokad.com
  3. // This code is released as Open Source under the terms of the New BSD Licence
  4. #endregion
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Runtime.Serialization;
  10. using System.Threading;
  11. using Autofac;
  12. using Lokad.Cqrs.Build.Engine;
  13. using Lokad.Cqrs.Core.Dispatch.Events;
  14. using NUnit.Framework;
  15. namespace Lokad.Cqrs
  16. {
  17. [TestFixture, Explicit]
  18. public sealed class Performance_tests_for_reaction
  19. {
  20. [Test]
  21. public void Memory_partition_with_polymorphic()
  22. {
  23. TestConfiguration(c => c.Memory(m =>
  24. {
  25. m.AddMemorySender("test-accelerated");
  26. m.AddMemoryProcess("test-accelerated");
  27. }));
  28. }
  29. [Test]
  30. public void Memory_partition_with_lambda()
  31. {
  32. TestConfiguration(c =>
  33. c.Memory(m =>
  34. {
  35. m.AddMemorySender("test-accelerated");
  36. m.AddMemoryProcess("test-accelerated", x => x.DispatcherIsLambda(Factory));
  37. }));
  38. }
  39. [Test]
  40. public void Azure_partition_with_lambda()
  41. {
  42. var config = AzureStorage.CreateConfigurationForDev();
  43. WipeAzureAccount.Fast(s => s.StartsWith("performance"), config);
  44. TestConfiguration(c => c.Azure(m =>
  45. {
  46. m.AddAzureSender(config, "performance");
  47. m.AddAzureProcess(config, "performance", x => x.DispatcherIsLambda(Factory));
  48. }));
  49. }
  50. [Test]
  51. public void Azure_partition_with_polymorphic()
  52. {
  53. var config = AzureStorage.CreateConfigurationForDev();
  54. WipeAzureAccount.Fast(s => s.StartsWith("performance"), config);
  55. TestConfiguration(c => c.Azure(m =>
  56. {
  57. m.AddAzureSender(config, "performance");
  58. m.AddAzureProcess(config, "performance");
  59. }));
  60. }
  61. [Test]
  62. public void File_partition_with_lambda()
  63. {
  64. var config = FileStorage.CreateConfig("throughput-tests");
  65. config.Wipe();
  66. TestConfiguration(c => c.File(m =>
  67. {
  68. m.AddFileSender(config, "test-accelerated");
  69. m.AddFileProcess(config, "test-accelerated", x => x.DispatcherIsLambda(Factory));
  70. }));
  71. }
  72. [Test]
  73. public void File_partition_with_polymorphic()
  74. {
  75. var config = FileStorage.CreateConfig("throughput-tests");
  76. config.Wipe();
  77. TestConfiguration(c => c.File(m =>
  78. {
  79. m.AddFileSender(config, "test-accelerated");
  80. m.AddFileProcess(config, "test-accelerated");
  81. }));
  82. }
  83. static Action<ImmutableEnvelope> Factory(IComponentContext componentContext)
  84. {
  85. var sender = componentContext.Resolve<IMessageSender>();
  86. return envelope => sender.SendOne(envelope.Items[0].Content);
  87. }
  88. // ReSharper disable InconsistentNaming
  89. [DataContract]
  90. public sealed class UsualMessage : Define.Command
  91. {
  92. [DataMember(Order = 1)]
  93. public string Word { get; set; }
  94. }
  95. public sealed class Handler : Define.Handle<UsualMessage>
  96. {
  97. readonly IMessageSender _sender;
  98. public Handler(IMessageSender sender)
  99. {
  100. _sender = sender;
  101. }
  102. public void Consume(UsualMessage message)
  103. {
  104. _sender.SendOne(message);
  105. }
  106. }
  107. static void TestConfiguration(Action<CqrsEngineBuilder> build)
  108. {
  109. var builder = new CqrsEngineBuilder();
  110. builder.UseProtoBufSerialization();
  111. build(builder);
  112. var subj = new Subject<ISystemEvent>();
  113. builder.Advanced.Observers.Clear();
  114. builder.Advanced.RegisterObserver(subj);
  115. int count = 0;
  116. subj
  117. .OfType<EnvelopeAcked>()
  118. .Subscribe(acked => count += 1);
  119. var watch = new Stopwatch();
  120. using (var token = new CancellationTokenSource())
  121. using (var engine = builder.Build())
  122. {
  123. engine.Start(token.Token);
  124. engine
  125. .Resolve<IMessageSender>()
  126. .SendOne(new UsualMessage());
  127. watch.Start();
  128. token.Token.WaitHandle.WaitOne(5000);
  129. }
  130. watch.Stop();
  131. var messagesPerSecond = count / watch.Elapsed.TotalSeconds;
  132. var round = Math.Round(messagesPerSecond, 1);
  133. Console.WriteLine("{0} messages per second", round);
  134. }
  135. }
  136. }