Skip to content

Commit

Permalink
issue pywinauto#852: re-work Application.cpu_usage for Linux platform
Browse files Browse the repository at this point in the history
  • Loading branch information
airelil committed Jan 31, 2020
1 parent 39e20e9 commit 65a62e2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
32 changes: 26 additions & 6 deletions pywinauto/linux/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

from ..backend import registry
from ..base_application import AppStartError, ProcessNotFoundError, AppNotConnected, BaseApplication
from ..timings import Timings # noqa: E402


class Application(BaseApplication):
Expand Down Expand Up @@ -120,13 +121,32 @@ def cpu_usage(self, interval=None):
if not self.process:
raise AppNotConnected("Please use start or connect before trying "
"anything else")
if interval:
time.sleep(interval)
proc_pid_stat = "/proc/{}/stat".format(self.process)
def read_cpu_info():
with open(proc_pid_stat, 'r') as s:
pid_info = s.read().split()
with open("/proc/stat") as s:
info = s.read().split()
# return a tuple as following:
# pid utime, pid stime, total utime, total stime
return (int(pid_info[13]), int(pid_info[14]), int(info[1]), int(info[3]))

try:
proc_info = subprocess.check_output(["ps", "-p", str(self.process), "-o", "%cpu"], universal_newlines=True)
proc_info = proc_info.split("\n")
return float(proc_info[1])
except Exception:
before = read_cpu_info()
if not interval:
interval = Timings.cpu_usage_interval
time.sleep(interval)
after = read_cpu_info()
pid_time = (after[0] - before[0]) + (after[1] - before[1])
sys_time = (after[2] - before[2]) + (after[3] - before[3])
if not sys_time:
res = 0.0
else:
res = 100.0 * (float(pid_time) / float(sys_time))
#print("linux app cpu usage: ", res)
return res

except IOError:
raise ProcessNotFoundError()

def kill(self, soft=False):
Expand Down
20 changes: 16 additions & 4 deletions pywinauto/unittests/test_application_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
import time

sys.path.append(".")
from pywinauto.linux.application import Application, AppStartError, AppNotConnected
from pywinauto.application import WindowSpecification # noqa: E402
if sys.platform.startswith('linux'):
from pywinauto.controls import atspiwrapper # register atspi backend
from pywinauto.linux.application import Application # noqa: E402
from pywinauto.linux.application import AppStartError # noqa: E402
from pywinauto.linux.application import AppNotConnected # noqa: E402
from pywinauto.linux.application import ProcessNotFoundError # noqa: E402

app_name = r"gtk_example.py"

Expand Down Expand Up @@ -77,10 +80,19 @@ def test_connect_by_path(self):
self.app.connect(path=_test_app())
self.assertEqual(self.app.process, self.subprocess_app.pid)

def test_get_cpu_usage(self):
def test_cpu_usage(self):
self.app.start(_test_app())
time.sleep(1)
self.assertGreater(self.app.cpu_usage(), 0)
self.assertGreater(self.app.cpu_usage(0.1), 0)
self.app.wait_cpu_usage_lower(threshold=0.1, timeout=2.9, usage_interval=0.2)
# default timings
self.assertEqual(self.app.cpu_usage(), 0)

# non-existing process
self.app.kill()
self.assertRaises(ProcessNotFoundError, self.app.cpu_usage, 7.8)

# not connected or not started app
self.assertRaises(AppNotConnected, Application().cpu_usage, 12.3)

def test_is_process_running(self):
self.app.start(_test_app())
Expand Down

0 comments on commit 65a62e2

Please sign in to comment.