我的应用在开发环境下启动相当缓慢,我在各个地方设置了一些调试日志,以查看是什么花费了如此多的时间。结果发现,我的main.ts文件实际上需要将近9分钟的时间才能导入app.module
!
import { performance } from 'perf_hooks';
const startTime = performance.now();
import { Log } from 'api/common/util/logger/log';
Log.log.info(`┌────────────────────────────────────────────────────────────┐`);
Log.log.info(`│ Starting: ${new Date().toISOString()} │`);
Log.log.info(`└────────────────────────────────────────────────────────────┘`);
// From here -------------------->
import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import 'reflect-metadata';
import { existsSync, mkdirSync, writeFile } from 'fs';
import * as express from 'express';
import * as bodyParser from 'body-parser';
import * as helmet from 'helmet';
import * as morgan from 'morgan';
import * as morganBody from 'morgan-body';
// <------ to here, imports fly in as expected.
// Theese take a bit longer, but not enormously
import { Config } from './api/common/config';
import { HttpExceptionFilter } from './api/common/filters/http-exception.filter';
import { LogService } from 'api/common/util/logger/log.service';
// This one takes up the most time on startup (several minutes)
import { AppModule } from './api/app.module';
Log.log.debug(` * imports done in ${(performance.now() - startTime).toFixed(3)}ms`);
Log.log.debug(` * Memory: ${readMem()}`);
function readMem() {
const mem = process.memoryUsage();
const convert = { Kb: n => (n / 1024), Mb: n => convert.Kb(n) / 1024 };
const toHuman = (n, t) => `${convert[t](n).toFixed(2)}${t}`;
return `Used ${toHuman(mem.heapUsed, 'Mb')} of ${toHuman(mem.heapTotal, 'Mb')} - RSS: ${toHuman(mem.rss, 'Mb')}`;
}
输出
生产启动:
$ node dist/main.js
info: ┌──────────────────────────────────────────────────────────────────────────┐
info: │ Starting: 2019-01-29T13:06:13.751Z │
info: │ Memory: Used 6.54Mb of 11.70Mb - RSS: 25.33Mb │
info: │ Runtime: js │
info: └──────────────────────────────────────────────────────────────────────────┘
debug: * imports done in 6862.350ms
debug: * Memory: Used 87.99Mb of 113.76Mb - RSS: 133.58Mb
info: Nest application successfully started
info: ┌──────────────────────────────────────────────────────────────────────────┐
info: │ Memory: Used 93.71Mb of 122.52Mb - RSS: 144.20Mb │
info: │ Launch: 2019-01-29T13:06:25.377Z │
info: │ Time to start: 11991.049ms │
info: │ Bootstrap time: 5124.189ms │
info: └──────────────────────────────────────────────────────────────────────────┘
开发初创公司:
$ ts-node -r tsconfig-paths/register src/main.ts
info: ┌──────────────────────────────────────────────────────────────────────────┐
info: │ Starting: 2019-01-29T13:08:06.914Z │
info: │ Memory: Used 157.76Mb of 193.62Mb - RSS: 209.77Mb │
info: │ Runtime: ts │
info: └──────────────────────────────────────────────────────────────────────────┘
debug: * imports done in 471159.063ms
debug: * Memory: Used 297.45Mb of 385.35Mb - RSS: 408.90Mb
info: Nest application successfully started
info: ┌──────────────────────────────────────────────────────────────────────────┐
info: │ Memory: Used 216.64Mb of 383.35Mb - RSS: 409.11Mb │
info: │ Launch: 2019-01-29T13:16:05.521Z │
info: │ Time to start: 483228.325ms │
info: │ Bootstrap time: 12042.239ms │
info: └──────────────────────────────────────────────────────────────────────────┘
是的,我使用ts-node
开始了这个项目,但这是NestJS在开发和调试时推荐的。
问题
如何优化启动时间,使得后端每次进行微小更改都不需要等待10分钟?我本来就很难集中精力,这样做更加没帮助。
我的模块太多了吗?如果我将它们合并,会有所帮助吗?我有大约15个DB实体模型,每个模型都包含在自己的基于graphql的模块中,以提高可读性,但其中许多模型都存在由forwardRef()
注入解决的循环依赖关系。这可能是一个问题吗?
我尽量少使用第三方库,以避免node_modules地狱。我在我的模块中导入的要么是我的代码,要么是NestJS框架的东西。当然,我不知道有多少隐式依赖项被加载了,但是我携带的库数量是否会影响启动性能呢?如果会的话,我该如何监控堆栈上的内容以及每个脚本在评估时消耗的内存/CPU量?我是否可以预编译其中的一些内容以增加启动速度?
当我在生产环境下以编译后的Javascript运行时,就没有这个问题。
tsc --watch
,并在每次进行更改时重新启动node ./dist/main.js
。这可能可以使用nodemon更好地完成,但我无法让它与vscode一起工作。 - Øystein Amundsen