2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 14:07:06 +00:00
|
|
|
/* exported sleep, waitLeisure, createTestWindow, waitTestWindows,
|
|
|
|
destroyTestWindows, defineScriptEvent, scriptEvent,
|
2023-03-24 09:21:38 +00:00
|
|
|
collectStatistics, runPerfScript, disableHelperAutoExit */
|
2010-05-09 17:42:35 +00:00
|
|
|
|
2019-02-09 03:21:36 +00:00
|
|
|
const { Gio, GLib, Meta, Shell } = imports.gi;
|
2010-05-09 17:42:35 +00:00
|
|
|
|
2020-01-24 09:59:31 +00:00
|
|
|
const Config = imports.misc.config;
|
2011-06-13 13:54:05 +00:00
|
|
|
const Main = imports.ui.main;
|
2014-05-20 01:00:01 +00:00
|
|
|
const Params = imports.misc.params;
|
2020-01-24 09:59:31 +00:00
|
|
|
const Util = imports.misc.util;
|
2011-06-13 13:54:05 +00:00
|
|
|
|
2018-09-06 00:55:20 +00:00
|
|
|
const { loadInterfaceXML } = imports.misc.fileUtils;
|
|
|
|
|
2010-05-09 17:42:35 +00:00
|
|
|
// This module provides functionality for driving the shell user interface
|
|
|
|
// in an automated fashion. The primary current use case for this is
|
|
|
|
// automated performance testing (see runPerfScript()), but it could
|
|
|
|
// be applied to other forms of automation, such as testing for
|
|
|
|
// correctness as well.
|
|
|
|
//
|
|
|
|
// When scripting an automated test we want to make a series of calls
|
|
|
|
// in a linear fashion, but we also want to be able to let the main
|
|
|
|
// loop run so actions can finish. For this reason we write the script
|
2020-07-30 16:41:11 +00:00
|
|
|
// as an async function that uses await when it wants to let the main
|
2010-05-09 17:42:35 +00:00
|
|
|
// loop run.
|
|
|
|
//
|
2020-07-30 16:41:11 +00:00
|
|
|
// await Scripting.sleep(1000);
|
2010-05-09 17:42:35 +00:00
|
|
|
// main.overview.show();
|
2020-07-30 16:41:11 +00:00
|
|
|
// await Scripting.waitLeisure();
|
2010-05-09 17:42:35 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sleep:
|
2019-10-17 16:41:52 +00:00
|
|
|
* @param {number} milliseconds - number of milliseconds to wait
|
|
|
|
* @returns {Promise} that resolves after @milliseconds ms
|
2010-05-09 17:42:35 +00:00
|
|
|
*
|
|
|
|
* Used within an automation script to pause the the execution of the
|
|
|
|
* current script for the specified amount of time. Use as
|
|
|
|
* 'yield Scripting.sleep(500);'
|
|
|
|
*/
|
|
|
|
function sleep(milliseconds) {
|
2019-02-12 16:11:24 +00:00
|
|
|
return new Promise(resolve => {
|
2019-08-19 18:50:33 +00:00
|
|
|
let id = GLib.timeout_add(GLib.PRIORITY_DEFAULT, milliseconds, () => {
|
2019-02-12 16:11:24 +00:00
|
|
|
resolve();
|
|
|
|
return GLib.SOURCE_REMOVE;
|
|
|
|
});
|
|
|
|
GLib.Source.set_name_by_id(id, '[gnome-shell] sleep');
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2010-05-09 17:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* waitLeisure:
|
2019-10-17 16:41:52 +00:00
|
|
|
* @returns {Promise} that resolves when the shell is idle
|
2010-05-09 17:42:35 +00:00
|
|
|
*
|
|
|
|
* Used within an automation script to pause the the execution of the
|
|
|
|
* current script until the shell is completely idle. Use as
|
|
|
|
* 'yield Scripting.waitLeisure();'
|
|
|
|
*/
|
|
|
|
function waitLeisure() {
|
2019-02-12 16:11:24 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
global.run_at_leisure(resolve);
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2010-05-09 17:42:35 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 00:55:20 +00:00
|
|
|
const PerfHelperIface = loadInterfaceXML('org.gnome.Shell.PerfHelper');
|
2011-08-16 12:28:53 +00:00
|
|
|
var PerfHelperProxy = Gio.DBusProxy.makeProxyWrapper(PerfHelperIface);
|
2011-03-08 23:48:07 +00:00
|
|
|
|
|
|
|
let _perfHelper = null;
|
2023-05-27 01:19:39 +00:00
|
|
|
/** private */
|
|
|
|
async function _getPerfHelper() {
|
2023-03-24 09:21:38 +00:00
|
|
|
if (_perfHelper == null) {
|
2023-05-27 01:19:39 +00:00
|
|
|
_perfHelper = await PerfHelperProxy.newAsync(
|
|
|
|
Gio.DBus.session, 'org.gnome.Shell.PerfHelper', '/org/gnome/Shell/PerfHelper');
|
2023-03-24 09:21:38 +00:00
|
|
|
_perfHelper._autoExit = true;
|
|
|
|
}
|
2011-03-08 23:48:07 +00:00
|
|
|
|
|
|
|
return _perfHelper;
|
|
|
|
}
|
|
|
|
|
2020-01-24 09:59:31 +00:00
|
|
|
function _spawnPerfHelper() {
|
2022-08-17 14:43:13 +00:00
|
|
|
let path = GLib.getenv('GNOME_SHELL_BUILDDIR') || Config.LIBEXECDIR;
|
2020-01-24 09:59:31 +00:00
|
|
|
let command = `${path}/gnome-shell-perf-helper`;
|
|
|
|
Util.trySpawnCommandLine(command);
|
|
|
|
}
|
|
|
|
|
2011-03-08 23:48:07 +00:00
|
|
|
/**
|
|
|
|
* createTestWindow:
|
2019-10-17 16:41:52 +00:00
|
|
|
* @param {Object} params: options for window creation.
|
|
|
|
* {number} [params.width=640] - width of window, in pixels
|
|
|
|
* {number} [params.height=480] - height of window, in pixels
|
|
|
|
* {bool} [params.alpha=false] - whether the window should have an alpha channel
|
|
|
|
* {bool} [params.maximized=false] - whether the window should be created maximized
|
|
|
|
* {bool} [params.redraws=false] - whether the window should continually redraw itself
|
|
|
|
* @returns {Promise}
|
2011-03-08 23:48:07 +00:00
|
|
|
*
|
|
|
|
* Creates a window using gnome-shell-perf-helper for testing purposes.
|
|
|
|
* While this function can be used with yield in an automation
|
|
|
|
* script to pause until the D-Bus call to the helper process returns,
|
|
|
|
* because of the normal X asynchronous mapping process, to actually wait
|
|
|
|
* until the window has been mapped and exposed, use waitTestWindows().
|
|
|
|
*/
|
2023-05-27 01:19:39 +00:00
|
|
|
async function createTestWindow(params) {
|
2020-03-29 21:51:13 +00:00
|
|
|
params = Params.parse(params, {
|
|
|
|
width: 640,
|
|
|
|
height: 480,
|
|
|
|
alpha: false,
|
|
|
|
maximized: false,
|
|
|
|
redraws: false,
|
2023-03-24 10:26:05 +00:00
|
|
|
textInput: false,
|
2020-03-29 21:51:13 +00:00
|
|
|
});
|
2014-05-20 01:00:01 +00:00
|
|
|
|
2023-05-27 01:19:39 +00:00
|
|
|
let perfHelper = await _getPerfHelper();
|
2022-06-23 12:53:29 +00:00
|
|
|
perfHelper.CreateWindowAsync(
|
|
|
|
params.width, params.height,
|
2023-03-24 10:26:05 +00:00
|
|
|
params.alpha, params.maximized,
|
|
|
|
params.redraws, params.textInput).catch(logError);
|
2011-03-08 23:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* waitTestWindows:
|
2019-10-17 16:41:52 +00:00
|
|
|
* @returns {Promise}
|
2011-03-08 23:48:07 +00:00
|
|
|
*
|
|
|
|
* Used within an automation script to pause until all windows previously
|
|
|
|
* created with createTestWindow have been mapped and exposed.
|
|
|
|
*/
|
2023-05-27 01:19:39 +00:00
|
|
|
async function waitTestWindows() {
|
|
|
|
let perfHelper = await _getPerfHelper();
|
2023-04-18 14:27:58 +00:00
|
|
|
return perfHelper.WaitWindowsAsync().catch(logError);
|
2011-03-08 23:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* destroyTestWindows:
|
2019-10-17 16:41:52 +00:00
|
|
|
* @returns {Promise}
|
2011-03-08 23:48:07 +00:00
|
|
|
*
|
|
|
|
* Destroys all windows previously created with createTestWindow().
|
|
|
|
* While this function can be used with yield in an automation
|
|
|
|
* script to pause until the D-Bus call to the helper process returns,
|
|
|
|
* this doesn't guarantee that Mutter has actually finished the destroy
|
|
|
|
* process because of normal X asynchronicity.
|
|
|
|
*/
|
2023-05-27 01:19:39 +00:00
|
|
|
async function destroyTestWindows() {
|
|
|
|
let perfHelper = await _getPerfHelper();
|
2023-04-18 14:27:58 +00:00
|
|
|
return perfHelper.DestroyWindowsAsync().catch(logError);
|
2011-03-08 23:48:07 +00:00
|
|
|
}
|
|
|
|
|
2023-03-24 09:21:38 +00:00
|
|
|
/**
|
|
|
|
* disableHelperAutoExit:
|
|
|
|
*
|
|
|
|
* Don't exixt the perf helper after running the script. Instead it will remain
|
|
|
|
* running until something else makes it exit, e.g. the Wayland socket closing.
|
|
|
|
*/
|
2023-05-27 01:19:39 +00:00
|
|
|
async function disableHelperAutoExit() {
|
|
|
|
let perfHelper = await _getPerfHelper();
|
2023-03-24 09:21:38 +00:00
|
|
|
perfHelper._autoExit = false;
|
|
|
|
}
|
|
|
|
|
2010-05-09 17:42:35 +00:00
|
|
|
/**
|
|
|
|
* defineScriptEvent
|
2019-10-17 16:41:52 +00:00
|
|
|
* @param {string} name: The event will be called script.<name>
|
|
|
|
* @param {string} description: Short human-readable description of the event
|
2010-05-09 17:42:35 +00:00
|
|
|
*
|
|
|
|
* Convenience function to define a zero-argument performance event
|
|
|
|
* within the 'script' namespace that is reserved for events defined locally
|
|
|
|
* within a performance automation script
|
|
|
|
*/
|
|
|
|
function defineScriptEvent(name, description) {
|
2019-01-30 00:18:24 +00:00
|
|
|
Shell.PerfLog.get_default().define_event(`script.${name}`,
|
2010-05-09 17:42:35 +00:00
|
|
|
description,
|
|
|
|
"");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scriptEvent
|
2019-10-17 16:41:52 +00:00
|
|
|
* @param {string} name: Name registered with defineScriptEvent()
|
2010-05-09 17:42:35 +00:00
|
|
|
*
|
|
|
|
* Convenience function to record a script-local performance event
|
|
|
|
* previously defined with defineScriptEvent
|
|
|
|
*/
|
|
|
|
function scriptEvent(name) {
|
2019-01-30 00:18:24 +00:00
|
|
|
Shell.PerfLog.get_default().event(`script.${name}`);
|
2010-05-09 17:42:35 +00:00
|
|
|
}
|
|
|
|
|
2010-05-11 19:53:55 +00:00
|
|
|
/**
|
|
|
|
* collectStatistics
|
|
|
|
*
|
|
|
|
* Convenience function to trigger statistics collection
|
|
|
|
*/
|
|
|
|
function collectStatistics() {
|
|
|
|
Shell.PerfLog.get_default().collect_statistics();
|
|
|
|
}
|
|
|
|
|
2010-05-09 17:42:35 +00:00
|
|
|
function _collect(scriptModule, outputFile) {
|
|
|
|
let eventHandlers = {};
|
|
|
|
|
|
|
|
for (let f in scriptModule) {
|
|
|
|
let m = /([A-Za-z]+)_([A-Za-z]+)/.exec(f);
|
|
|
|
if (m)
|
2019-01-30 00:18:24 +00:00
|
|
|
eventHandlers[`${m[1]}.${m[2]}`] = scriptModule[f];
|
2010-05-09 17:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Shell.PerfLog.get_default().replay(
|
2017-10-31 00:38:18 +00:00
|
|
|
(time, eventName, signature, arg) => {
|
2010-05-09 17:42:35 +00:00
|
|
|
if (eventName in eventHandlers)
|
|
|
|
eventHandlers[eventName](time, arg);
|
|
|
|
});
|
|
|
|
|
|
|
|
if ('finish' in scriptModule)
|
|
|
|
scriptModule.finish();
|
|
|
|
|
|
|
|
if (outputFile) {
|
2010-05-12 21:24:52 +00:00
|
|
|
let f = Gio.file_new_for_path(outputFile);
|
|
|
|
let raw = f.replace(null, false,
|
|
|
|
Gio.FileCreateFlags.NONE,
|
|
|
|
null);
|
2019-08-19 17:55:49 +00:00
|
|
|
let out = Gio.BufferedOutputStream.new_sized(raw, 4096);
|
|
|
|
Shell.write_string_to_stream(out, "{\n");
|
2010-05-12 21:24:52 +00:00
|
|
|
|
|
|
|
Shell.write_string_to_stream(out, '"events":\n');
|
|
|
|
Shell.PerfLog.get_default().dump_events(out);
|
|
|
|
|
2011-06-13 13:54:05 +00:00
|
|
|
let monitors = Main.layoutManager.monitors;
|
|
|
|
let primary = Main.layoutManager.primaryIndex;
|
2010-05-18 23:13:56 +00:00
|
|
|
Shell.write_string_to_stream(out, ',\n"monitors":\n[');
|
|
|
|
for (let i = 0; i < monitors.length; i++) {
|
|
|
|
let monitor = monitors[i];
|
|
|
|
if (i != 0)
|
|
|
|
Shell.write_string_to_stream(out, ', ');
|
2022-02-07 14:14:06 +00:00
|
|
|
const prefix = i === primary ? '*' : '';
|
|
|
|
Shell.write_string_to_stream(out,
|
|
|
|
`"${prefix}${monitor.width}x${monitor.height}+${monitor.x}+${monitor.y}"`);
|
2010-05-18 23:13:56 +00:00
|
|
|
}
|
|
|
|
Shell.write_string_to_stream(out, ' ]');
|
|
|
|
|
2010-05-12 21:24:52 +00:00
|
|
|
Shell.write_string_to_stream(out, ',\n"metrics":\n[ ');
|
|
|
|
let first = true;
|
|
|
|
for (let name in scriptModule.METRICS) {
|
2011-03-08 23:51:15 +00:00
|
|
|
let metric = scriptModule.METRICS[name];
|
|
|
|
// Extra checks here because JSON.stringify generates
|
|
|
|
// invalid JSON for undefined values
|
|
|
|
if (metric.description == null) {
|
2019-01-30 00:18:24 +00:00
|
|
|
log(`Error: No description found for metric ${name}`);
|
2011-03-08 23:51:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (metric.units == null) {
|
2019-01-30 00:18:24 +00:00
|
|
|
log(`Error: No units found for metric ${name}`);
|
2011-03-08 23:51:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (metric.value == null) {
|
2019-01-30 00:18:24 +00:00
|
|
|
log(`Error: No value found for metric ${name}`);
|
2011-03-08 23:51:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-05-12 21:24:52 +00:00
|
|
|
|
|
|
|
if (!first)
|
|
|
|
Shell.write_string_to_stream(out, ',\n ');
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
Shell.write_string_to_stream(out,
|
2019-01-30 00:18:24 +00:00
|
|
|
`{ "name": ${JSON.stringify(name)},\n` +
|
|
|
|
` "description": ${JSON.stringify(metric.description)},\n` +
|
|
|
|
` "units": ${JSON.stringify(metric.units)},\n` +
|
|
|
|
` "value": ${JSON.stringify(metric.value)} }`);
|
2010-05-09 17:42:35 +00:00
|
|
|
}
|
2010-05-12 21:24:52 +00:00
|
|
|
Shell.write_string_to_stream(out, ' ]');
|
2010-05-09 17:42:35 +00:00
|
|
|
|
2019-08-19 17:55:49 +00:00
|
|
|
Shell.write_string_to_stream(out, ',\n"log":\n');
|
2010-05-12 21:24:52 +00:00
|
|
|
Shell.PerfLog.get_default().dump_log(out);
|
|
|
|
|
2019-08-19 17:55:49 +00:00
|
|
|
Shell.write_string_to_stream(out, '\n}\n');
|
2010-05-12 21:24:52 +00:00
|
|
|
out.close(null);
|
2010-05-09 17:42:35 +00:00
|
|
|
} else {
|
|
|
|
let metrics = [];
|
|
|
|
for (let metric in scriptModule.METRICS)
|
|
|
|
metrics.push(metric);
|
|
|
|
|
|
|
|
metrics.sort();
|
|
|
|
|
2019-08-19 17:55:49 +00:00
|
|
|
print('------------------------------------------------------------');
|
2010-05-09 17:42:35 +00:00
|
|
|
for (let i = 0; i < metrics.length; i++) {
|
|
|
|
let metric = metrics[i];
|
2019-08-19 17:55:49 +00:00
|
|
|
print(`# ${scriptModule.METRICS[metric].description}`);
|
|
|
|
print(`${metric}: ${scriptModule.METRICS[metric].value}${scriptModule.METRICS[metric].units}`);
|
2010-05-09 17:42:35 +00:00
|
|
|
}
|
2019-08-19 17:55:49 +00:00
|
|
|
print('------------------------------------------------------------');
|
2010-05-09 17:42:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 09:59:31 +00:00
|
|
|
async function _runPerfScript(scriptModule, outputFile) {
|
2020-07-30 16:41:11 +00:00
|
|
|
try {
|
|
|
|
await scriptModule.run();
|
|
|
|
} catch (err) {
|
|
|
|
log(`Script failed: ${err}\n${err.stack}`);
|
|
|
|
Meta.exit(Meta.ExitCode.ERROR);
|
2020-01-24 09:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
_collect(scriptModule, outputFile);
|
|
|
|
} catch (err) {
|
|
|
|
log(`Script failed: ${err}\n${err.stack}`);
|
|
|
|
Meta.exit(Meta.ExitCode.ERROR);
|
|
|
|
}
|
2022-08-16 10:40:57 +00:00
|
|
|
|
|
|
|
try {
|
2023-05-27 01:19:39 +00:00
|
|
|
const perfHelper = await _getPerfHelper();
|
2023-03-24 09:21:38 +00:00
|
|
|
if (perfHelper._autoExit)
|
|
|
|
perfHelper.ExitSync();
|
2022-08-16 10:40:57 +00:00
|
|
|
} catch (err) {
|
|
|
|
log(`Failed to exit helper: ${err}\n${err.stack}`);
|
|
|
|
Meta.exit(Meta.ExitCode.ERROR);
|
|
|
|
}
|
|
|
|
|
2022-08-16 10:47:55 +00:00
|
|
|
global.context.terminate();
|
2020-01-24 09:59:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-09 17:42:35 +00:00
|
|
|
/**
|
|
|
|
* runPerfScript
|
2019-10-17 16:41:52 +00:00
|
|
|
* @param {Object} scriptModule: module object with run and finish
|
|
|
|
* functions and event handlers
|
|
|
|
* @param {string} outputFile: path to write output to
|
2010-05-09 17:42:35 +00:00
|
|
|
*
|
|
|
|
* Runs a script for automated collection of performance data. The
|
|
|
|
* script is defined as a Javascript module with specified contents.
|
|
|
|
*
|
|
|
|
* First the run() function within the module will be called as a
|
|
|
|
* generator to automate a series of actions. These actions will
|
|
|
|
* trigger performance events and the script can also record its
|
|
|
|
* own performance events.
|
|
|
|
*
|
|
|
|
* Then the recorded event log is replayed using handler functions
|
|
|
|
* within the module. The handler for the event 'foo.bar' is called
|
|
|
|
* foo_bar().
|
|
|
|
*
|
|
|
|
* Finally if the module has a function called finish(), that will
|
|
|
|
* be called.
|
|
|
|
*
|
|
|
|
* The event handler and finish functions are expected to fill in
|
2010-05-17 18:04:09 +00:00
|
|
|
* metrics to an object within the module called METRICS. Each
|
|
|
|
* property of this object represents an individual metric. The
|
|
|
|
* name of the property is the name of the metric, the value
|
|
|
|
* of the property is an object with the following properties:
|
|
|
|
*
|
|
|
|
* description: human readable description of the metric
|
|
|
|
* units: a string representing the units of the metric. It has
|
|
|
|
* the form '<unit> <unit> ... / <unit> / <unit> ...'. Certain
|
|
|
|
* unit values are recognized: s, ms, us, B, KiB, MiB. Other
|
|
|
|
* values can appear but are uninterpreted. Examples 's',
|
|
|
|
* '/ s', 'frames', 'frames / s', 'MiB / s / frame'
|
|
|
|
* value: computed value of the metric
|
2010-05-09 17:42:35 +00:00
|
|
|
*
|
|
|
|
* The resulting metrics will be written to @outputFile as JSON, or,
|
|
|
|
* if @outputFile is not provided, logged.
|
|
|
|
*
|
|
|
|
* After running the script and collecting statistics from the
|
|
|
|
* event log, GNOME Shell will exit.
|
|
|
|
**/
|
2020-01-24 09:59:31 +00:00
|
|
|
function runPerfScript(scriptModule, outputFile) {
|
2010-05-09 17:42:35 +00:00
|
|
|
Shell.PerfLog.get_default().set_enabled(true);
|
2020-01-24 09:59:31 +00:00
|
|
|
_spawnPerfHelper();
|
2010-05-09 17:42:35 +00:00
|
|
|
|
2020-01-24 09:59:31 +00:00
|
|
|
Gio.bus_watch_name(Gio.BusType.SESSION,
|
|
|
|
'org.gnome.Shell.PerfHelper',
|
|
|
|
Gio.BusNameWatcherFlags.NONE,
|
|
|
|
() => _runPerfScript(scriptModule, outputFile),
|
|
|
|
null);
|
2010-05-09 17:42:35 +00:00
|
|
|
}
|