~canonical-platform-qa/ubuntu-test-cases/memevent

« back to all changes in this revision

Viewing changes to tests/memevent/support_scripts/generate_nfss_result_file.py

  • Committer: Christopher Lee
  • Date: 2014-11-13 04:34:55 UTC
  • mfrom: (335.1.11 memevent-dep8)
  • Revision ID: chris.lee@canonical.com-20141113043455-3bc7f6l5xindv3w5
Merge dep8 and nfss script work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import sys
20
20
import subprocess
21
21
 
 
22
from autopilot import platform
22
23
from collections import defaultdict
23
24
from glob import glob
24
25
 
25
26
 
 
27
def usage(extra_msg=None):
 
28
    print(
 
29
        '{} <source file path> <destination path for results>'.format(
 
30
            sys.argv[0]
 
31
        )
 
32
    )
 
33
 
 
34
    if extra_msg:
 
35
        print(extra_msg)
 
36
 
 
37
 
26
38
def _get_run_details(app_name):
27
39
    return dict(
28
40
        image_arch='',
40
52
    """
41
53
 
42
54
    try:
 
55
        dpkg_cmd = _get_dpkg_command(appname)
43
56
        dpkg_output = subprocess.check_output(
44
 
            ['adb', 'shell', 'dpkg', '-s', appname],
 
57
            dpkg_cmd,
45
58
            universal_newlines=True
46
59
        )
47
60
        version_line = [
49
62
        ]
50
63
        return version_line[0].split()[1]
51
64
    except (subprocess.CalledProcessError, IndexError):
52
 
        return "Unknown"
 
65
        return 'Unknown'
 
66
 
 
67
 
 
68
def _get_dpkg_command(appname):
 
69
    return ['dpkg', '-s', appname]
53
70
 
54
71
 
55
72
def _get_files_app_name_and_test(filename):
58
75
        'memory_usage_([a-zA-Z-]*).(.*)\.json',
59
76
        filename
60
77
    )
61
 
    if "" in app_name_search.groups():
62
 
        raise ValueError("Cannot find app and/or test name: %s" % filename)
 
78
    if '' in app_name_search.groups():
 
79
        raise ValueError('Cannot find app and/or test name: %s' % filename)
63
80
    return (app_name_search.group(1), app_name_search.group(2))
64
81
 
65
82
 
66
83
def get_application_readings(json_data):
67
84
    """Returns a list of result event data."""
68
85
    app_results = []
69
 
    pids = json_data["pids"]
70
 
    for reading in json_data["readings"]:
 
86
    pids = json_data['pids']
 
87
    for reading in json_data['readings']:
71
88
        results = dict(
72
 
            event=reading["event"],
73
 
            start_time=reading["start_time"],
74
 
            stop_time=reading["stop_time"],
 
89
            event=reading['event'],
 
90
            start_time=reading['start_time'],
 
91
            stop_time=reading['stop_time'],
75
92
        )
76
93
        # find the data results for this event (based on pid).
77
94
        for pid in pids:
78
 
            if str(pid) in reading["data"].keys():
79
 
                results["data"] = reading["data"][str(pid)]
80
 
                results["pid"] = pid
 
95
            if str(pid) in reading['data'].keys():
 
96
                results['data'] = reading['data'][str(pid)]
 
97
                results['pid'] = pid
81
98
                break
82
99
        app_results.append(results)
83
100
    return app_results
85
102
 
86
103
def get_application_results(json_filepath):
87
104
    """Load a json file into memory, return the a list of event readings from
88
 
    it."""
89
 
    with open(json_filepath, "r") as f:
 
105
    it.
 
106
 
 
107
    """
 
108
    with open(json_filepath, 'r') as f:
90
109
        json_data = json.load(f)
91
110
    return get_application_readings(json_data)
92
111
 
134
153
 
135
154
def get_test_result_files(source_file_path):
136
155
    json_results_filename_pattern = os.path.join(
137
 
            source_file_path,
138
 
            "memory_usage_*.json"
139
 
        )
 
156
        source_file_path,
 
157
        'memory_usage_*.json'
 
158
    )
140
159
    return glob(json_results_filename_pattern)
141
160
 
142
161
 
143
 
def usage(extra_msg=None):
144
 
    print("{} <source file path> <destination file>".format(sys.argv[0]))
145
 
    if extra_msg:
146
 
        print(extra_msg)
147
 
 
148
 
 
149
162
def raise_on_invalid_args(argv):
150
163
    if len(argv) != 3:
151
 
        raise RuntimeError("Not enough arguments")
 
164
        raise RuntimeError('Not enough arguments')
152
165
 
153
166
 
154
167
def parse_args(argv):
157
170
        destination_file = argv[2]
158
171
        return (source_file_path, destination_file)
159
172
    except IndexError:
160
 
        raise RuntimeError("Need both source path and destination file.")
 
173
        raise RuntimeError('Need both source path and destination file.')
 
174
 
 
175
 
 
176
def write_data_to_file(destination_path, run_data):
 
177
    data_file_name = 'nfss-data.json'
 
178
    if platform.image_codename() == 'krillin':
 
179
        data_file_name = 'private-nfss-data.json'
 
180
 
 
181
    file_output = os.path.join(destination_path, data_file_name)
 
182
    print('Writing results to %s' % (file_output))
 
183
    with open(file_output, 'w') as f:
 
184
        json.dump(run_data, f)
161
185
 
162
186
 
163
187
def main():
164
188
    try:
165
189
        raise_on_invalid_args(sys.argv)
166
 
        source_file_path, destination_file = parse_args(sys.argv)
 
190
        source_file_path, destination_path = parse_args(sys.argv)
167
191
    except RuntimeError as e:
168
192
        usage(e)
169
193
        exit(1)
171
195
    test_result_files = get_test_result_files(source_file_path)
172
196
    all_app_details = get_all_applications_details(test_result_files)
173
197
 
174
 
    # all_app_details is a map of ['appname'] => [list|event data]
 
198
    all_test_data = []
175
199
    for app_name in all_app_details.keys():
176
200
        run_details = _get_run_details(app_name)
177
 
        run_details["events"] = all_app_details[app_name]
178
 
        # write to file for application.
179
 
        file_output = os.path.join(
180
 
            destination_file,
181
 
            "{app_name}_results.json".format(app_name=app_name)
 
201
        run_details['events'] = all_app_details[app_name]
 
202
        project_details = dict(
 
203
            project='memevent',
 
204
            test=app_name,
 
205
            data=run_details
182
206
        )
183
 
        print("Writing results for %s to %s" %(app_name,  file_output))
184
 
        with open(file_output, "w") as f:
185
 
            json.dump(run_details, f)
 
207
        all_test_data.append(project_details)
 
208
 
 
209
    write_data_to_file(destination_path, all_test_data)
186
210
 
187
211
 
188
212
if __name__ == '__main__':
 
213
    print('Generating NFSS data file.')
189
214
    main()