-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpowerusageanalyzer.py
202 lines (154 loc) · 7.99 KB
/
powerusageanalyzer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import argparse
import os
import time
import json
from comparisons import compare_data, compare_to_wpa
def analysisparser():
parser = argparse.ArgumentParser(
description='Analyze data from a `usagerunfrom<TIME>` folder containing the directories'
'`baseline`, and `test`. Analysis performed depends on the flag. Currently '
'the only existing analysis is a simple comparison against the baseline.'
)
parser.add_argument('--data', type=str, default=None,
help='Location of the data (must point to usagerunfrom*).')
parser.add_argument('--ignore-power', action='store_true', default=False,
help="Ignore power SRUMUTIL measurements.")
parser.add_argument('--time-to-analyze', type=int, default=None,
help='Amount of time to analyze.')
parser.add_argument('--baseline-data', type=str, default=None,
help='Sets the baseline data directory. Ignored if --data is specified.')
parser.add_argument('--baseline-time', type=int, default=None,
help='Length of baseline in seconds, default is obtained from the config.')
parser.add_argument('--test-time', type=int, default=None,
help='Length of baseline in seconds, default is obtained from the config.')
parser.add_argument('--test-data', type=str, default=None,
help='Sets the test data directory. Ignored if --data is specified.')
parser.add_argument('--config-data', type=str, default=None,
help='Sets the config to use (a .json). Ignored if --data is specified. '
'It can be found in usagerunfrom* folders.')
parser.add_argument('--application', nargs='+', required=True,
help='A string that represents the application we should be looking for '
'in the given `test` directories `srumuti*.csv` files. i.e. `firefox` '
'or `firefox.exe`.')
parser.add_argument('--baseline-application', nargs='+', default=None,
help='Same as application, but for the baseline, by default we use power '
'usage measurements from all listed applications. As an example, this '
'can be used to compare Firefox idling against Firefox displaying '
'a full screen video.')
parser.add_argument('--exclude-baseline-apps', nargs='+', default=None,
help='Excludes listed applications from power measurements during baseline.')
parser.add_argument('--exclude-test-apps', nargs='+', default=None,
help='Excludes listed applications from power measurements during test.')
parser.add_argument('--output', type=str, default=os.getcwd(),
help='Location to store output.')
parser.add_argument('--smooth-battery', action='store_true', default=False,
help='Type of output to store, can be either `csv` or `json`.')
parser.add_argument('--plot-power', action='store_true', default=False,
help='Plots power usage over time.')
parser.add_argument('--plot-battery', action='store_true', default=False,
help='Plots battery usage over time, drain rates, and the approximate linear drain rate.')
parser.add_argument('--consumption-from', nargs='+', default=None,
help='Only calculates power consumption from these sources (must match values from SRUMUTIL csv header).')
parser.add_argument('--compare', action='store_true', default=False,
help='Compares the baseline data to the test data (defined by the folder names).')
parser.add_argument('--compare-to-wpa', action='store_true', default=False,
help='Compares the data type specified by --wpa-type, to the WPA data specified with this flag.')
parser.add_argument('--wpa-type', type=str, default='baseline',
help='The data type (either baseline, by default, or test) to use in comparison with WPA data.')
return parser
def display_results(results):
print()
for key in results:
print(key)
print(results[key])
print()
print("Summary of baseline results")
print("Battery percent-lost: %s" % str(results['battery'].split('\n')[-1].split(',')[4]))
print("Battery: %s mW, %s mWh" % (
str(results['battery'].split('\n')[-1].split(',')[0]),
str(results['battery'].split('\n')[-1].split(',')[2])
)
)
print("Power: %s mW, %s mWh" % (
str(results['power-base-mw'].split('\n')[-1].split(',')[-1]),
str(results['power-base-mwh'].split('\n')[-1].split(',')[-1])
)
)
print()
print("Summary of test results")
print("Battery percent-lost: %s" % str(results['battery'].split('\n')[-1].split(',')[5]))
print("Battery: %s mW, %s mWh" % (
str(results['battery'].split('\n')[-1].split(',')[1]),
str(results['battery'].split('\n')[-1].split(',')[3])
)
)
print("Power: %s mW, %s mWh" % (
str(results['power-test-mw'].split('\n')[-1].split(',')[-1]),
str(results['power-test-mwh'].split('\n')[-1].split(',')[-1])
)
)
return
def display_wpa_results(results):
return
def do_comparison(args):
if args['data']:
datadir_abs = os.path.abspath(args['data'])
baselinedir = os.path.join(datadir_abs, 'baseline')
testdir = os.path.join(datadir_abs, 'testing')
resultsdir = os.path.join(datadir_abs, 'results')
configfile = os.path.join(datadir_abs, 'config.json')
else:
baselinedir = os.path.abspath(args['baseline_data'])
testdir = os.path.abspath(args['test_data'])
resultsdir = os.path.join(os.getcwd(), 'results')
configfile = os.path.abspath(args['config_data'])
with open(configfile, 'r') as f:
config = json.load(f)
if not args['baseline_time']:
try:
args['baseline_time'] = config['baselineendtime'] - config['baselinestarttime']
print("Baseline time in seconds: %s" % str(args['baseline_time']))
except Exception as e:
print("Error while trying to get baseline start times: %s" % str(e))
print("Assuming 10 minute length")
config['baselinestarttime'] = config['starttime']
args['baseline_time'] = 600
if not args['test_time']:
try:
args['test_time'] = config['teststarttime'] - config['testendtime']
print("Testing time in seconds: %s" % str(args['baseline_time']))
except Exception as e:
print("Error while trying to get testing times: %s" % str(e))
print("Assuming 10 minute length...")
args['test_time'] = 600
print("Results will be stored in %s" % resultsdir)
if not os.path.exists(resultsdir):
os.mkdir(resultsdir)
print("Comparing data...")
if args['compare']:
results = compare_data(baselinedir, testdir, config, args)
elif args['compare_to_wpa']:
data = baselinedir
if args['wpa_type'] != 'baseline':
data = testdir
results = compare_to_wpa(data, config, args)
else:
raise Expection("Missing either --compare, or --compare-to-wpa in arguments.")
currtime = str(int(time.time()))
for key in results:
resultscsv = os.path.join(resultsdir, str(key) + currtime + '.csv')
print("Saving results to %s..." % resultscsv)
with open(resultscsv, 'w') as f:
f.write(results[key])
print("Displaying results...")
if args['compare']:
display_results(results)
elif args['compare_to_wpa']:
display_wpa_results(results)
def main():
parser = analysisparser()
args = parser.parse_args()
args = dict(vars(args))
do_comparison(args)
if __name__=="__main__":
main()