I use Amazon Lambda with node12. I run htmlparser2 in my code. When I deploy application, htmlparser2's execution time is around 160ms. But after a while function execution time increases to 5000ms. Do you have any idea? In addition same situation in Google Cloud Functions.
index.js
const crawl = require('./crawl.js');
exports.helloWorld = async (req, res) => {
const url = req.query.url;
const result = await crawl(url);
res.status(200).type('application/json').send(result);
};
crawl.js
const bookRecipe = require('./recipes/book');
const quotesRecipe = require('./recipes/quotes');
module.exports = async (url) => {
console.log('url', url);
console.time('books');
const result = await bookRecipe(url);
console.timeEnd('books');
console.time('quotes');
const quotes = await quotesRecipe(result.quotesLink);
console.timeEnd('quotes');
result.quotes = quotes;
return result;
}
recipes/book.js
module.exports = async (url) => {
const random = Math.floor(Math.random() * agents.length);
const agent = agents[random];
console.time("books::fetch")
const result = await fetch(url, {
headers: agent
});
console.timeEnd("books::fetch");
const text = await result.text();
console.time('books::parser');
const dom = htmlparser2.parseDocument(text);
console.log('books::length', text.length);
console.timeEnd('books::parser');
console.time('books::cheerio');
const $ = cheerio.load(dom);
console.timeEnd('books::cheerio');
...
}
Everything is almost same in logs. But htmlparser2's execution is too long in second logs.
Logs:
Normal:
2021-01-26T05:43:28.976+03:00 2021-01-26T02:43:28.976Z 5e3c40c1-36ec-4e94-9230-cbd9d524f984 INFO books::fetch: 134.696ms
2021-01-26T05:43:29.317+03:00 2021-01-26T02:43:29.317Z 5e3c40c1-36ec-4e94-9230-cbd9d524f984 INFO books::length 520143
2021-01-26T05:43:29.317+03:00 2021-01-26T02:43:29.317Z 5e3c40c1-36ec-4e94-9230-cbd9d524f984 INFO books::parser: 297.435ms
2021-01-26T05:43:29.317+03:00 2021-01-26T02:43:29.317Z 5e3c40c1-36ec-4e94-9230-cbd9d524f984 INFO books::cheerio: 0.097ms
2021-01-26T05:43:29.360+03:00 2021-01-26T02:43:29.360Z 5e3c40c1-36ec-4e94-9230-cbd9d524f984 INFO books: 518.615ms
2021-01-26T05:43:30.152+03:00 2021-01-26T02:43:30.152Z 5e3c40c1-36ec-4e94-9230-cbd9d524f984 INFO quotes::fetch: 792.172ms
2021-01-26T05:43:30.218+03:00 2021-01-26T02:43:30.217Z 5e3c40c1-36ec-4e94-9230-cbd9d524f984 INFO quotes::length 312774
2021-01-26T05:43:30.218+03:00 2021-01-26T02:43:30.217Z 5e3c40c1-36ec-4e94-9230-cbd9d524f984 INFO quotes::parser: 59.300ms
2021-01-26T05:43:30.218+03:00 2021-01-26T02:43:30.218Z 5e3c40c1-36ec-4e94-9230-cbd9d524f984 INFO quotes::cheerio: 0.081ms
2021-01-26T05:43:30.520+03:00
2021-01-26T02:43:30.520Z 5e3c40c1-36ec-4e94-9230-cbd9d524f984 INFO quotes: 1159.586ms
2021-01-26T02:43:30.520Z 5e3c40c1-36ec-4e94-9230-cbd9d524f984 INFO quotes: 1159.586ms
Abnormal:
2021-01-26T05:44:48.379+03:00 2021-01-26T02:44:48.379Z 7770ae19-a466-4149-83d2-0d99b9c35a7f INFO books::fetch: 181.431ms
2021-01-26T05:45:01.737+03:00 2021-01-26T02:45:01.720Z 7770ae19-a466-4149-83d2-0d99b9c35a7f INFO books::length 518608
2021-01-26T05:45:01.737+03:00 2021-01-26T02:45:01.737Z 7770ae19-a466-4149-83d2-0d99b9c35a7f INFO books::parser: 13316.728ms
2021-01-26T05:45:01.737+03:00 2021-01-26T02:45:01.737Z 7770ae19-a466-4149-83d2-0d99b9c35a7f INFO books::cheerio: 0.159ms
2021-01-26T05:45:01.897+03:00 2021-01-26T02:45:01.897Z 7770ae19-a466-4149-83d2-0d99b9c35a7f INFO books: 13699.625ms
2021-01-26T05:45:02.668+03:00 2021-01-26T02:45:02.667Z 7770ae19-a466-4149-83d2-0d99b9c35a7f INFO quotes::fetch: 770.189ms
2021-01-26T05:45:09.798+03:00 2021-01-26T02:45:09.797Z 7770ae19-a466-4149-83d2-0d99b9c35a7f INFO quotes::length 313700
2021-01-26T05:45:09.798+03:00 2021-01-26T02:45:09.798Z 7770ae19-a466-4149-83d2-0d99b9c35a7f INFO quotes::parser: 7117.983ms
2021-01-26T05:45:09.798+03:00 2021-01-26T02:45:09.798Z 7770ae19-a466-4149-83d2-0d99b9c35a7f INFO quotes::cheerio: 0.123ms
2021-01-26T05:45:09.861+03:00 2021-01-26T02:45:09.861Z 7770ae19-a466-4149-83d2-0d99b9c35a7f INFO quotes: 7963.365ms
question from:
https://stackoverflow.com/questions/65895308/aws-lambda-slow-down-after-few-request 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…