Revise simulation code

pull/955/head^2
Halil ibrahim Kalkan 6 years ago
parent 3948223b52
commit ee51584e0a

@ -2,7 +2,7 @@
@model Volo.ClientSimulation.Web.Pages.IndexModel
@section styles {
<abp-style-bundle>
<abp-style src="/Pages/SimulationArea.min.css" />
<abp-style src="/Pages/SimulationArea.css" />
</abp-style-bundle>
}
@section scripts {

@ -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; }

@ -1 +1 @@
.simulation-client{border:1px solid #008000;background-color:#d6ffce;margin:3px;padding:5px;}
.simulation-client{border:1px solid #008000;background-color:#d6ffce;margin:3px;padding:5px;min-width:250px;overflow:hidden;display:inline-block;}

@ -3,4 +3,7 @@
background-color: #d6ffce;
margin: 3px;
padding: 5px;
min-width: 250px;
overflow: hidden;
display: inline-block;
}

@ -11,10 +11,15 @@ namespace Volo.ClientSimulation
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
//TODO: Temporary add a DemoScenario, remove later
Configure<ClientSimulationOptions>(options =>
{
options.Scenarios.Add(new DemoScenario());
//TODO: Temporary add a DemoScenario
options.Scenarios.Add(
new ScenarioConfiguration(
typeof(DemoScenario),
clientCount: 20
)
);
});
}
}

@ -1,15 +1,14 @@
using System.Collections.Generic;
using Volo.ClientSimulation.Scenarios;
namespace Volo.ClientSimulation
{
public class ClientSimulationOptions
{
public List<IScenario> Scenarios { get; }
public List<ScenarioConfiguration> Scenarios { get; }
public ClientSimulationOptions()
{
Scenarios = new List<IScenario>();
Scenarios = new List<ScenarioConfiguration>();
}
}
}

@ -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));
}
}
}

@ -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));
}
}
}

@ -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
};
}
}
}

@ -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;
}
}
}

@ -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)));
}
}
}

@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace Volo.ClientSimulation.Scenarios
{
public interface IScenario
{
IReadOnlyList<IScenarioStep> Steps { get; }
IScenarioStep CurrentStep { get; }
int CurrentStepIndex { get; }
string GetDisplayText();
void Proceed();
}
}

@ -0,0 +1,9 @@
namespace Volo.ClientSimulation.Scenarios
{
public interface IScenarioStep
{
void Run();
string GetDisplayText();
}
}

@ -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<IScenarioStep> Steps => StepList.ToImmutableList();
protected List<IScenarioStep> 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<IScenarioStep> 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);
}
}
}

@ -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));
}
}
}

@ -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})";
}
}
}

@ -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<IDisposableClientHandler> 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<ClientSimulationOptions> options)
{
GlobalOptions = globalOptions;
ClientFactory = clientFactory;
Options = options.Value;
ServiceScope = serviceScopeFactory.CreateScope();
ActiveClients = new List<IDisposableClientHandler>();
}
@ -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();
}
}
}

Loading…
Cancel
Save