2012-04-12 13:43:08 -04:00
|
|
|
#!@PYTHON@
|
|
|
|
# -*- mode: Python; indent-tabs-mode: nil; -*-
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
from gi.repository import GLib, GObject, Gio
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import base64
|
2014-08-29 10:23:56 -04:00
|
|
|
from configparser import RawConfigParser
|
2012-04-12 13:43:08 -04:00
|
|
|
import hashlib
|
|
|
|
import hmac
|
2014-08-29 10:23:56 -04:00
|
|
|
from http import client
|
|
|
|
from urllib import parse
|
2012-04-12 13:43:08 -04:00
|
|
|
|
|
|
|
def show_version(option, opt_str, value, parser):
|
2014-08-29 10:23:56 -04:00
|
|
|
print("GNOME Shell Performance Test @VERSION@")
|
2012-04-12 13:43:08 -04:00
|
|
|
sys.exit()
|
|
|
|
|
2012-10-31 15:28:55 -04:00
|
|
|
def start_shell(perf_output=None):
|
2012-04-12 13:43:08 -04:00
|
|
|
# Set up environment
|
|
|
|
env = dict(os.environ)
|
|
|
|
env['SHELL_PERF_MODULE'] = options.perf
|
2014-06-05 15:03:03 -04:00
|
|
|
|
|
|
|
filters = ['Gnome-shell-perf-helper'] + options.extra_filter
|
|
|
|
env['MUTTER_WM_CLASS_FILTER'] = ','.join(filters)
|
2012-04-12 13:43:08 -04:00
|
|
|
|
|
|
|
if perf_output is not None:
|
|
|
|
env['SHELL_PERF_OUTPUT'] = perf_output
|
|
|
|
|
2014-08-11 08:35:52 -04:00
|
|
|
# A fixed background image
|
|
|
|
env['SHELL_BACKGROUND_IMAGE'] = '@pkgdatadir@/perf-background.xml'
|
|
|
|
|
2012-04-12 13:43:08 -04:00
|
|
|
self_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
|
2012-10-31 15:28:55 -04:00
|
|
|
args = []
|
2012-04-12 13:43:08 -04:00
|
|
|
args.append(os.path.join(self_dir, 'gnome-shell'))
|
2012-10-31 15:28:55 -04:00
|
|
|
|
|
|
|
if options.replace:
|
|
|
|
args.append('--replace')
|
2012-04-12 13:43:08 -04:00
|
|
|
|
|
|
|
return subprocess.Popen(args, env=env)
|
|
|
|
|
2012-10-31 15:28:55 -04:00
|
|
|
def run_shell(perf_output=None):
|
2012-04-12 13:43:08 -04:00
|
|
|
# we do no additional supervision of gnome-shell,
|
|
|
|
# beyond that of wait
|
|
|
|
# in particular, we don't kill the shell upon
|
|
|
|
# receving a KeyboardInterrupt, as we expect to be
|
|
|
|
# in the same process group
|
2012-10-31 15:28:55 -04:00
|
|
|
shell = start_shell(perf_output=perf_output)
|
2012-04-12 13:43:08 -04:00
|
|
|
shell.wait()
|
|
|
|
return shell.returncode == 0
|
|
|
|
|
2014-02-21 06:30:40 -05:00
|
|
|
def restore_shell():
|
|
|
|
pid = os.fork()
|
|
|
|
if (pid == 0):
|
|
|
|
os.execlp("gnome-shell", "gnome-shell", "--replace")
|
|
|
|
else:
|
|
|
|
sys.exit(0)
|
|
|
|
|
2012-04-12 13:43:08 -04:00
|
|
|
def upload_performance_report(report_text):
|
|
|
|
try:
|
|
|
|
config_home = os.environ['XDG_CONFIG_HOME']
|
|
|
|
except KeyError:
|
|
|
|
config_home = None
|
|
|
|
|
|
|
|
if not config_home:
|
|
|
|
config_home = os.path.expanduser("~/.config")
|
|
|
|
|
|
|
|
config_file = os.path.join(config_home, "gnome-shell/perf.ini")
|
|
|
|
|
|
|
|
try:
|
|
|
|
config = RawConfigParser()
|
|
|
|
f = open(config_file)
|
|
|
|
config.readfp(f)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
base_url = config.get('upload', 'url')
|
|
|
|
system_name = config.get('upload', 'name')
|
|
|
|
secret_key = config.get('upload', 'key')
|
2014-08-29 10:23:56 -04:00
|
|
|
except Exception as e:
|
|
|
|
print("Can't read upload configuration from %s: %s" % (config_file, str(e)))
|
2012-04-12 13:43:08 -04:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Determine host, port and upload URL from provided data, we're
|
|
|
|
# a bit extra-careful about normalization since the URL is part
|
|
|
|
# of the signature.
|
|
|
|
|
2014-08-29 10:23:56 -04:00
|
|
|
split = parse.urlsplit(base_url)
|
2012-04-12 13:43:08 -04:00
|
|
|
scheme = split[0].lower()
|
|
|
|
netloc = split[1]
|
|
|
|
base_path = split[2]
|
|
|
|
|
|
|
|
m = re.match(r'^(.*?)(?::(\d+))?$', netloc)
|
|
|
|
if m.group(2):
|
|
|
|
host, port = m.group(1), int(m.group(2))
|
|
|
|
else:
|
|
|
|
host, port = m.group(1), None
|
|
|
|
|
|
|
|
if scheme != "http":
|
2014-08-29 10:23:56 -04:00
|
|
|
print("'%s' is not a HTTP URL" % base_url)
|
2012-04-12 13:43:08 -04:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if port is None:
|
|
|
|
port = 80
|
|
|
|
|
|
|
|
if base_path.endswith('/'):
|
|
|
|
base_path = base_path[:-1]
|
|
|
|
|
|
|
|
if port == 80:
|
|
|
|
normalized_base = "%s://%s%s" % (scheme, host, base_path)
|
|
|
|
else:
|
|
|
|
normalized_base = "%s://%s:%d%s" % (scheme, host, port, base_path)
|
|
|
|
|
|
|
|
upload_url = normalized_base + '/system/%s/upload' % system_name
|
2014-08-29 10:23:56 -04:00
|
|
|
upload_path = parse.urlsplit(upload_url)[2] # path portion
|
2012-04-12 13:43:08 -04:00
|
|
|
|
|
|
|
# Create signature based on upload URL and the report data
|
|
|
|
|
|
|
|
signature_data = 'POST&' + upload_url + "&&"
|
|
|
|
h = hmac.new(secret_key, digestmod=hashlib.sha1)
|
|
|
|
h.update(signature_data)
|
|
|
|
h.update(report_text)
|
2014-08-29 10:23:56 -04:00
|
|
|
signature = parse.quote(base64.b64encode(h.digest()), "~")
|
2012-04-12 13:43:08 -04:00
|
|
|
|
|
|
|
headers = {
|
|
|
|
'User-Agent': 'gnome-shell-performance-tool/@VERSION@',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Shell-Signature': 'HMAC-SHA1 ' + signature
|
|
|
|
};
|
|
|
|
|
2014-08-29 10:23:56 -04:00
|
|
|
connection = client.HTTPConnection(host, port)
|
2012-04-12 13:43:08 -04:00
|
|
|
connection.request('POST', upload_path, report_text, headers)
|
|
|
|
response = connection.getresponse()
|
|
|
|
|
|
|
|
if response.status == 200:
|
2014-08-29 10:23:56 -04:00
|
|
|
print("Performance report upload succeeded")
|
2012-04-12 13:43:08 -04:00
|
|
|
else:
|
2014-08-29 10:23:56 -04:00
|
|
|
print("Performance report upload failed with status %d" % response.status)
|
|
|
|
print(response.read())
|
2012-04-12 13:43:08 -04:00
|
|
|
|
2014-06-05 15:03:03 -04:00
|
|
|
def gnome_hwtest_log(*args):
|
|
|
|
command = ['gnome-hwtest-log', '-t', 'gnome-shell-perf-tool']
|
|
|
|
command.extend(args)
|
|
|
|
subprocess.check_call(command)
|
|
|
|
|
2012-10-31 15:28:55 -04:00
|
|
|
def run_performance_test():
|
2012-04-12 13:43:08 -04:00
|
|
|
iters = options.perf_iters
|
|
|
|
if options.perf_warmup:
|
|
|
|
iters += 1
|
|
|
|
|
|
|
|
logs = []
|
|
|
|
metric_summaries = {}
|
|
|
|
|
2014-08-29 10:23:56 -04:00
|
|
|
for i in range(0, iters):
|
2012-04-12 13:43:08 -04:00
|
|
|
# We create an empty temporary file that the shell will overwrite
|
|
|
|
# with the contents.
|
|
|
|
handle, output_file = tempfile.mkstemp(".json", "gnome-shell-perf.")
|
|
|
|
os.close(handle)
|
|
|
|
|
|
|
|
# Run the performance test and collect the output as JSON
|
|
|
|
normal_exit = False
|
|
|
|
try:
|
2012-10-31 15:28:55 -04:00
|
|
|
normal_exit = run_shell(perf_output=output_file)
|
2012-04-12 13:43:08 -04:00
|
|
|
except:
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
if not normal_exit:
|
|
|
|
os.remove(output_file)
|
|
|
|
|
|
|
|
if not normal_exit:
|
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
|
|
|
f = open(output_file)
|
|
|
|
output = json.load(f)
|
|
|
|
f.close()
|
|
|
|
except:
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
os.remove(output_file)
|
|
|
|
|
|
|
|
# Grab the event definitions and monitor layout the first time around
|
|
|
|
if i == 0:
|
|
|
|
events = output['events']
|
|
|
|
monitors = output['monitors']
|
|
|
|
|
|
|
|
if options.perf_warmup and i == 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
for metric in output['metrics']:
|
|
|
|
name = metric['name']
|
|
|
|
if not name in metric_summaries:
|
|
|
|
summary = {}
|
|
|
|
summary['description'] = metric['description']
|
|
|
|
summary['units'] = metric['units']
|
|
|
|
summary['values'] = []
|
|
|
|
metric_summaries[name] = summary
|
|
|
|
else:
|
|
|
|
summary = metric_summaries[name]
|
|
|
|
|
|
|
|
summary['values'].append(metric['value'])
|
|
|
|
|
|
|
|
logs.append(output['log'])
|
|
|
|
|
|
|
|
if options.perf_output or options.perf_upload:
|
|
|
|
# Write a complete report, formatted as JSON. The Javascript/C code that
|
|
|
|
# generates the individual reports we are summarizing here is very careful
|
|
|
|
# to format them nicely, but we just dump out a compressed no-whitespace
|
|
|
|
# version here for simplicity. Using json.dump(indent=0) doesn't real
|
|
|
|
# improve the readability of the output much.
|
|
|
|
report = {
|
|
|
|
'date': datetime.datetime.utcnow().isoformat() + 'Z',
|
|
|
|
'events': events,
|
|
|
|
'monitors': monitors,
|
|
|
|
'metrics': metric_summaries,
|
|
|
|
'logs': logs
|
|
|
|
}
|
|
|
|
|
|
|
|
# Add the Git revision if available
|
|
|
|
self_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
|
|
|
|
if os.path.exists(os.path.join(self_dir, 'gnome-shell-jhbuild.in')):
|
|
|
|
top_dir = os.path.dirname(self_dir)
|
|
|
|
git_dir = os.path.join(top_dir, '.git')
|
|
|
|
if os.path.exists(git_dir):
|
|
|
|
env = dict(os.environ)
|
|
|
|
env['GIT_DIR'] = git_dir
|
|
|
|
revision = subprocess.Popen(['git', 'rev-parse', 'HEAD'],
|
|
|
|
env=env,
|
|
|
|
stdout=subprocess.PIPE).communicate()[0].strip()
|
|
|
|
report['revision'] = revision
|
|
|
|
|
|
|
|
if options.perf_output:
|
|
|
|
f = open(options.perf_output, 'w')
|
|
|
|
json.dump(report, f)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
if options.perf_upload:
|
|
|
|
upload_performance_report(json.dumps(report))
|
2014-06-05 15:03:03 -04:00
|
|
|
elif options.hwtest:
|
|
|
|
# Log to systemd journal
|
|
|
|
for metric in sorted(metric_summaries.keys()):
|
|
|
|
summary = metric_summaries[metric]
|
|
|
|
gnome_hwtest_log('--metric=' + metric + '=' + str(summary['values'][0]) + summary['units'],
|
|
|
|
'--metric-description=' + summary['description'])
|
|
|
|
gnome_hwtest_log('--finished')
|
2012-04-12 13:43:08 -04:00
|
|
|
else:
|
|
|
|
# Write a human readable summary
|
2014-08-29 10:23:56 -04:00
|
|
|
print('------------------------------------------------------------')
|
2012-04-12 13:43:08 -04:00
|
|
|
for metric in sorted(metric_summaries.keys()):
|
|
|
|
summary = metric_summaries[metric]
|
2014-08-29 10:23:56 -04:00
|
|
|
print("#", summary['description'])
|
|
|
|
print(metric, ", ".join((str(x) for x in summary['values'])))
|
|
|
|
print('------------------------------------------------------------')
|
2012-04-12 13:43:08 -04:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Main program
|
|
|
|
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option("", "--perf", metavar="PERF_MODULE",
|
2014-06-05 15:03:03 -04:00
|
|
|
help="Specify the name of a performance module to run")
|
2012-04-12 13:43:08 -04:00
|
|
|
parser.add_option("", "--perf-iters", type="int", metavar="ITERS",
|
|
|
|
help="Numbers of iterations of performance module to run",
|
|
|
|
default=1)
|
|
|
|
parser.add_option("", "--perf-warmup", action="store_true",
|
|
|
|
help="Run a dry run before performance tests")
|
|
|
|
parser.add_option("", "--perf-output", metavar="OUTPUT_FILE",
|
|
|
|
help="Output file to write performance report")
|
|
|
|
parser.add_option("", "--perf-upload", action="store_true",
|
|
|
|
help="Upload performance report to server")
|
2014-06-05 15:03:03 -04:00
|
|
|
parser.add_option("", "--extra-filter", action="append",
|
|
|
|
help="add an extra window class that should be allowed")
|
|
|
|
parser.add_option("", "--hwtest", action="store_true",
|
|
|
|
help="Log results appropriately for GNOME Hardware Testing")
|
2012-04-12 13:43:08 -04:00
|
|
|
parser.add_option("", "--version", action="callback", callback=show_version,
|
|
|
|
help="Display version and exit")
|
|
|
|
|
2012-10-31 15:28:55 -04:00
|
|
|
parser.add_option("-r", "--replace", action="store_true",
|
|
|
|
help="Replace the running window manager")
|
|
|
|
|
2012-04-12 13:43:08 -04:00
|
|
|
options, args = parser.parse_args()
|
|
|
|
|
2014-06-05 15:03:03 -04:00
|
|
|
if options.perf == None:
|
|
|
|
if options.hwtest:
|
|
|
|
options.perf = 'hwtest'
|
|
|
|
else:
|
|
|
|
options.perf = 'core'
|
|
|
|
|
|
|
|
if options.extra_filter is None:
|
2018-04-29 12:03:40 -04:00
|
|
|
options.extra_filter = []
|
|
|
|
|
|
|
|
if options.perf == 'hwtest':
|
|
|
|
options.extra_filter.append('Gedit')
|
2014-06-05 15:03:03 -04:00
|
|
|
|
2012-10-31 15:28:55 -04:00
|
|
|
if args:
|
|
|
|
parser.print_usage()
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
normal_exit = run_performance_test()
|
2012-04-12 13:43:08 -04:00
|
|
|
if normal_exit:
|
2014-06-05 15:03:03 -04:00
|
|
|
if not options.hwtest:
|
|
|
|
restore_shell()
|
2012-04-12 13:43:08 -04:00
|
|
|
else:
|
|
|
|
sys.exit(1)
|