From a5bcca9d038c4d6b3f25f24c1c477cf00af0a32e Mon Sep 17 00:00:00 2001 From: Sergey Shepelev Date: Thu, 10 Jan 2013 16:25:04 +0400 Subject: [PATCH] tests: check_idle_cpu_usage measures getrusage() around sleep(); throws AssertionError --- tests/__init__.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/__init__.py b/tests/__init__.py index 836741d..22933fd 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,9 +1,11 @@ # package is named tests, not test, so it won't be confused with test in stdlib import errno import os +import resource import unittest import warnings +import eventlet from eventlet import debug, hubs @@ -175,6 +177,19 @@ class LimitedTestCase(unittest.TestCase): assertLessThanEqual = assert_less_than_equal + +def check_idle_cpu_usage(duration, allowed_part): + r1 = resource.getrusage(resource.RUSAGE_SELF) + eventlet.sleep(duration) + r2 = resource.getrusage(resource.RUSAGE_SELF) + utime = r2.ru_utime - r1.ru_utime + stime = r2.ru_stime - r1.ru_stime + assert utime + stime < duration * allowed_part, \ + "CPU usage over limit: user %.0f%% sys %.0f%% allowed %.0f%%" % ( + utime / duration * 100, stime / duration * 100, + allowed_part * 100) + + def verify_hub_empty(): from eventlet import hubs hub = hubs.get_hub()