Skip to content

Commit

Permalink
improved performance for ReportingStatsCache by replacing Map on Dict…
Browse files Browse the repository at this point in the history
…ionary
  • Loading branch information
AntyaDev committed Nov 25, 2022
1 parent b149b26 commit 1da8ab9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
14 changes: 7 additions & 7 deletions src/NBomber/Domain/Concurrency/ScenarioActor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ type ScenarioActor(scnCtx: ScenarioContextArgs, scenarioInfo: ScenarioInfo) =

let _logger = scnCtx.Logger.ForContext<ScenarioActor>()
let _scenario = scnCtx.Scenario
let mutable _actorWorking = false
let mutable _working = false

let _scenarioCtx = ScenarioContext(scenarioInfo, scnCtx)

let execSteps (runInfinite: bool) = backgroundTask {
try
if not _actorWorking then
if not _working then
let mutable shouldRun = true
_actorWorking <- true
_working <- true

while shouldRun && _actorWorking
while shouldRun && _working
&& not scnCtx.ScenarioCancellationToken.IsCancellationRequested
&& _scenario.PlanedDuration.TotalMilliseconds > scnCtx.ScenarioTimer.Elapsed.TotalMilliseconds do

Expand All @@ -32,13 +32,13 @@ type ScenarioActor(scnCtx: ScenarioContextArgs, scenarioInfo: ScenarioInfo) =
else
_logger.Fatal($"Unhandled exception: ExecSteps was invoked for already working actor with Scenario: {0}", _scenario.ScenarioName)
finally
_actorWorking <- false
_working <- false
}

member _.ScenarioStatsActor = scnCtx.ScenarioStatsActor
member _.ScenarioInfo = scenarioInfo
member _.Working = _actorWorking
member _.Working = _working

member _.ExecSteps() = execSteps false
member _.RunInfinite() = execSteps true
member _.Stop() = _actorWorking <- false
member _.Stop() = _working <- false
13 changes: 6 additions & 7 deletions src/NBomber/Domain/LoadTimeLine.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
module internal NBomber.Domain.LoadTimeLine

open System

open System.Collections.Generic
open FsToolkit.ErrorHandling

open NBomber
open NBomber.Contracts
open NBomber.Contracts.Stats
Expand Down Expand Up @@ -56,12 +55,12 @@ module Validation =

module TimeLineHistory =

let create (schedulersRealtimeStats: Map<TimeSpan,ScenarioStats> seq) =
let create (schedulersRealtimeStats: IReadOnlyDictionary<TimeSpan,ScenarioStats> seq) =
schedulersRealtimeStats
|> Seq.collect(fun stats -> stats |> Map.toList)
|> Seq.groupBy(fun (duration, _) -> duration)
|> Seq.map(fun (duration, stats) ->
let data = stats |> Seq.map snd |> Seq.toArray
|> Seq.collect id
|> Seq.groupBy(fun x -> x.Key)
|> Seq.map(fun (duration, scnStats) ->
let data = scnStats |> Seq.map(fun item -> item.Value) |> Seq.toArray
{ ScenarioStats = data; Duration = duration }
)
|> Seq.sortBy(fun x -> x.Duration)
Expand Down
8 changes: 4 additions & 4 deletions src/NBomber/Domain/Stats/ScenarioStatsActor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type State = {
StepsOrder: Dictionary<string, int>
GlobalInfoDataSize: ResizeArray<int>

mutable ReportingStatsCache: Map<TimeSpan,ScenarioStats>
ReportingStatsCache: Dictionary<TimeSpan,ScenarioStats>
CoordinatorStepsResults: Dictionary<string,RawMeasurementStats>
IntervalStepsResults: Dictionary<string,RawMeasurementStats>
mutable ConsoleScenarioStats: ScenarioStats // need to display on console (we merge absolute request counts)
Expand Down Expand Up @@ -63,7 +63,7 @@ let createState logger scenario reportingInterval mergeStatsFn =
StepsOrder = stepsOrder
GlobalInfoDataSize = ResizeArray<_>()

ReportingStatsCache = Map.empty
ReportingStatsCache = Dict.empty()

CoordinatorStepsResults = Dict.empty()
IntervalStepsResults = Dict.empty()
Expand Down Expand Up @@ -182,7 +182,7 @@ let mergeConsoleStats (consoleStats: ScenarioStats) (reportingStats: ScenarioSta
{ reportingStats with StepStats = updatedSteps }

let addReportingStats (state: State) (reportingStats: ScenarioStats) =
state.ReportingStatsCache <- Map.add reportingStats.Duration reportingStats state.ReportingStatsCache
state.ReportingStatsCache[reportingStats.Duration] <- reportingStats
state.ConsoleScenarioStats <- mergeConsoleStats state.ConsoleScenarioStats reportingStats

// reset reporting interval steps data
Expand Down Expand Up @@ -244,7 +244,7 @@ type ScenarioStatsActor(logger: ILogger,
loop() |> ignore

member _.ScenarioFailCount = _state.ScenarioFailCount
member _.AllRealtimeStats = _state.ReportingStatsCache
member _.AllRealtimeStats = _state.ReportingStatsCache :> IReadOnlyDictionary<_,_>
member _.ConsoleScenarioStats = _state.ConsoleScenarioStats

[<MethodImpl(MethodImplOptions.AggressiveInlining)>]
Expand Down
9 changes: 8 additions & 1 deletion src/NBomber/DomainServices/TestHost/TestHost.fs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,14 @@ type internal TestHost(dep: IGlobalDependency, regScenarios: Scenario list) as t
createScenarioSchedulers scenarios operation getScenarioClusterCount createStatsActor

member _.GetRealtimeStats(duration) =
_currentSchedulers |> List.map(fun x -> x.AllRealtimeStats.TryFind duration) |> Option.sequence
_currentSchedulers
|> List.map(fun x ->
if x.AllRealtimeStats.ContainsKey duration then
Some x.AllRealtimeStats[duration]
else
None
)
|> Option.sequence

member _.RunSession(sessionArgs: SessionArgs) = taskResult {
let! initializedScenarios = this.StartInit sessionArgs
Expand Down

0 comments on commit 1da8ab9

Please sign in to comment.