~vila/ubuntu-test-cases/retry-apt-get-update

« back to all changes in this revision

Viewing changes to tests/memevent/nfss_upload_results.py

  • Committer: Leo Arias
  • Date: 2014-11-10 19:28:56 UTC
  • mfrom: (345 touch)
  • mto: This revision was merged to the branch mainline in revision 352.
  • Revision ID: leo.arias@canonical.com-20141110192856-rgpksx9n9j0b39yl
Merged with the touch branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
 
 
3
"""This script takes memevent-test result json files and uploads the
 
4
interesting data contained within to a nfss backend.
 
5
 
 
6
It will process any memory result files found in 'source_file_path matching the
 
7
naming scheme: "memory_usage_*.json"
 
8
 
 
9
"""
 
10
 
 
11
import datetime
 
12
import json
 
13
import os
 
14
import re
 
15
import sys
 
16
import subprocess
 
17
 
 
18
from collections import defaultdict
 
19
from glob import glob
 
20
 
 
21
source_file_path = ""
 
22
upload_script = ""
 
23
nfss_config = ""
 
24
 
 
25
 
 
26
def _get_run_details(app_name):
 
27
    return dict(
 
28
        image_arch='',
 
29
        ran_at=datetime.datetime.utcnow().isoformat(),
 
30
        package_version=_get_package_version(app_name),
 
31
        application_name=app_name,
 
32
    )
 
33
 
 
34
 
 
35
def _get_package_version(appname):
 
36
    """Return the package version for application *appname*.
 
37
 
 
38
    Return empty string if appname details are not found.
 
39
 
 
40
    """
 
41
 
 
42
    try:
 
43
        dpkg_output = subprocess.check_output(
 
44
            ['adb', 'shell', 'dpkg', '-s', appname],
 
45
            universal_newlines=True
 
46
        )
 
47
        version_line = [
 
48
            l for l in dpkg_output.split('\n') if l.startswith('Version:')
 
49
        ]
 
50
        return version_line[0].split()[1]
 
51
    except (subprocess.CalledProcessError, IndexError):
 
52
        return "Unknown"
 
53
 
 
54
 
 
55
def upload_json_details(run_details, app_details):
 
56
    app_run_details = run_details.copy()
 
57
    app_run_details["events"] = app_details
 
58
 
 
59
    _upload_data(run_details['application_name'], app_run_details)
 
60
 
 
61
 
 
62
def _upload_data(test_name, run_json):
 
63
    try:
 
64
        run_json_string = json.dumps(run_json)
 
65
    except ValueError as e:
 
66
        print("Error: Data does not appear to be valid json: %s" % e)
 
67
        sys.exit(3)
 
68
 
 
69
    print("Uploading data for :memevent:-", test_name)
 
70
    global upload_script
 
71
    global nfss_config
 
72
    try:
 
73
        upload_process = subprocess.Popen(
 
74
            [upload_script, nfss_config, 'memevent', test_name],
 
75
            stdin=subprocess.PIPE,
 
76
            stdout=subprocess.PIPE,
 
77
            stderr=subprocess.PIPE,
 
78
        )
 
79
        stdout, stderr = upload_process.communicate(
 
80
            input=run_json_string.encode()
 
81
        )
 
82
        print("stdout: {}\n\nstderr: {}".format(stdout, stderr))
 
83
    except Exception as e:
 
84
        print("Something went terribly wrong: ", e)
 
85
    if upload_process.returncode != 0:
 
86
        raise subprocess.CalledProcessError('Failed to upload to nfss')
 
87
 
 
88
 
 
89
def _get_files_app_name_and_test(filename):
 
90
    """return tuple containing (appname, testname)."""
 
91
    app_name_search = re.search(
 
92
        'memory_usage_([a-zA-Z-]*).(.*)\.json',
 
93
        filename
 
94
    )
 
95
    return (app_name_search.group(1), app_name_search.group(2))
 
96
 
 
97
 
 
98
def get_application_readings(json_data):
 
99
    app_results = []
 
100
    pids = json_data["pids"]
 
101
    for reading in json_data["readings"]:
 
102
        results = dict(
 
103
            event=reading["event"],
 
104
            start_time=reading["start_time"],
 
105
            stop_time=reading["stop_time"],
 
106
        )
 
107
        # find the data results for this event (based on pid).
 
108
        for pid in pids:
 
109
            if str(pid) in reading["data"].keys():
 
110
                results["data"] = reading["data"][str(pid)]
 
111
                results["pid"] = pid
 
112
                break
 
113
        app_results.append(results)
 
114
    return app_results
 
115
 
 
116
 
 
117
def get_application_results(json_filepath):
 
118
    with open(json_filepath, "r") as f:
 
119
        json_data = json.load(f)
 
120
    return get_application_readings(json_data)
 
121
 
 
122
 
 
123
def get_application_details(application_files):
 
124
    """For every file this application has grab out the details and return a
 
125
    list dictionaries containing reading details.
 
126
 
 
127
    """
 
128
    app_result = []
 
129
    for json_file in application_files:
 
130
        app_result.extend(get_application_results(json_file))
 
131
    return app_result
 
132
 
 
133
 
 
134
def map_files_to_applications():
 
135
    """For any memory result files that exist, return a dictionary whos keys
 
136
    are the applications name mapped to the file.
 
137
 
 
138
    We can then produce a single json result for each application regardless of
 
139
    there being many tests / results for it.
 
140
 
 
141
    """
 
142
 
 
143
    global source_file_path
 
144
    json_results_filename_pattern = os.path.join(
 
145
        source_file_path,
 
146
        "memory_usage_*.json"
 
147
    )
 
148
    file_map = defaultdict(list)
 
149
    for json_result_file in glob(json_results_filename_pattern):
 
150
        app_name, test_name = _get_files_app_name_and_test(json_result_file)
 
151
        file_map[app_name].append(json_result_file)
 
152
    return file_map
 
153
 
 
154
 
 
155
def usage():
 
156
    print("{} <source file path> <nfss upload script> <nfss config file>"
 
157
          .format(sys.argv[0]))
 
158
 
 
159
 
 
160
def main():
 
161
    if len(sys.argv) != 4:
 
162
        usage()
 
163
        exit(1)
 
164
 
 
165
    global source_file_path
 
166
    source_file_path = sys.argv[1]
 
167
 
 
168
    global upload_script
 
169
    upload_script = sys.argv[2]
 
170
 
 
171
    global nfss_config
 
172
    nfss_config = sys.argv[3]
 
173
 
 
174
    app_details = dict()
 
175
    file_map = map_files_to_applications()
 
176
    for app_name in file_map.keys():
 
177
        app_details[app_name] = get_application_details(file_map[app_name])
 
178
 
 
179
    for app_name in app_details.keys():
 
180
        run_details = _get_run_details(app_name)
 
181
        upload_json_details(run_details, app_details[app_name])
 
182
 
 
183
 
 
184
if __name__ == '__main__':
 
185
    main()