성능 개선을 테스트하기 위한 환경을 마련하고자 test server와 test data를 준비해보았다. (코드 참고)
기본적으로 성능에 영향을 주는, 다시 말해 API에서 사용하는 field 값만 random으로 정한다.
그 외에는 여러 값을 가질 수 있어도 고정 값을 사용한다.
Trouble Shooting 참고
$ MYSQL_DATABASE=performance DATA_SIZE=10000 npm run test:insert
> nest-js-example@0.0.1 test:insert
> cross-env NODE_ENV=test ts-node ./test/performance/insert-test-data.ts
Test server running at http://localhost:3333
Connected database: performance
Cleared all test data
Successfully inserted test data (+28524ms):
┌─────────┬─────────────────┬────────┐
│ (index) │ Entity │ Count │
├─────────┼─────────────────┼────────┤
│ 0 │ 'Users' │ 10000 │
│ 1 │ 'Dogs' │ 25187 │
│ 2 │ 'UserDogs' │ 25187 │
│ 3 │ 'Journals' │ 210526 │
│ 4 │ 'JournalsDogs' │ 421673 │
│ 5 │ 'JournalPhotos' │ 316036 │
│ 6 │ 'Excrements' │ 421673 │
└─────────┴─────────────────┴────────┘
Total data size: 1430282
Close connection with the database
Terminate the test application
$ MYSQL_DATABASE=performance npm run test:clear
MDN 공식 문서에 따르면 Math.random()
method는 사용자가 seed를 따로 지정할 수가 없다.
The
Math.random()
static method returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1, with approximately uniform distribution over that range — which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.
그래서 직접 난수 생성 알고리즘을 구현하거나 외부 라이브러리를 사용해야 하는데 검색해본 결과 seedrandom을 사용하기로 결정했다.
seedrandom 라이브러리의 경우 CommonJS 스타일만 지원하는 관계로 어쩔 수 없이 eslint의 no-var-requires 규칙을 무시하도록 주석을 추가했다.
// eslint-disable-next-line @typescript-eslint/no-var-requires
const seedrandom = require('seedrandom');
const randomNumberGenerator = seedrandom(process.env.SEED ?? 'dangdangwalk');
randomNumberGenerator();
환경변수 SEED
(default 'dangdangwalk'
)에 같은 값을 넣으면 동일한 개수의 테스트 데이터가 삽입된다.
SEED=hello
를 적용한 경우:
$ MYSQL_DATABASE=performance DATA_SIZE=100 SEED=hello npm run test:insert
> nest-js-example@0.0.1 test:insert
> cross-env NODE_ENV=test ts-node ./test/performance/insert-test-data.ts
Test server running at http://localhost:3333
Connected database: performance
Cleared all test data
Successfully inserted test data (+301ms):
┌─────────┬─────────────────┬───────┐
│ (index) │ Entity │ Count │
├─────────┼─────────────────┼───────┤
│ 0 │ 'Users' │ 100 │
│ 1 │ 'Dogs' │ 251 │
│ 2 │ 'UserDogs' │ 251 │
│ 3 │ 'Journals' │ 2071 │
│ 4 │ 'JournalsDogs' │ 4161 │
│ 5 │ 'JournalPhotos' │ 3122 │
│ 6 │ 'Excrements' │ 4161 │
└─────────┴─────────────────┴───────┘
Total data size: 14117
Close connection with the database
Terminate the test application