|
| 1 | +var file = require('sdk/io/file'); |
| 2 | +var {Cc,Ci,Cu} = require("chrome"); |
| 3 | + |
| 4 | +function Profiler() { |
| 5 | + this._profiler = Cc["@mozilla.org/tools/profiler;1"].getService(Ci.nsIProfiler); |
| 6 | +}; |
| 7 | + |
| 8 | +Profiler.prototype = { |
| 9 | + start: function(entries, interval, features) { |
| 10 | + this._profiler.StartProfiler(entries, interval, features, features.length); |
| 11 | + }, |
| 12 | + stop: function(cb) { |
| 13 | + this._profiler.StopProfiler(); |
| 14 | + // this.gcStats.clear(); |
| 15 | + }, |
| 16 | + getProfileData: function() { |
| 17 | + return this._profiler.getProfileData(); |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +function saveToFile(savePath, str) { |
| 22 | + var textWriter = file.open(savePath, 'w'); |
| 23 | + textWriter.write(str); |
| 24 | + textWriter.close(); |
| 25 | +} |
| 26 | + |
| 27 | +function forceGC() { |
| 28 | + Cu.forceGC(); |
| 29 | + var os = Cc["@mozilla.org/observer-service;1"] |
| 30 | + .getService(Ci.nsIObserverService); |
| 31 | + os.notifyObservers(null, "child-gc-request", null); |
| 32 | +} |
| 33 | + |
| 34 | +var profiler = new Profiler(); |
| 35 | +function startProfiler() { |
| 36 | + profiler.start(/* = profiler memory */ 10000000, 1, ['leaf', 'js', "stackwalk", 'gc']); |
| 37 | +}; |
| 38 | +function stopAndRecord(filePath) { |
| 39 | + var profileData = profiler.getProfileData(); |
| 40 | + profiler.stop(); |
| 41 | + saveToFile(filePath, JSON.stringify(profileData, null, 2)); |
| 42 | +}; |
| 43 | + |
| 44 | + |
| 45 | +var mod = require("sdk/page-mod"); |
| 46 | +var data = require("sdk/self").data; |
| 47 | +mod.PageMod({ |
| 48 | + include: ['*'], |
| 49 | + contentScriptFile: data.url("installed_script.js"), |
| 50 | + onAttach: function(worker) { |
| 51 | + worker.port.on('startProfiler', function() { |
| 52 | + startProfiler(); |
| 53 | + }); |
| 54 | + worker.port.on('stopAndRecord', function(filePath) { |
| 55 | + stopAndRecord(filePath); |
| 56 | + }); |
| 57 | + worker.port.on('forceGC', function() { |
| 58 | + forceGC(); |
| 59 | + }); |
| 60 | + } |
| 61 | +}); |
0 commit comments