Check/Log how much bandwidth PhantomJS/CasperJS used

Is it possible to check/log how much data has been transferred during each run of PhantomJs/CasperJS?

Each instance of Phantom/Casper has a instance_id assigned to it (by the PHP function that spun up the instance). After the run has finished, the amount of data transferred and the instance_id will have to make its way to be inserted into a MySQL database, possibly via the PHP function that spawned the instance. This way the bandwidth utilization of individual phantomjs runs can be logged.

There can be many phantom/casper instances running, each lasting a minute or two.


The easiest and most accurate approach when trying to capture data is to get the collector and emitter as close as possible. In this case it would be ideal if phantomjs could capture that data that you need and send it back to your PHP function to associate it to the instance_id and do the database interaction. Turns out it can (at least partially).

Here is one approach:

var page = require('webpage').create();
var bytesReceived = 0;

page.onResourceReceived = function (res) {
    if (res.bodySize) {
        bytesReceived += res.bodySize;
    }
};

page.open("http://www.google.com", function (status) {
    console.log(bytesReceived);
    phantom.exit();
});

This captures the size of all resources retrieved, adds them up, and spits out the result to standard output where your PHP code is able to work with it. This does not include the size of headers or any POST activity. Depending upon your application, this might be enough. If not, then hopefully this gives you a good jumping off point.

链接地址: http://www.djcxy.com/p/73478.html

上一篇: casperjs测试冷冻导致jenkins生成超时

下一篇: 检查/记录PhantomJS / CasperJS使用的带宽