Skip to content

Commit

Permalink
Move benchmark extension example to new location and clean it up to u…
Browse files Browse the repository at this point in the history
…se browser actions rather than toolstrips.

BUG=26106
TEST=none

Review URL: http://codereview.chromium.org/384065

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@31815 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
erikkay@chromium.org committed Nov 12, 2009
1 parent 0335b08 commit 2468fc5
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 63 deletions.
3 changes: 2 additions & 1 deletion chrome/app/chrome_dll_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,8 @@ int ChromeMain(int argc, char** argv) {
// of the process. It is not cleaned up.
// TODO(port): we probably need to shut this down correctly to avoid
// leaking shared memory regions on posix platforms.
if (parsed_command_line.HasSwitch(switches::kEnableStatsTable)) {
if (parsed_command_line.HasSwitch(switches::kEnableStatsTable) ||
parsed_command_line.HasSwitch(switches::kEnableBenchmarking)) {
std::string statsfile = StringPrintf("%s-%lld", chrome::kStatsFilename,
static_cast<int64>(browser_pid));
StatsTable *stats_table = new StatsTable(statsfile,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
Benchmark Extension
-------------------
This extension provides basic page-level benchmarking into the browser.
With the extension installed you can test web pages and then compare
results in a subwindow.
Between each page load you can optionally clear idle http connections and
clear the cache so that page loads are more like the user experience
when first connecting to a site.
To use this benchmark, you'll need to run chrome with the the
"--enable-benchmarking" flag. This flag enables a v8-extension so that
the benchmark can clear idle connections and the cache.
The code found in the jst/ subdirectory is JSTemplate code from
http://code.google.com/p/google-jstemplate/
Benchmark Extension
-------------------
This extension provides basic page-level benchmarking into the browser.

With the extension installed you can test web pages and then compare
results in a subwindow.

Between each page load you can optionally clear idle http connections and
clear the cache so that page loads are more like the user experience
when first connecting to a site.

To use this benchmark, you'll need to run chrome with the the
"--enable-benchmarking" flag. This flag enables a v8-extension so that
the benchmark can clear idle connections and the cache.

The code found in the jst/ subdirectory is JSTemplate code from
http://code.google.com/p/google-jstemplate/

Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,29 @@
window.testUrl = "";
window.windowId = 0;

var optionsForm = 0;
function show_options() {
// This functionality is a bit fragile, probably due to the extension API.
// If we open the window from within the getSelected() callback, then the
// window opens in a new window rather than a new tab (is that a bug?).
// If we open the window before we call getSelected(), then the selected
// tab URL will be NULL, because the page won't have loaded yet, but it will
// be the selected tab. So, we do this weird thing where we first call
// get selected, knowing that it is asynchronous. Then we open the window,
// and finally when the callback occurs for getSelected, the window will
// be created and we can use it.
chrome.tabs.getSelected(windowId, function(tab) {
chrome.tabs.getSelected(null, function(tab) {
window.testUrl = tab.url;
optionsForm.setUrl(window.testUrl);
var tabs = chrome.extension.getExtensionTabs();
if (tabs && tabs.length) {
tabs[0].setUrl(testUrl);
var optionsUrl = chrome.extension.getURL("options.html");
chrome.tabs.getAllInWindow(null, function(all) {
for (var i = 0; i < all.length; i++) {
if (all[i].url == optionsUrl) {
chrome.tabs.update(all[i].id, {selected: true });
return;
}
}
});
} else {
chrome.tabs.create({"url":"options.html"});
}
});
optionsForm = window.open("options.html", "optionswindow");
}

chrome.browserAction.onClicked.addListener(show_options);

function Benchmark() {
var runCount_ = 0;
var count_;
Expand Down Expand Up @@ -110,19 +115,27 @@
// push the result
window.results.data.push(current_);
current_ = 0;

// show the latest
show_options();
}

// Update the UI after a test run.
this.displayResults = function() {
var span = document.getElementById("result");
var score = 0;
if (count_ > 0) {
score = totalTime_ / count_;
var text = score.toFixed(1) + "ms avg";
chrome.browserAction.setTitle({"title": text});
}
if (runCount_) {
chrome.browserAction.setBadgeText({"text": "" + runCount_});
chrome.browserAction.setBadgeBackgroundColor({"color": [255, 0, 0, 255]});
} else {
chrome.browserAction.setBadgeText({"text": "" + score.toFixed()});
chrome.browserAction.setBadgeBackgroundColor({"color": [0, 255, 0, 255]});
var tabs = chrome.extension.getExtensionTabs();
if (tabs && tabs.length) {
tabs[0].location.reload();
}
}
span.innerHTML = score.toFixed(1) + " (" + (runCount_) + ")";
}

// Run a single page in the benchmark
Expand Down Expand Up @@ -184,7 +197,6 @@
});

function run() {
show_options();
var urls = testUrl.split(",");
for (var i = 0; i < urls.length; i++) {
var benchmark = new Benchmark();
Expand All @@ -198,16 +210,3 @@
window.windowId = currentWindow.id;
});
</script>

<style>
#result {
color: green;
text-align: center;
vertical-align: center;
}
</style>

<div id="bench">
<img src="stopwatch.jpg" height="25" width="25" align=top onclick="show_options()">
<span id="result"></span>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
"name": "Page Benchmarker",
"version": "1.0",
"description": "Chromium Page Benchmarker.",
"toolstrips": [
"toolstrip.html"
],
"background_page": "background.html",
"browser_action": {
"default_title": "Benchmark page load time",
"default_icon": "stopwatch.jpg"
},
"options_page": "options.html",
"content_scripts": [
{
"matches": ["http://*/*"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<style>
body {
font-size: 84%;
font-family: Arial, Helvetica, sans-serif;
font-family: Helvetica, Arial, sans-serif;
padding: 0.75em;
margin: 0;
min-width: 45em;
Expand Down Expand Up @@ -149,8 +149,10 @@
}

function jsinit() {
var extension = chrome.extension.getBackgroundPage();

// Run the template to show results
var data = window.opener.results;
var data = extension.results;
computeResults(data);

var context = new JsEvalContext(data);
Expand All @@ -160,11 +162,10 @@
jstProcess(context, template);

// Set the options
var extension = window.opener;
document.getElementById("iterations").value = extension.iterations;
document.getElementById("clearconns").checked = extension.clearConnections;
document.getElementById("clearcache").checked = extension.clearCache;
document.getElementById("testurl").value = extension.testUrl;
setUrl(extension.testUrl);
}

function getWidth(mean, max_width) {
Expand All @@ -173,7 +174,7 @@

// Apply configuration back to our extension
function config() {
var extension = window.opener;
var extension = chrome.extension.getBackgroundPage();
var iterations = parseInt(document.getElementById("iterations").value);
var clearConnections = document.getElementById("clearconns").checked;
var clearCache = document.getElementById("clearcache").checked;
Expand All @@ -187,19 +188,22 @@
// Set the url in the benchmark url box.
function setUrl(url) {
document.getElementById("testurl").value = url;
console.log(url);
console.log(document.getElementById("testurl").value);
}

// Start the benchmark.
function run() {
var extension = window.opener;
var extension = chrome.extension.getBackgroundPage();
var testUrl = document.getElementById("testurl").value;
extension.testUrl = testUrl;
extension.run();
}

// Clear the results
function clearResults() {
window.opener.results.data = new Array();
var extension = chrome.extension.getBackgroundPage();
extension.results.data = new Array();
jsinit();
}
</script>
Expand All @@ -218,7 +222,7 @@ <h1>Configuration</h1>
Clear Connections?<input id="clearconns" type="checkbox">
Clear Cache?<input id="clearcache" type="checkbox">
<input type="button" value="OK" onclick="config();"><P>
URL to load <input type="text" id="testurl" size=100 value="http://www.google.com/"></input>
URL to load <input type="text" id="testurl" size=100>
<input type="button" value="Run" onclick="run();"><P>
<p>

Expand Down
2 changes: 1 addition & 1 deletion webkit/extensions/v8/benchmarking_extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class BenchmarkingWrapper : public v8::Extension {
}

static v8::Handle<v8::Value> GetCounter(const v8::Arguments& args) {
if (!args.Length() || !args[0]->IsString())
if (!args.Length() || !args[0]->IsString() || !StatsTable::current())
return v8::Undefined();

// Extract the name argument
Expand Down

0 comments on commit 2468fc5

Please sign in to comment.