diff options
Diffstat (limited to 'toolkit/components/perf')
-rw-r--r-- | toolkit/components/perf/.eslintrc.js | 7 | ||||
-rw-r--r-- | toolkit/components/perf/PerfMeasurement.cpp | 120 | ||||
-rw-r--r-- | toolkit/components/perf/PerfMeasurement.h | 30 | ||||
-rw-r--r-- | toolkit/components/perf/PerfMeasurement.jsm | 19 | ||||
-rw-r--r-- | toolkit/components/perf/chrome.ini | 3 | ||||
-rw-r--r-- | toolkit/components/perf/moz.build | 21 | ||||
-rw-r--r-- | toolkit/components/perf/test_pm.xul | 48 |
7 files changed, 248 insertions, 0 deletions
diff --git a/toolkit/components/perf/.eslintrc.js b/toolkit/components/perf/.eslintrc.js new file mode 100644 index 000000000..4e6d4bcf0 --- /dev/null +++ b/toolkit/components/perf/.eslintrc.js @@ -0,0 +1,7 @@ +"use strict"; + +module.exports = { + "extends": [ + "../../../testing/mochitest/chrome.eslintrc.js" + ] +}; diff --git a/toolkit/components/perf/PerfMeasurement.cpp b/toolkit/components/perf/PerfMeasurement.cpp new file mode 100644 index 000000000..1b211b79c --- /dev/null +++ b/toolkit/components/perf/PerfMeasurement.cpp @@ -0,0 +1,120 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "PerfMeasurement.h" +#include "jsperf.h" +#include "mozilla/ModuleUtils.h" +#include "nsMemory.h" +#include "mozilla/Preferences.h" +#include "mozJSComponentLoader.h" +#include "nsZipArchive.h" +#include "xpc_make_class.h" + +#define JSPERF_CONTRACTID \ + "@mozilla.org/jsperf;1" + +#define JSPERF_CID \ +{ 0x421c38e6, 0xaee0, 0x4509, \ + { 0xa0, 0x25, 0x13, 0x0f, 0x43, 0x78, 0x03, 0x5a } } + +namespace mozilla { +namespace jsperf { + +NS_GENERIC_FACTORY_CONSTRUCTOR(Module) + +NS_IMPL_ISUPPORTS(Module, nsIXPCScriptable) + +Module::Module() +{ +} + +Module::~Module() +{ +} + +#define XPC_MAP_CLASSNAME Module +#define XPC_MAP_QUOTED_CLASSNAME "Module" +#define XPC_MAP_WANT_CALL +#define XPC_MAP_FLAGS nsIXPCScriptable::WANT_CALL +#include "xpc_map_end.h" + +static bool +SealObjectAndPrototype(JSContext* cx, JS::Handle<JSObject *> parent, const char* name) +{ + JS::Rooted<JS::Value> prop(cx); + if (!JS_GetProperty(cx, parent, name, &prop)) + return false; + + if (prop.isUndefined()) { + // Pretend we sealed the object. + return true; + } + + JS::Rooted<JSObject*> obj(cx, prop.toObjectOrNull()); + if (!JS_GetProperty(cx, obj, "prototype", &prop)) + return false; + + JS::Rooted<JSObject*> prototype(cx, prop.toObjectOrNull()); + return JS_FreezeObject(cx, obj) && JS_FreezeObject(cx, prototype); +} + +static bool +InitAndSealPerfMeasurementClass(JSContext* cx, JS::Handle<JSObject*> global) +{ + // Init the PerfMeasurement class + if (!JS::RegisterPerfMeasurement(cx, global)) + return false; + + // Seal up Object, Function, and Array and their prototypes. (This single + // object instance is shared amongst everyone who imports the jsperf module.) + if (!SealObjectAndPrototype(cx, global, "Object") || + !SealObjectAndPrototype(cx, global, "Function") || + !SealObjectAndPrototype(cx, global, "Array")) + return false; + + // Finally, seal the global object, for good measure. (But not recursively; + // this breaks things.) + return JS_FreezeObject(cx, global); +} + +NS_IMETHODIMP +Module::Call(nsIXPConnectWrappedNative* wrapper, + JSContext* cx, + JSObject* obj, + const JS::CallArgs& args, + bool* _retval) +{ + + mozJSComponentLoader* loader = mozJSComponentLoader::Get(); + JS::Rooted<JSObject*> targetObj(cx); + nsresult rv = loader->FindTargetObject(cx, &targetObj); + NS_ENSURE_SUCCESS(rv, rv); + + *_retval = InitAndSealPerfMeasurementClass(cx, targetObj); + return NS_OK; +} + +} // namespace jsperf +} // namespace mozilla + +NS_DEFINE_NAMED_CID(JSPERF_CID); + +static const mozilla::Module::CIDEntry kPerfCIDs[] = { + { &kJSPERF_CID, false, nullptr, mozilla::jsperf::ModuleConstructor }, + { nullptr } +}; + +static const mozilla::Module::ContractIDEntry kPerfContracts[] = { + { JSPERF_CONTRACTID, &kJSPERF_CID }, + { nullptr } +}; + +static const mozilla::Module kPerfModule = { + mozilla::Module::kVersion, + kPerfCIDs, + kPerfContracts +}; + +NSMODULE_DEFN(jsperf) = &kPerfModule; diff --git a/toolkit/components/perf/PerfMeasurement.h b/toolkit/components/perf/PerfMeasurement.h new file mode 100644 index 000000000..b158d1685 --- /dev/null +++ b/toolkit/components/perf/PerfMeasurement.h @@ -0,0 +1,30 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef COMPONENTS_PERFMEASUREMENT_H +#define COMPONENTS_PERFMEASUREMENT_H + +#include "nsIXPCScriptable.h" +#include "mozilla/Attributes.h" + +namespace mozilla { +namespace jsperf { + +class Module final : public nsIXPCScriptable +{ +public: + NS_DECL_ISUPPORTS + NS_DECL_NSIXPCSCRIPTABLE + + Module(); + +private: + ~Module(); +}; + +} // namespace jsperf +} // namespace mozilla + +#endif diff --git a/toolkit/components/perf/PerfMeasurement.jsm b/toolkit/components/perf/PerfMeasurement.jsm new file mode 100644 index 000000000..29a221c6f --- /dev/null +++ b/toolkit/components/perf/PerfMeasurement.jsm @@ -0,0 +1,19 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +this.EXPORTED_SYMBOLS = [ "PerfMeasurement" ]; + +/* + * This is the js module for jsperf. Import it like so: + * Components.utils.import("resource://gre/modules/PerfMeasurement.jsm"); + * + * This will create a 'PerfMeasurement' class. Instances of this class can + * be used to benchmark browser operations. + * + * For documentation on the API, see js/src/perf/jsperf.h. + * + */ + +Components.classes["@mozilla.org/jsperf;1"].createInstance()(); diff --git a/toolkit/components/perf/chrome.ini b/toolkit/components/perf/chrome.ini new file mode 100644 index 000000000..eaa3c2401 --- /dev/null +++ b/toolkit/components/perf/chrome.ini @@ -0,0 +1,3 @@ +[DEFAULT] + +[test_pm.xul] diff --git a/toolkit/components/perf/moz.build b/toolkit/components/perf/moz.build new file mode 100644 index 000000000..d153244c5 --- /dev/null +++ b/toolkit/components/perf/moz.build @@ -0,0 +1,21 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +SOURCES += [ + 'PerfMeasurement.cpp', +] + +EXTRA_JS_MODULES += [ + 'PerfMeasurement.jsm', +] + +FINAL_LIBRARY = 'xul' + +LOCAL_INCLUDES += [ + '/js/xpconnect/loader', +] + +MOCHITEST_CHROME_MANIFESTS += ['chrome.ini'] diff --git a/toolkit/components/perf/test_pm.xul b/toolkit/components/perf/test_pm.xul new file mode 100644 index 000000000..7dbf27b92 --- /dev/null +++ b/toolkit/components/perf/test_pm.xul @@ -0,0 +1,48 @@ +<?xml version="1.0"?> +<!-- This Source Code Form is subject to the terms of the Mozilla Public + - License, v. 2.0. If a copy of the MPL was not distributed with this + - file, You can obtain one at http://mozilla.org/MPL/2.0/. --> + +<window title="Performance measurement tests" + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" + onload="test()"> + + <script type="application/javascript" + src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> + <script type="application/javascript" + src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/> + + <script type="application/javascript"><![CDATA[ +function test() +{ + SimpleTest.waitForExplicitFinish(); + + Components.utils.import("resource://gre/modules/PerfMeasurement.jsm"); + let pm = new PerfMeasurement(PerfMeasurement.ALL); + if (pm.eventsMeasured == 0) { + todo(false, "stub, skipping test"); + } else { + pm.start(); + for (let i = 0; i < 10000; i++) ; + pm.stop(); + + events = ["cpu_cycles", "instructions", "cache_references", "cache_misses", + "branch_instructions", "branch_misses", "bus_cycles", "page_faults", + "major_page_faults", "context_switches", "cpu_migrations"]; + + for (var i = 0; i < events.length; i++) { + var e = events[i]; + ((pm.eventsMeasured & PerfMeasurement[e.toUpperCase()]) ? isnot : todo_is)(pm[e], -1, e); + } + } + SimpleTest.finish(); +} +]]></script> + + <body xmlns="http://www.w3.org/1999/xhtml"> + <p id="display"></p> + <div id="content" style="display:none;"></div> + <pre id="test"></pre> + </body> + <label id="test-result"/> +</window> |