-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (110 loc) · 3.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const fs = require('fs-extra');
const path = require('path');
const program = require('commander');
const chalk = require('chalk');
const { parse, stringify } = require('flatted/cjs');
//
program
.version(require('./package').version, '-v, --version')
.usage('[options]')
.option('--crawler <startURL>', 'Run Crawler, starting with URL startURL.')
.option('--memory <startURL>', 'Run Memory, starting with URL startURL.')
.option(
'--psi <startURL>',
'Run PageSpeed test for the home page of startURL.'
)
.parse(process.argv);
//
(async () => {
const { crawler, memory, psi } = program;
const startTS = Date.now();
if (crawler) {
console.log('Running Crawler test...');
console.log(' ');
const errors = await require('./crawler')(crawler, {
debug: true,
// maxCrawl: 100,
cluster: {
maxConcurrency: 5,
monitor: true
}
});
console.log('\n' + chalk.bgRedBright(` ERROR `));
const dir = path.resolve(__dirname, '../../logs/crawler/');
await fs.ensureDir(dir);
await fs.writeJson(
path.resolve(dir, `${encodeURIComponent(crawler)}.json`),
parse(stringify(errors)),
{
spaces: '\t'
}
);
Object.entries(errors).forEach(([type, errors]) => {
console.log('❌ ' + chalk.underline(type));
errors.forEach(err => {
console.log(`● ${err.url}`);
if (err.pageUrl) console.log(` pageUrl: ${err.pageUrl}`);
switch (type) {
case 'no gzip':
case 'large file': {
let size = err.contentLength;
if (size > 1024 * 1024) {
size = getSize(size);
} else if (size > 1024) {
size = getSize(size);
}
console.log(` contentLength: ${size}`);
break;
}
case 'console error': {
console.log(` ${err.message}`);
break;
}
default: {
}
}
});
});
// Object.keys(errors).forEach(type => {
// errors[type].forEach((err, index) => {
// const { message, res, type, ...infos } = err;
// const r = { ...infos };
// if (type === 'console error') r.message = message;
// errors[type][index] = r;
// });
// });
// Object.entries(errors).forEach(([key, value]) => {
// console.log(chalk.underline(key));
// console.log(JSON.stringify(value, null, 2));
// });
console.log('');
} else if (memory) {
console.log('Running Memory test for 60s...');
console.log('');
const result = await require('./memory')(memory, 60, true);
result.forEach(r => {
console.log(`● ${r.prevUrl}`);
console.log(`⇢ ${r.newUrl}`);
console.log(` heap used: ${getSize(r.JSHeapUsedSize)}`);
console.log(` heap total: ${getSize(r.JSHeapTotalSize)}`);
});
console.log('');
} else if (psi) {
console.log('Running PageSpeed tests...');
console.log('');
const result = await require('./psi')(psi);
console.dir(result);
console.log('');
}
const endTS = Date.now();
console.log(`Elapsed ${((endTS - startTS) / 1000).toFixed(3)}ms`);
// program.help();
})();
//
const getSize = size => {
if (size > 1024 * 1024) {
return (size / 1024 / 1024).toFixed(2) + 'MB';
} else if (size > 1024) {
return (size / 1024).toFixed(2) + 'KB';
}
};