Sent to you by Porti via Google Reader:
Running performance analysis on Greasemonkey scripts can be a pain in the butt. They aren't part of a webpage so standard tools for analyzing web sites don't work... or do they?
The Goal
What You'll Need
The Trick
#1: You need to remove all of the Greasemonkey GM_* functions from the script you want to profile. This is easier than it sounds because all of the functions can be performed by plain 'ole javascript (except for the open in new tab function and register menu command).
#2: You need to embed your Greasemonkey script inside of the running page so you can analyze it with Firebug's profile tool. I have a function below that can embed a function inside the current web page.
#3: You'll need to call the function either using unsafeWindow or by embedding a call to the function in the page.
#4: Litter your code with calls to Firebug's console.profile() and console.time() functions.
Sample Code Template
(function() { function embedFunction(s) { document.body.appendChild(document.createElement('script')).innerHTML = s.toString().replace(/([\s\S]*?return;){2}([\s\S]*)}/,'$2'); } function myKickassGreasemonkeyScript() { console.profile(); // Put everything you need for your Greasemonkey script in here // Don't use any of the GM_* functions! function kickass() { console.time("Block1"); // Block of code that might take a lot of time console.time("Block2"); // another block of code console.timeEnd("Block2"); console.timeEnd("Block1"); } // more cowbell console.profileEnd(); } embedFunction(myKickassGreasemonkeyScript); // Method 1: embed the function call into the current page document.body.appendChild(document.createElement('script')).innerHTML = "myKickassGreasemonkeyScript();"; // Method 2: directly call the function using unsafeWindow // window.addEventListener("load", function(e) { // unsafeWindow.myKickassGreasemonkeyScript(); // this.removeEventListener('load',arguments.callee,false); // }, false); })();
Firebug Tutorial
Michael Sync has a tutorial on using Firebug that describes the console.time() and console.profile() functions. The official website has a nice list of Firebug keyboard shortcuts and a brief description of all the console.* functions.
Related Posts
Things you can do from here:
- Subscribe to // Internet Duct Tape using Google Reader
- Get started using Google Reader to easily keep up with all your favorite sites
No hay comentarios:
Publicar un comentario