test
This commit is contained in:
parent
77348e7df7
commit
5fd3c3158e
33
ssr.js
33
ssr.js
|
|
@ -1,8 +1,13 @@
|
||||||
import puppeteer from 'puppeteer';
|
import puppeteer from 'puppeteer';
|
||||||
// import redisClient from './redis/index.js';
|
// import redisClient from './redis/index.js';
|
||||||
import { getBrowser, scheduledTask } from './puppeteerTool.js'
|
// import { getBrowser, scheduledTask } from './puppeteerTool.js'
|
||||||
|
|
||||||
scheduledTask()
|
const MAX_WSE = 4; //启动几个浏览器
|
||||||
|
let WSE_LIST = []; //存储browserWSEndpoint列表
|
||||||
|
|
||||||
|
// scheduledTask()
|
||||||
|
|
||||||
|
init()
|
||||||
|
|
||||||
async function ssr(url) {
|
async function ssr(url) {
|
||||||
// const REDIS_KEY = `ssr:${url}`;
|
// const REDIS_KEY = `ssr:${url}`;
|
||||||
|
|
@ -16,7 +21,10 @@ async function ssr(url) {
|
||||||
|
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
|
|
||||||
const browser = getBrowser()
|
let tmp = Math.floor(Math.random()* MAX_WSE);
|
||||||
|
let browserWSEndpoint = WSE_LIST[tmp];
|
||||||
|
const browser = await puppeteer.connect({browserWSEndpoint});
|
||||||
|
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
try {
|
try {
|
||||||
// networkidle0 waits for the network to be idle (no requests for 500ms).
|
// networkidle0 waits for the network to be idle (no requests for 500ms).
|
||||||
|
|
@ -38,5 +46,24 @@ async function ssr(url) {
|
||||||
return {html, ttRenderMs};
|
return {html, ttRenderMs};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function init(){
|
||||||
|
(async () => {
|
||||||
|
for(var i=0;i<MAX_WSE;i++){
|
||||||
|
const browser = await puppeteer.launch({headless:'new',
|
||||||
|
args: [
|
||||||
|
'--disable-gpu',
|
||||||
|
'--disable-dev-shm-usage',
|
||||||
|
'--disable-setuid-sandbox',
|
||||||
|
'--no-first-run',
|
||||||
|
'--no-sandbox',
|
||||||
|
'--no-zygote',
|
||||||
|
'--single-process'
|
||||||
|
]});
|
||||||
|
let browserWSEndpoint = await browser.wsEndpoint();
|
||||||
|
WSE_LIST[i] = browserWSEndpoint;
|
||||||
|
}
|
||||||
|
console.log(WSE_LIST);
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
|
||||||
export {ssr as default};
|
export {ssr as default};
|
||||||
Loading…
Reference in New Issue