44import time
55import typing
66import enum
7+ import sys
78
89import ezmsg .core as ez
910
@@ -26,6 +27,9 @@ class StrEnum(str, enum.Enum):
2627
2728 pass
2829
30+ TIME = time .monotonic
31+ if sys .platform .startswith ('win' ):
32+ TIME = time .perf_counter
2933
3034def collect (
3135 components : typing .Optional [typing .Mapping [str , ez .Component ]] = None ,
@@ -96,16 +100,16 @@ async def initialize(self) -> None:
96100 @ez .publisher (OUTPUT )
97101 async def publish (self ) -> typing .AsyncGenerator :
98102 ez .logger .info (f"Load test publisher started. (PID: { os .getpid ()} )" )
99- start_time = time . time ()
103+ start_time = TIME ()
100104 for _ in range (self .SETTINGS .num_msgs ):
101- current_time = time . time ()
105+ current_time = TIME ()
102106 if current_time - start_time >= self .SETTINGS .max_duration :
103107 break
104108
105109 yield (
106110 self .OUTPUT ,
107111 LoadTestSample (
108- _timestamp = time . time (),
112+ _timestamp = TIME (),
109113 counter = self .STATE .counter ,
110114 dynamic_data = np .zeros (
111115 int (self .SETTINGS .dynamic_size // 4 ), dtype = np .float32
@@ -154,7 +158,7 @@ async def receive(self, sample: LoadTestSample) -> None:
154158 if sample .counter != counter + 1 :
155159 ez .logger .warning (f"{ sample .counter - counter - 1 } samples skipped!" )
156160 self .STATE .received_data .append (
157- (sample ._timestamp , time . time (), sample .counter )
161+ (sample ._timestamp , TIME (), sample .counter )
158162 )
159163 self .STATE .counters [sample .key ] = sample .counter
160164
@@ -322,10 +326,13 @@ def calculate_metrics(sink: LoadTestSink) -> Metrics:
322326 )
323327
324328 rx_timestamps = np .array ([rx_ts for _ , rx_ts , _ in sink .STATE .received_data ])
329+ rx_timestamps .sort ()
325330 runtime = max_timestamp - min_timestamp
326331 num_samples = len (sink .STATE .received_data )
327332 samplerate_mean = num_samples / runtime
328- samplerate_median = 1.0 / float (np .median (np .diff (rx_timestamps )))
333+ diff_timestamps = np .diff (rx_timestamps )
334+ diff_timestamps = diff_timestamps [np .nonzero (diff_timestamps )]
335+ samplerate_median = 1.0 / float (np .median (diff_timestamps ))
329336 latency_mean = total_latency / num_samples
330337 latency_median = list (sorted (latency ))[len (latency ) // 2 ]
331338 total_data = num_samples * sink .SETTINGS .dynamic_size
0 commit comments