Skip to content

Commit

Permalink
added IScenarioContext.TestInfo and NodeInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
AntyaDev committed Dec 28, 2022
1 parent 7644e4e commit d89cf0e
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/NBomber/Api/CSharp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ type Scenario =
static member WithLoadSimulations(scenario: ScenarioProps, [<ParamArray>]loadSimulations: LoadSimulation[]) =
scenario |> FSharp.Scenario.withLoadSimulations(Seq.toList loadSimulations)

/// With this configuration, you can enable or disable Scenario iteration reset.
/// This method allows enabling or disabling the reset of Scenario iteration in case of step failure.
/// By default, on fail Step response, NBomber will reset the current Scenario iteration.
/// Sometimes, you would like to handle failed steps differently: retry, ignore or use a fallback.
/// For such cases, you can disable scenario iteration reset.
Expand Down
2 changes: 1 addition & 1 deletion src/NBomber/Api/FSharp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ module Scenario =
let withLoadSimulations (loadSimulations: LoadSimulation list) (scenario: ScenarioProps) =
{ scenario with LoadSimulations = loadSimulations }

/// With this configuration, you can enable or disable Scenario iteration reset.
/// This method allows enabling or disabling the reset of Scenario iteration in case of step failure.
/// By default, on fail Step response, NBomber will reset the current Scenario iteration.
/// Sometimes, you would like to handle failed steps differently: retry, ignore or use a fallback.
/// For such cases, you can disable scenario iteration reset.
Expand Down
2 changes: 1 addition & 1 deletion src/NBomber/Domain/Concurrency/ScenarioActor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type ScenarioActor(scnCtx: ScenarioContextArgs, scenarioInfo: ScenarioInfo) =
let _cancelToken = scnCtx.ScenarioCancellationToken.Token
let mutable _working = false

let _scenarioCtx = ScenarioContext(scenarioInfo, scnCtx)
let _scenarioCtx = ScenarioContext(scnCtx, scenarioInfo)

let execSteps (runInfinite: bool) = backgroundTask {
try
Expand Down
12 changes: 9 additions & 3 deletions src/NBomber/Domain/ScenarioContext.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ open System.Diagnostics
open System.Threading
open Serilog
open NBomber.Contracts
open NBomber.Contracts.Stats
open NBomber.Domain.DomainTypes
open NBomber.Domain.Stats.ScenarioStatsActor

Expand All @@ -16,14 +17,17 @@ type ScenarioContextArgs = {
ScenarioOperation: ScenarioOperation
ScenarioStatsActor: ScenarioStatsActor
ExecStopCommand: StopCommand -> unit
TestInfo: TestInfo
GetNodeInfo: unit -> NodeInfo
}

type ScenarioContext(scenarioInfo, args: ScenarioContextArgs) =
type ScenarioContext(args: ScenarioContextArgs, scenarioInfo) =

let _logger = args.Logger
let _scnActor = args.ScenarioStatsActor
let _timer = args.ScenarioTimer
let _resetIteration = args.Scenario.ResetIterationOnFail
let _testInfo = args.TestInfo
let _data = Dictionary<string,obj>()
let mutable _invocationNumber = 0

Expand All @@ -37,9 +41,11 @@ type ScenarioContext(scenarioInfo, args: ScenarioContextArgs) =
_data.Clear()

interface IScenarioContext with
member this.InvocationNumber = _invocationNumber
member this.Logger = _logger
member this.TestInfo = _testInfo
member this.ScenarioInfo = scenarioInfo
member this.NodeInfo = args.GetNodeInfo()
member this.Logger = _logger
member this.InvocationNumber = _invocationNumber
member this.Data = _data
member this.StopCurrentTest(reason) = args.ExecStopCommand(StopTest reason)
member this.StopScenario(scenarioName, reason) = args.ExecStopCommand(StopScenario(scenarioName, reason))
43 changes: 25 additions & 18 deletions src/NBomber/DomainServices/TestHost/TestHost.fs
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,14 @@ type internal TestHost(dep: IGlobalDependency, regScenarios: Scenario list) as t
let mutable _targetScenarios = List.empty<Scenario>
let mutable _sessionArgs = SessionArgs.empty
let mutable _currentOperation = OperationType.None
let _defaultNodeInfo = NodeInfo.init None
let mutable _nodeInfo = { NodeInfo.init None with NodeType = dep.NodeType; CurrentOperation = _currentOperation }

let getCurrentNodeInfo () =
{ _defaultNodeInfo with NodeType = dep.NodeType; CurrentOperation = _currentOperation }

let execStopCommand (command: StopCommand) =
match command with
| StopScenario (scenarioName, reason) ->
_currentSchedulers
|> List.tryFind(fun sch -> sch.Working && sch.Scenario.ScenarioName = scenarioName)
|> Option.iter(fun sch ->
sch.Stop()
_log.Warning("Stopping scenario early: {ScenarioName}, reason: {StopReason}", sch.Scenario.ScenarioName, reason)
)

| StopTest reason -> this.StopScenarios(reason) |> ignore
if _nodeInfo.CurrentOperation = _currentOperation then
_nodeInfo
else
_nodeInfo <- { _nodeInfo with CurrentOperation = _currentOperation }
_nodeInfo

let createScenarioSchedulers (targetScenarios: Scenario list)
(operation: ScenarioOperation)
Expand All @@ -67,7 +59,9 @@ type internal TestHost(dep: IGlobalDependency, regScenarios: Scenario list) as t
ScenarioTimer = Stopwatch()
ScenarioOperation = operation
ScenarioStatsActor = createStatsActor _log scn (_sessionArgs.GetReportingInterval())
ExecStopCommand = execStopCommand
ExecStopCommand = this.ExecStopCommand
TestInfo = _sessionArgs.TestInfo
GetNodeInfo = getCurrentNodeInfo
}
let count = getScenarioClusterCount scn.ScenarioName
new ScenarioScheduler(scnDep, count)
Expand Down Expand Up @@ -190,11 +184,24 @@ type internal TestHost(dep: IGlobalDependency, regScenarios: Scenario list) as t
_log.Information "Starting bombing..."
do! startScenarios schedulers (Some reportingManager)

do! this.StopScenarios()
do! this.StopAllScenarios()
_currentOperation <- OperationType.Complete
}

member _.StopScenarios([<Optional;DefaultParameterValue("":string)>]reason: string) =
abstract member ExecStopCommand: command:StopCommand -> unit
default _.ExecStopCommand(command) =
match command with
| StopScenario (scenarioName, reason) ->
_currentSchedulers
|> List.tryFind(fun sch -> sch.Working && sch.Scenario.ScenarioName = scenarioName)
|> Option.iter(fun sch ->
sch.Stop()
_log.Warning("Stopping scenario early: {ScenarioName}, reason: {StopReason}", sch.Scenario.ScenarioName, reason)
)

| StopTest reason -> this.StopAllScenarios(reason) |> ignore

member _.StopAllScenarios([<Optional;DefaultParameterValue("":string)>]reason: string) =
if _currentOperation <> OperationType.Stop && not _stopped then
_currentOperation <- OperationType.Stop

Expand Down Expand Up @@ -262,7 +269,7 @@ type internal TestHost(dep: IGlobalDependency, regScenarios: Scenario list) as t
member _.Dispose() =
if not _disposed then
_disposed <- true
this.StopScenarios().Wait()
this.StopAllScenarios().Wait()

for sink in dep.ReportingSinks do
use _ = sink
Expand Down
7 changes: 3 additions & 4 deletions src/NBomber/NBomber.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
<ServerGarbageCollection>true</ServerGarbageCollection>
<ConcurrentGarbageCollection>true</ConcurrentGarbageCollection>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageReleaseNotes>- added ClientPool
- fxed bug with wrong data size calculation for GlobalStep info</PackageReleaseNotes>
<PackageReleaseNotes></PackageReleaseNotes>
</PropertyGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.fs" />
Expand Down Expand Up @@ -72,7 +71,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NBomber.Contracts" Version="[4.0.0-beta13]" />
<PackageReference Include="NBomber.Contracts" Version="[4.0.0-beta15]" />
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="FsToolkit.ErrorHandling.TaskResult" Version="2.13.0" />
<PackageReference Include="FuncyDown" Version="1.3.0" />
Expand All @@ -92,6 +91,6 @@
<EmbeddedResource Include="Resources\HtmlReport\assets\css\sidebar.css" />
<EmbeddedResource Include="Resources\HtmlReport\assets\css\index.css" />
<EmbeddedResource Include="Resources\HtmlReport\index.html" />
</ItemGroup>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ open Xunit

open NBomber
open NBomber.Contracts
open NBomber.Contracts.Stats
open NBomber.FSharp
open NBomber.Domain
open NBomber.Domain.Stats.ScenarioStatsActor
Expand Down Expand Up @@ -41,6 +42,8 @@ let internal baseScnDep = {
ScenarioOperation = ScenarioOperation.Bombing
ScenarioStatsActor = ScenarioStatsActor(logger, baseScenario, Constants.DefaultReportingInterval)
ExecStopCommand = fun _ -> ()
TestInfo = TestInfo.empty
GetNodeInfo = fun () -> NodeInfo.empty
}

[<Fact>]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ open FsToolkit.ErrorHandling

open NBomber
open NBomber.Contracts
open NBomber.Contracts.Stats
open NBomber.FSharp
open NBomber.Domain
open NBomber.Domain.Stats.ScenarioStatsActor
Expand Down Expand Up @@ -41,6 +42,8 @@ let internal baseScnDep = {
ScenarioOperation = ScenarioOperation.Bombing
ScenarioStatsActor = ScenarioStatsActor(logger, baseScenario, Constants.DefaultReportingInterval)
ExecStopCommand = fun _ -> ()
TestInfo = TestInfo.empty
GetNodeInfo = fun () -> NodeInfo.empty
}

[<Fact>]
Expand Down

0 comments on commit d89cf0e

Please sign in to comment.