Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / tools / telemetry / experimental / service_noise_benchmark.py
blob660e415cd47754a48ad3114a7ed2b94ff38111fa
1 #!/usr/bin/env python
2 # Copyright 2015 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 import csv
7 import logging
8 import os
9 import subprocess
10 import sys
11 import time
13 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir))
15 from telemetry.core import platform
18 MONITORING_DURATION_SECONDS = 60 * 60
19 SAMPLE_INTERVAL_SECONDS = 10
20 RESULTS_FILE_NAME = 'service_benchmark_results.csv'
21 METRIC_NAMES = ['Power (W)', 'Temperature (C)']
22 FIELD_NAMES = ['label'] + METRIC_NAMES
23 SERVICES = (
24 'Adobe Acrobat Update Service',
25 'DSM SA Connection Service',
26 'DSM SA Data Manager',
27 'DSM SA Event Manager',
28 'DSM SA Shared Services',
29 'Intel(R) HD Graphics Control Panel Service',
30 'LEMSS Agent',
31 'Lumension Patch Module',
32 #'NSClient++ (x64)',
33 'Puppet Agent',
34 'SQL Server VSS Writer',
35 'Windows Firewall',
36 'Windows Update',
40 def MonitorAndRecordPower(label):
41 logging.debug('Monitoring %s for %d seconds...', label, MONITORING_DURATION_SECONDS)
43 results = []
44 for _ in xrange(int(MONITORING_DURATION_SECONDS / SAMPLE_INTERVAL_SECONDS)):
45 platform.GetHostPlatform().StartMonitoringPower(None)
46 time.sleep(SAMPLE_INTERVAL_SECONDS)
47 result = platform.GetHostPlatform().StopMonitoringPower()
49 result = {
50 'label': label,
51 'Power (W)': result['energy_consumption_mwh'] * 3.6 / SAMPLE_INTERVAL_SECONDS,
52 'Temperature (C)': result['component_utilization']['whole_package']['average_temperature_c'],
54 results.append(result)
56 with open(RESULTS_FILE_NAME, 'a') as results_file:
57 for result in results:
58 csv.DictWriter(results_file, fieldnames=FIELD_NAMES).writerow(result)
61 def DisableService(service):
62 logging.debug('Stopping %s.', service)
63 subprocess.check_call(('net', 'stop', service), stdout=subprocess.PIPE)
66 def EnableService(service):
67 logging.debug('Starting %s.', service)
68 subprocess.check_call(('net', 'start', service), stdout=subprocess.PIPE)
71 class PauseServices(object):
72 def __init__(self, services):
73 self._services = services
74 self._disabled_services = []
76 def __enter__(self):
77 for service in self._services:
78 try:
79 DisableService(service)
80 self._disabled_services.append(service)
81 except subprocess.CalledProcessError:
82 logging.info('Failed to stop %s.' % service)
84 def __exit__(self, _, __, ___):
85 for service in self._disabled_services:
86 try:
87 EnableService(service)
88 except subprocess.CalledProcessError:
89 logging.info('Failed to start %s.' % service)
90 self._disabled_services = []
93 def ReformatResults():
94 results = {}
95 for metric in METRIC_NAMES:
96 results[metric] = {}
97 with open(RESULTS_FILE_NAME, 'r') as results_file:
98 reader = csv.DictReader(results_file)
99 for row in reader:
100 for metric in METRIC_NAMES:
101 if row['label'] not in results[metric]:
102 results[metric][row['label']] = []
103 results[metric][row['label']].append(row[metric])
105 for metric in METRIC_NAMES:
106 root, ext = os.path.splitext(RESULTS_FILE_NAME)
107 metric_results_file_name = '%s %s%s' % (root, metric, ext)
108 with open(metric_results_file_name, 'w') as metric_results_file:
109 labels = results[metric].keys()
110 writer = csv.DictWriter(metric_results_file, fieldnames=labels)
111 writer.writeheader()
113 data_point_count = max(map(len, results[metric].itervalues()))
114 for i in xrange(data_point_count):
115 writer.writerow({label: results[metric][label][i] for label in labels})
118 def SetUp():
119 logging.getLogger().setLevel(logging.INFO)
121 if not platform.GetHostPlatform().CanMonitorPower():
122 print >> sys.stderr, "Can't monitor power."
123 sys.exit(1)
125 with open(RESULTS_FILE_NAME, 'w') as results_file:
126 csv.DictWriter(results_file, fieldnames=FIELD_NAMES).writeheader()
129 def main():
130 SetUp()
132 logging.info('Testing %d services.' % len(SERVICES))
134 logging.info('Testing with services enabled for %d seconds.',
135 MONITORING_DURATION_SECONDS)
136 MonitorAndRecordPower('default')
138 with PauseServices(SERVICES):
139 logging.info('Testing with services disabled for %d seconds.',
140 MONITORING_DURATION_SECONDS)
141 MonitorAndRecordPower('control')
143 for i, service in enumerate(SERVICES):
144 logging.info('Testing %s for %d seconds. (%d/%d)', service,
145 MONITORING_DURATION_SECONDS, i + 1, len(SERVICES))
146 EnableService(service)
147 MonitorAndRecordPower(service)
148 DisableService(service)
150 ReformatResults()
153 if __name__ == '__main__':
154 main()