-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stats.fs
73 lines (58 loc) · 2.57 KB
/
Stats.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
namespace Propulsion.EventStoreDB
open System
open Propulsion.Streams
open Serilog
module Stats =
[<NoComparison; NoEquality>]
type InternalBatch =
{
firstPosition : int64
lastPosition : int64
events : StreamEvent<byte []> []
isEnd : bool
}
member this.Length = this.events.Length
type IStats =
abstract Start: unit -> Async<unit>
abstract UpdateBatch: InternalBatch -> unit
abstract UpdateEmptySlice: unit -> unit
abstract UpdateCommitedPosition: int64 -> unit
abstract UpdateCurMax: int -> int -> unit
type Stats (logger: ILogger, statsInterval: TimeSpan) =
let mutable batchFirstPosition = 0L
let mutable batchLastPosition = 0L
let mutable batchCaughtUp = false
let mutable slicesRead = 0
let mutable slicesEmpty = 0
let mutable recentSlicesRead = 0
let mutable recentSlicesEmpty = 0
let mutable currentBatches = 0
let mutable maxBatches = 0
let mutable lastCommittedPosition = 0L
let report () =
logger.Information (
"Slices Read {slicesRead} Empty {slicesEmpty} | Recent Read {recentSlicesRead} Empty {recentSlicesEmpty} | Position Read {batchLastPosition} Committed {lastCommittedPosition} | Caught up {caughtUp} | cur {cur} / max {max}",
slicesRead, slicesEmpty, recentSlicesRead, recentSlicesEmpty, batchLastPosition, lastCommittedPosition, batchCaughtUp, currentBatches, maxBatches)
interface IStats with
member __.UpdateBatch (batch: InternalBatch) =
batchFirstPosition <- batch.firstPosition
batchLastPosition <- batch.lastPosition
batchCaughtUp <- batch.isEnd
slicesRead <- slicesRead + 1
recentSlicesRead <- recentSlicesRead + 1
member __.UpdateEmptySlice () =
slicesEmpty <- slicesEmpty + 1
recentSlicesEmpty <- recentSlicesEmpty + 1
member __.UpdateCommitedPosition pos =
lastCommittedPosition <- pos
member __.UpdateCurMax cur max =
currentBatches <- cur
maxBatches <- max
member __.Start () = async {
let! ct = Async.CancellationToken
while not ct.IsCancellationRequested do
report ()
recentSlicesRead <- 0
recentSlicesEmpty <- 0
do! Async.Sleep statsInterval
}