1# Copyright 2015 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import csv, logging, os
6import time
7
8from autotest_lib.client.bin import site_utils, test, utils
9from autotest_lib.client.common_lib import error
10from autotest_lib.client.common_lib.cros import chrome
11
12# Measurement duration [seconds] for one interation.
13MEASUREMENT_DURATION = 10
14
15TOTAL_TEST_DURATION = 600 # change the test time to 7 days [seconds].
16
17# Time to exclude from calculation after launching the demo [seconds].
18STABILIZATION_DURATION = 20
19
20_PERF_RESULT_FILE = '/tmp/perf.csv'
21
22class enterprise_KioskPerf(test.test):
23    """Enrolls to kiosk mode and monitors cpu/memory usage."""
24
25    version = 1
26
27
28    def test_cpu_usage(self):
29        """
30        Runs the video cpu usage test.
31
32        @param local_path: the path to the video file.
33
34        @returns a dictionary that contains the test result.
35        """
36        cpu_usage_start = site_utils.get_cpu_usage()
37        time.sleep(MEASUREMENT_DURATION)
38        cpu_usage_end = site_utils.get_cpu_usage()
39        return site_utils.compute_active_cpu_time(cpu_usage_start,
40                                                  cpu_usage_end) * 100
41
42
43    def used_mem(self):
44        """Returns total used memory in %."""
45        total_memory = site_utils.get_mem_total()
46        return (total_memory - site_utils.get_mem_free()) * 100 / total_memory
47
48    def verify_enrollment(self, user_id):
49        """Verifies enterprise enrollment using /home/.shadow config."""
50        with open('/home/.shadow/install_attributes.pb') as f:
51            if not user_id in f.read():
52                raise error.TestError('Device is not enrolled or '
53                                      'enterprise owned.')
54
55    def run_once(self):
56        user_id, password = utils.get_signin_credentials(os.path.join(
57                os.path.dirname(os.path.realpath(__file__)), 'credentials.txt'))
58        if not (user_id and password):
59            logging.warn('No credentials found - exiting test.')
60            return
61
62        with chrome.Chrome(auto_login=False) as cr:
63            cr.browser.oobe.NavigateGaiaLogin(
64                    user_id, password,
65                    enterprise_enroll=True,
66                    for_user_triggered_enrollment=True)
67            time.sleep(STABILIZATION_DURATION)
68            self.verify_enrollment(user_id)
69            start_time = time.time()
70            perf_keyval = {}
71            perf_file = open(_PERF_RESULT_FILE, 'w')
72            writer = csv.writer(perf_file)
73            writer.writerow(['cpu','memory', 'timestamp'])
74            while (time.time() - start_time) < TOTAL_TEST_DURATION:
75                perf_keyval['cpu_usage'] = self.test_cpu_usage()
76                perf_keyval['memory_usage'] = self.used_mem()
77                writer.writerow([perf_keyval['cpu_usage'],
78                                perf_keyval['memory_usage'],
79                                time.strftime('%Y/%m/%d %H:%M:%S')])
80                self.write_perf_keyval(perf_keyval)
81                time.sleep(10)
82            perf_file.close()
83