Created
April 3, 2013 22:39
-
-
Save mattparker/5306109 to your computer and use it in GitHub Desktop.
A little hacky effort at testing YUI javascript memory usage using Node.js and phantomjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var util = require('util') | |
http=require('http'), | |
phantom=require('node-phantom'), | |
// simple logging of memory use: | |
memoryCache = [0], | |
logMemory = function (msg) { | |
var mem = process.memoryUsage(); | |
//console.log(msg + " current memory use: ", util.inspect(process.memoryUsage())); | |
console.log(msg + " heapUsed change: " + (mem.heapUsed - memoryCache[memoryCache.length - 1])); | |
memoryCache.push(mem.heapUsed); | |
}; | |
console.log("hello, testing some memory."); | |
logMemory("Start up"); | |
// Serve a very boring page, with YUI seed and a bunch of spans | |
var server = http.createServer(function (request,response) { | |
response.writeHead(200,{"Content-Type": "text/html"}); | |
var doc = '<html><head>' + | |
'<title>Testing memory use</title>' + | |
'<script src="http://yui.yahooapis.com/3.9.1/build/yui/yui-min.js"></script>' + | |
'</head>' + | |
'<body>' + | |
'<h1>The original page</h1>', | |
i = 0; | |
for (; i < 500; i = i + 1) { | |
doc += '<span class="t' + i + '">' + i + '</span>'; | |
} | |
//doc += '<script type="text/javascript">YUI();</script>'; | |
doc += '</body></html>'; | |
response.end(doc); | |
}).listen(1337); | |
logMemory("Server started"); | |
// Use phantomjs to load the page we're serving above, inject some YUI-ness | |
// and see what happens to the memory: | |
phantom.create(function (err, ph) { | |
ph.createPage(function(err,page){ | |
logMemory("Create page"); | |
page.open('http://localhost:1337', function (err,status) { | |
logMemory("Page opened " + status); | |
// findAllSpans does a Y.all("span") to create a bunch of (YUI) Node | |
// instances | |
page.injectJs('memtest/findAllSpans.js', function (err) { | |
logMemory("Just injected js "); | |
// the timeout is so that the YUI().use("node") can download | |
// the required node modules from Yahoo and then call the callback | |
// to do Y.all() | |
setTimeout(function () { | |
page.evaluate(function () { | |
// Sanity check - the injected script modifies the h1 | |
// to say how many spans it found (and how many Y.Node instances | |
// there are) | |
return document.getElementsByTagName('h1')[0].innerText; | |
}, function (err, res) { | |
console.log("h1 content: " + res); | |
logMemory("After YUI.all has happened"); | |
page.close(); | |
logMemory("Page closed"); | |
}); | |
}, 10000); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment