From ee51584e0a3a15df8a02b0ab3277c34da75edfa3 Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Sat, 6 Apr 2019 22:38:51 +0300 Subject: [PATCH] Revise simulation code --- .../Pages/Index.cshtml | 2 +- .../Pages/SimulationArea.css | 5 +- .../Pages/SimulationArea.min.css | 2 +- .../Pages/SimulationArea.scss | 3 + .../ClientSimulationModule.cs | 9 ++- .../ClientSimulationOptions.cs | 5 +- .../Clients/DefaultClientFactory.cs | 13 ++++ .../Clients/IClientFactory.cs | 13 +--- .../Volo/ClientSimulation/GlobalOptions.cs | 17 ----- .../ClientSimulation/ScenarioConfiguration.cs | 19 ++++++ .../Scenarios/DemoScenario.cs | 13 ++++ .../ClientSimulation/Scenarios/IScenario.cs | 17 +++++ .../Scenarios/IScenarioStep.cs | 9 +++ .../ClientSimulation/Scenarios/Scenario.cs | 64 +------------------ .../Scenarios/ScenarioStep.cs | 16 +++++ .../Scenarios/SleepScenarioStep.cs | 24 +++++++ .../Volo/ClientSimulation/Simulation.cs | 41 ++++++------ 17 files changed, 155 insertions(+), 117 deletions(-) create mode 100644 utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Clients/DefaultClientFactory.cs delete mode 100644 utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/GlobalOptions.cs create mode 100644 utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/ScenarioConfiguration.cs create mode 100644 utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/DemoScenario.cs create mode 100644 utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/IScenario.cs create mode 100644 utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/IScenarioStep.cs create mode 100644 utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/ScenarioStep.cs create mode 100644 utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/SleepScenarioStep.cs diff --git a/utils/client-simulation/src/Volo.ClientSimulation.Web/Pages/Index.cshtml b/utils/client-simulation/src/Volo.ClientSimulation.Web/Pages/Index.cshtml index 48292e6dd0..dc4ef3334a 100644 --- a/utils/client-simulation/src/Volo.ClientSimulation.Web/Pages/Index.cshtml +++ b/utils/client-simulation/src/Volo.ClientSimulation.Web/Pages/Index.cshtml @@ -2,7 +2,7 @@ @model Volo.ClientSimulation.Web.Pages.IndexModel @section styles { - + } @section scripts { diff --git a/utils/client-simulation/src/Volo.ClientSimulation.Web/Pages/SimulationArea.css b/utils/client-simulation/src/Volo.ClientSimulation.Web/Pages/SimulationArea.css index 30cd9cb44e..417b6990d5 100644 --- a/utils/client-simulation/src/Volo.ClientSimulation.Web/Pages/SimulationArea.css +++ b/utils/client-simulation/src/Volo.ClientSimulation.Web/Pages/SimulationArea.css @@ -2,4 +2,7 @@ border: 1px solid #008000; background-color: #d6ffce; margin: 3px; - padding: 5px; } + padding: 5px; + min-width: 250px; + overflow: hidden; + display: inline-block; } diff --git a/utils/client-simulation/src/Volo.ClientSimulation.Web/Pages/SimulationArea.min.css b/utils/client-simulation/src/Volo.ClientSimulation.Web/Pages/SimulationArea.min.css index b5120f66b5..831d426b05 100644 --- a/utils/client-simulation/src/Volo.ClientSimulation.Web/Pages/SimulationArea.min.css +++ b/utils/client-simulation/src/Volo.ClientSimulation.Web/Pages/SimulationArea.min.css @@ -1 +1 @@ -.simulation-client{border:1px solid #008000;background-color:#d6ffce;margin:3px;padding:5px;} \ No newline at end of file +.simulation-client{border:1px solid #008000;background-color:#d6ffce;margin:3px;padding:5px;min-width:250px;overflow:hidden;display:inline-block;} \ No newline at end of file diff --git a/utils/client-simulation/src/Volo.ClientSimulation.Web/Pages/SimulationArea.scss b/utils/client-simulation/src/Volo.ClientSimulation.Web/Pages/SimulationArea.scss index b43f39c08f..1a40c5d874 100644 --- a/utils/client-simulation/src/Volo.ClientSimulation.Web/Pages/SimulationArea.scss +++ b/utils/client-simulation/src/Volo.ClientSimulation.Web/Pages/SimulationArea.scss @@ -3,4 +3,7 @@ background-color: #d6ffce; margin: 3px; padding: 5px; + min-width: 250px; + overflow: hidden; + display: inline-block; } diff --git a/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/ClientSimulationModule.cs b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/ClientSimulationModule.cs index 28f79b1ba3..1ce09db41f 100644 --- a/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/ClientSimulationModule.cs +++ b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/ClientSimulationModule.cs @@ -11,10 +11,15 @@ namespace Volo.ClientSimulation { public override void ConfigureServices(ServiceConfigurationContext context) { - //TODO: Temporary add a DemoScenario, remove later Configure(options => { - options.Scenarios.Add(new DemoScenario()); + //TODO: Temporary add a DemoScenario + options.Scenarios.Add( + new ScenarioConfiguration( + typeof(DemoScenario), + clientCount: 20 + ) + ); }); } } diff --git a/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/ClientSimulationOptions.cs b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/ClientSimulationOptions.cs index 8de165fd2b..cdf94001c2 100644 --- a/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/ClientSimulationOptions.cs +++ b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/ClientSimulationOptions.cs @@ -1,15 +1,14 @@ using System.Collections.Generic; -using Volo.ClientSimulation.Scenarios; namespace Volo.ClientSimulation { public class ClientSimulationOptions { - public List Scenarios { get; } + public List Scenarios { get; } public ClientSimulationOptions() { - Scenarios = new List(); + Scenarios = new List(); } } } diff --git a/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Clients/DefaultClientFactory.cs b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Clients/DefaultClientFactory.cs new file mode 100644 index 0000000000..8015f58b86 --- /dev/null +++ b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Clients/DefaultClientFactory.cs @@ -0,0 +1,13 @@ +using Volo.Abp.DependencyInjection; +using Volo.ClientSimulation.Scenarios; + +namespace Volo.ClientSimulation.Clients +{ + public class DefaultClientFactory : IClientFactory, ISingletonDependency + { + public IDisposableClientHandler Create(IScenario scenario) + { + return new DisposableClientHandler(new Client(scenario)); + } + } +} \ No newline at end of file diff --git a/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Clients/IClientFactory.cs b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Clients/IClientFactory.cs index 0fd4d64849..b519cb086f 100644 --- a/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Clients/IClientFactory.cs +++ b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Clients/IClientFactory.cs @@ -1,7 +1,4 @@ -using System.Collections.Generic; -using System.Text; -using Volo.Abp.DependencyInjection; -using Volo.ClientSimulation.Scenarios; +using Volo.ClientSimulation.Scenarios; namespace Volo.ClientSimulation.Clients { @@ -9,12 +6,4 @@ namespace Volo.ClientSimulation.Clients { IDisposableClientHandler Create(IScenario scenario); } - - public class DefaultClientFactory : IClientFactory, ISingletonDependency - { - public IDisposableClientHandler Create(IScenario scenario) - { - return new DisposableClientHandler(new Client(scenario)); - } - } } diff --git a/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/GlobalOptions.cs b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/GlobalOptions.cs deleted file mode 100644 index 56e094a421..0000000000 --- a/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/GlobalOptions.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Volo.Abp.DependencyInjection; - -namespace Volo.ClientSimulation -{ - public class GlobalOptions : ISingletonDependency - { - public int MaxClientCount { get; set; } = 2; - - public GlobalOptions Clone() - { - return new GlobalOptions - { - MaxClientCount = MaxClientCount - }; - } - } -} \ No newline at end of file diff --git a/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/ScenarioConfiguration.cs b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/ScenarioConfiguration.cs new file mode 100644 index 0000000000..8b68c4482d --- /dev/null +++ b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/ScenarioConfiguration.cs @@ -0,0 +1,19 @@ +using System; + +namespace Volo.ClientSimulation +{ + public class ScenarioConfiguration + { + public Type ScenarioType { get; } + + public int ClientCount { get; } + + public ScenarioConfiguration( + Type scenarioType, + int clientCount = 1) + { + ScenarioType = scenarioType; + ClientCount = clientCount; + } + } +} \ No newline at end of file diff --git a/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/DemoScenario.cs b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/DemoScenario.cs new file mode 100644 index 0000000000..15ebd03a45 --- /dev/null +++ b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/DemoScenario.cs @@ -0,0 +1,13 @@ +using Volo.Abp; + +namespace Volo.ClientSimulation.Scenarios +{ + public class DemoScenario : Scenario + { + public DemoScenario() + { + AddStep(new SleepScenarioStep(RandomHelper.GetRandom(1000, 5000))); + AddStep(new SleepScenarioStep(RandomHelper.GetRandom(2000, 6000))); + } + } +} \ No newline at end of file diff --git a/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/IScenario.cs b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/IScenario.cs new file mode 100644 index 0000000000..951315dc5c --- /dev/null +++ b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/IScenario.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; + +namespace Volo.ClientSimulation.Scenarios +{ + public interface IScenario + { + IReadOnlyList Steps { get; } + + IScenarioStep CurrentStep { get; } + + int CurrentStepIndex { get; } + + string GetDisplayText(); + + void Proceed(); + } +} \ No newline at end of file diff --git a/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/IScenarioStep.cs b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/IScenarioStep.cs new file mode 100644 index 0000000000..0ec37ebaf4 --- /dev/null +++ b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/IScenarioStep.cs @@ -0,0 +1,9 @@ +namespace Volo.ClientSimulation.Scenarios +{ + public interface IScenarioStep + { + void Run(); + + string GetDisplayText(); + } +} \ No newline at end of file diff --git a/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/Scenario.cs b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/Scenario.cs index 48170ee6c5..51fb952d35 100644 --- a/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/Scenario.cs +++ b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/Scenario.cs @@ -1,12 +1,11 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; -using System.Text; -using System.Threading; +using Volo.Abp.DependencyInjection; namespace Volo.ClientSimulation.Scenarios { - public abstract class Scenario : IScenario + public abstract class Scenario : IScenario, ITransientDependency { public IReadOnlyList Steps => StepList.ToImmutableList(); protected List StepList { get; } @@ -52,7 +51,7 @@ namespace Volo.ClientSimulation.Scenarios if (StepList.Count <= 0) { throw new ApplicationException( - $"No Steps defined for the scenario '{GetDisplayText()}'" + $"No Steps added to the scenario '{GetDisplayText()}'" ); } } @@ -62,61 +61,4 @@ namespace Volo.ClientSimulation.Scenarios StepList.Add(step); } } - - public interface IScenario - { - IReadOnlyList Steps { get; } - - IScenarioStep CurrentStep { get; } - - int CurrentStepIndex { get; } - - string GetDisplayText(); - - void Proceed(); - } - - public interface IScenarioStep - { - void Run(); - - string GetDisplayText(); - } - - public abstract class ScenarioStep : IScenarioStep - { - public abstract void Run(); - - public virtual string GetDisplayText() - { - return GetType() - .Name - .RemovePostFix(nameof(ScenarioStep)); - } - } - - public class DemoScenario : Scenario - { - public DemoScenario() - { - AddStep(new SleepScenarioStep()); - AddStep(new SleepScenarioStep(3000)); - AddStep(new SleepScenarioStep()); - } - } - - public class SleepScenarioStep : ScenarioStep - { - public int Duration { get; } - - public SleepScenarioStep(int duration = 1000) - { - Duration = duration; - } - - public override void Run() - { - Thread.Sleep(Duration); - } - } } diff --git a/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/ScenarioStep.cs b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/ScenarioStep.cs new file mode 100644 index 0000000000..2751b5d35e --- /dev/null +++ b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/ScenarioStep.cs @@ -0,0 +1,16 @@ +using System; + +namespace Volo.ClientSimulation.Scenarios +{ + public abstract class ScenarioStep : IScenarioStep + { + public abstract void Run(); + + public virtual string GetDisplayText() + { + return GetType() + .Name + .RemovePostFix(nameof(ScenarioStep)); + } + } +} \ No newline at end of file diff --git a/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/SleepScenarioStep.cs b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/SleepScenarioStep.cs new file mode 100644 index 0000000000..499db9ac47 --- /dev/null +++ b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Scenarios/SleepScenarioStep.cs @@ -0,0 +1,24 @@ +using System.Threading; + +namespace Volo.ClientSimulation.Scenarios +{ + public class SleepScenarioStep : ScenarioStep + { + public int Duration { get; } + + public SleepScenarioStep(int duration = 1000) + { + Duration = duration; + } + + public override void Run() + { + Thread.Sleep(Duration); + } + + public override string GetDisplayText() + { + return base.GetDisplayText() + $"({Duration})"; + } + } +} \ No newline at end of file diff --git a/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Simulation.cs b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Simulation.cs index b266ea9c7c..783d4de39b 100644 --- a/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Simulation.cs +++ b/utils/client-simulation/src/Volo.ClientSimulation/Volo/ClientSimulation/Simulation.cs @@ -1,18 +1,15 @@ using System; using System.Collections.Generic; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; -using Volo.Abp; using Volo.Abp.DependencyInjection; using Volo.ClientSimulation.Clients; +using Volo.ClientSimulation.Scenarios; namespace Volo.ClientSimulation { - public class Simulation : ISingletonDependency + public class Simulation : ISingletonDependency, IDisposable { - public GlobalOptions GlobalOptions { get; } - - public GlobalOptions CurrentOptions { get; private set; } - public SimulationState State { get; private set; } public List ActiveClients { get; } @@ -21,14 +18,16 @@ namespace Volo.ClientSimulation protected ClientSimulationOptions Options { get; } + protected IServiceScope ServiceScope { get; } + public Simulation( - GlobalOptions globalOptions, IClientFactory clientFactory, + IServiceScopeFactory serviceScopeFactory, IOptions options) { - GlobalOptions = globalOptions; ClientFactory = clientFactory; Options = options.Value; + ServiceScope = serviceScopeFactory.CreateScope(); ActiveClients = new List(); } @@ -36,22 +35,21 @@ namespace Volo.ClientSimulation { State = SimulationState.Starting; - CurrentOptions = GlobalOptions.Clone(); - lock (ActiveClients) { ActiveClients.Clear(); - for (int i = 0; i < CurrentOptions.MaxClientCount; i++) + foreach (var scenarioConfiguration in Options.Scenarios) { - var selectedScenario = RandomHelper.GetRandomOfList(Options.Scenarios); - ActiveClients.Add(ClientFactory.Create(selectedScenario)); - } - - foreach (var activeClient in ActiveClients) - { - activeClient.Client.Stopped += ActiveClientOnStopped; - activeClient.Client.Start(); + for (int i = 0; i < scenarioConfiguration.ClientCount; i++) + { + var scenario = (IScenario) ServiceScope.ServiceProvider.GetRequiredService(scenarioConfiguration.ScenarioType); + var clientHandler = ClientFactory.Create(scenario); + + ActiveClients.Add(clientHandler); + clientHandler.Client.Stopped += ActiveClientOnStopped; + clientHandler.Client.Start(); + } } } @@ -82,5 +80,10 @@ namespace Volo.ClientSimulation ActiveClients.RemoveAll(c => c.Client == client); } } + + public void Dispose() + { + ServiceScope.Dispose(); + } } }