brianwalkertoretto / performance-optimization Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
const puppeteer = require('puppeteer');
/*
优化一:puppeteer连接池(启动多个puppeteer,然后依次使用某个puppeteer,页签大于maxUses就关闭它)
最少puppeteer数,低于这个数就启动多个puppeteer
优化二:每个puppeteer的页签最大使用maxUses,多余这个就关闭这个puppeteer
优化三:每个puppeteer的页签最大并发数,多于这个数就切换或者启动另一个puppeteer
优化四:远程连接:browser.wsEndpoint
*/
const createBrowserFactory = {
instance: null,
maxUses: 110, // 自定义的属性:每一个 实例 最大可重用次数。
// starting: false, // 启动中,默认:false,
create: async function(args) {
if(this.validate()) {
return this
}
await this.close()
return puppeteer.launch({
// 关闭无头模式,方便我们看到这个无头浏览器执行的过程
headless: true,
timeout: 30000, // 默认超时为30秒,设置为0则表示不设置超时
args,
ignoreHTTPSErrors: true,
defaultViewport: {
width: 1920, height: 1080,
// deviceScaleFactor: 1,
// isMobile
// hasTouch
// isLandscape
}
}).then(instance => {
return this.load(instance)
}).catch(() => {
this.close();
return null
})
},
load: function(instance) {
instance.useCount = 0;
instance.on('disconnected', async () => {
await this.close()
})
this.instance = instance;
return this;
},
// destroy
close: async function() {
if(this.instance) {
await this.instance.close()
this.instance = null
}
},
validate: function() {
return this.instance && this.instance.useCount < this.maxUses ? true : false
},
newPage: function() {
this.instance.useCount++;
return this.instance.newPage()
}
}
module.exports = createBrowserFactory;
// 延迟启动
process.nextTick(() => {
createBrowserFactory.create()
})
createBrowserFactory.create([
'--no-zygote',
'--no-sandbox',
'--disable-gpu', // 没啥需要gpu渲染的css等,可以先禁用
'--no-first-run',
'--single-process',
'--disable-extensions',
"--disable-xss-auditor",
'--disable-dev-shm-usage',
'--disable-popup-blocking',
'--disable-setuid-sandbox',
'--disable-accelerated-2d-canvas',
'--enable-features=NetworkService',
])
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.