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.
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
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',
31 'Lumension Patch Module',
34 'SQL Server VSS Writer',
40 def MonitorAndRecordPower(label
):
41 logging
.debug('Monitoring %s for %d seconds...', label
, MONITORING_DURATION_SECONDS
)
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()
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
= []
77 for service
in self
._services
:
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
:
87 EnableService(service
)
88 except subprocess
.CalledProcessError
:
89 logging
.info('Failed to start %s.' % service
)
90 self
._disabled
_services
= []
93 def ReformatResults():
95 for metric
in METRIC_NAMES
:
97 with
open(RESULTS_FILE_NAME
, 'r') as results_file
:
98 reader
= csv
.DictReader(results_file
)
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
)
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
})
119 logging
.getLogger().setLevel(logging
.INFO
)
121 if not platform
.GetHostPlatform().CanMonitorPower():
122 print >> sys
.stderr
, "Can't monitor power."
125 with
open(RESULTS_FILE_NAME
, 'w') as results_file
:
126 csv
.DictWriter(results_file
, fieldnames
=FIELD_NAMES
).writeheader()
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
)
153 if __name__
== '__main__':