@@ -798,33 +798,37 @@ def assert_no_warning(pat, msg = nil)
798
798
MIN_MEASURABLE = 1.0 / MIN_HZ
799
799
800
800
def assert_cpu_usage_low ( msg = nil , pct : 0.05 , wait : 1.0 , stop : nil )
801
- require 'benchmark'
802
-
803
801
wait = EnvUtil . apply_timeout_scale ( wait )
804
802
if wait < 0.1 # TIME_QUANTUM_USEC in thread_pthread.c
805
803
warn "test #{ msg || 'assert_cpu_usage_low' } too short to be accurate"
806
804
end
807
- tms = Benchmark . measure ( msg || '' ) do
808
- if stop
809
- th = Thread . start { sleep wait ; stop . call }
810
- yield
811
- th . join
812
- else
813
- begin
814
- Timeout . timeout ( wait ) { yield }
815
- rescue Timeout ::Error
816
- end
805
+
806
+ t0 , r0 = Process . times , Process . clock_gettime ( Process ::CLOCK_MONOTONIC )
807
+
808
+ if stop
809
+ th = Thread . start { sleep wait ; stop . call }
810
+ yield
811
+ th . join
812
+ else
813
+ begin
814
+ Timeout . timeout ( wait ) { yield }
815
+ rescue Timeout ::Error
817
816
end
818
817
end
819
818
820
- max = pct * tms . real
819
+ t1 , r1 = Process . times , Process . clock_gettime ( Process ::CLOCK_MONOTONIC )
820
+
821
+ total = t1 . utime - t0 . utime + t1 . stime - t0 . stime + t1 . cutime - t0 . cutime + t1 . cstime - t0 . cstime
822
+ real = r1 - r0
823
+
824
+ max = pct * real
821
825
min_measurable = MIN_MEASURABLE
822
826
min_measurable *= 1.30 # add a little (30%) to account for misc. overheads
823
827
if max < min_measurable
824
828
max = min_measurable
825
829
end
826
830
827
- assert_operator tms . total , :<= , max , msg
831
+ assert_operator total , :<= , max , msg
828
832
end
829
833
830
834
def assert_is_minus_zero ( f )
0 commit comments