我正在尝试使用javascript这样的灯塔:
var lighthouse = require('lighthouse');
var chromeLauncher = require('lighthouse/chrome-launcher/chrome-launcher');
var ReportGenerator = require('lighthouse/lighthouse-core/report/v2/report-generator')
var url = 'http://localhost:8080';
var flags = {output: 'json'};
var config = null;
chromeLauncher.launch().then(function(chrome) {
flags.port = chrome.port;
return lighthouse(url, flags, config).then(function(results) {
return chrome.kill().then(function() { return results; })
});
}).then(function(results) {
html = new ReportGenerator().generateReportHtml(results);
console.log(html);
});
但是,似乎以编程方式生成html会导致节点崩溃OOM
$ node index2.js
<--- Last few GCs --->
69281 ms: Mark-sweep 1250.1 (1438.1) -> 1250.1 (1438.1) MB, 793.8 / 0.0 ms [allocation failure] [scavenge might not succeed].
69998 ms: Mark-sweep 1250.1 (1438.1) -> 1250.1 (1438.1) MB, 716.6 / 0.0 ms [allocation failure] [scavenge might not succeed].
70795 ms: Mark-sweep 1250.1 (1438.1) -> 1265.1 (1422.1) MB, 797.4 / 0.0 ms [last resort gc].
71556 ms: Mark-sweep 1265.1 (1422.1) -> 1280.7 (1422.1) MB, 760.9 / 0.0 ms [last resort gc].
<--- JS stacktrace --->
==== JS stack trace =========================================
Security context: 0x1940ecbcf781 <JS Object>
1: stringify [native json.js:178] [pc=0x2037a6e19a57] (this=0x1940ecbc92a1 <a JSON with map 0x26acd5f09b09>,E=0x9b9ceafc99 <an Object with map 0x1601799c0a11>,F=0x1940ecb04381 <undefined>,S=0x1940ecb04381 <undefined>)
2: arguments adaptor frame: 1->3
3: generateReportHtml [/Users/arbi/Projects/test-lighthouse/node_modules/lighthouse/lighthouse-core/report/v2/report-generato...
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
1: node::Abort() [/Users/arbi/.nvm/versions/node/v6.11.3/bin/node]
2: node::FatalException(v8::Isolate*, v8::Local<v8::Value>, v8::Local<v8::Message>) [/Users/arbi/.nvm/versions/node/v6.11.3/bin/node]
3: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/Users/arbi/.nvm/versions/node/v6.11.3/bin/node]
4: v8::internal::Factory::NewRawTwoByteString(int, v8::internal::PretenureFlag) [/Users/arbi/.nvm/versions/node/v6.11.3/bin/node]
5: v8::internal::IncrementalStringBuilder::Extend() [/Users/arbi/.nvm/versions/node/v6.11.3/bin/node]
6: v8::internal::BasicJsonStringifier::Result v8::internal::BasicJsonStringifier::Serialize_<true>(v8::internal::Handle<v8::internal::Object>, bool, v8::internal::Handle<v8::internal::Object>) [/Users/arbi/.nvm/versions/node/v6.11.3/bin/node]
7: v8::internal::BasicJsonStringifier::Result v8::internal::BasicJsonStringifier::Serialize_<false>(v8::internal::Handle<v8::internal::Object>, bool, v8::internal::Handle<v8::internal::Object>) [/Users/arbi/.nvm/versions/node/v6.11.3/bin/node]
8: v8::internal::BasicJsonStringifier::Result v8::internal::BasicJsonStringifier::Serialize_<true>(v8::internal::Handle<v8::internal::Object>, bool, v8::internal::Handle<v8::internal::Object>) [/Users/arbi/.nvm/versions/node/v6.11.3/bin/node]
9: v8::internal::BasicJsonStringifier::Result v8::internal::BasicJsonStringifier::Serialize_<true>(v8::internal::Handle<v8::internal::Object>, bool, v8::internal::Handle<v8::internal::Object>) [/Users/arbi/.nvm/versions/node/v6.11.3/bin/node]
10: v8::internal::BasicJsonStringifier::Result v8::internal::BasicJsonStringifier::Serialize_<true>(v8::internal::Handle<v8::internal::Object>, bool, v8::internal::Handle<v8::internal::Object>) [/Users/arbi/.nvm/versions/node/v6.11.3/bin/node]
11: v8::internal::BasicJsonStringifier::Result v8::internal::BasicJsonStringifier::Serialize_<true>(v8::internal::Handle<v8::internal::Object>, bool, v8::internal::Handle<v8::internal::Object>) [/Users/arbi/.nvm/versions/node/v6.11.3/bin/node]
12: v8::internal::BasicJsonStringifier::Result v8::internal::BasicJsonStringifier::Serialize_<false>(v8::internal::Handle<v8::internal::Object>, bool, v8::internal::Handle<v8::internal::Object>) [/Users/arbi/.nvm/versions/node/v6.11.3/bin/node]
13: v8::internal::Runtime_BasicJSONStringify(int, v8::internal::Object**, v8::internal::Isolate*) [/Users/arbi/.nvm/versions/node/v6.11.3/bin/node]
14: 0x2037a65092a7
[1] 11901 abort node index2.js
在json中仅输出lighthouse报告似乎正常,但是我也需要能够生成html。
也可以通过增加内存来解决问题
$ node --max-old-space-size=16000 index2.js
有什么想法可以在不增加内存的情况下解决该问题吗?