perf: optimize user internal history reads
This commit is contained in:
1 parent
e80313e459
commit
d6e0bbf0e8
4 files changed
+293
-44
No files matched your search
@@ -205,24 +205,36 @@ describe('CommunityStatsService', () => {
|
||||
get: jest.fn().mockResolvedValue(null),
|
||||
set: jest.fn().mockResolvedValue(undefined),
|
||||
} as unknown as jest.Mocked<CommunityStatsCacheService>;
|
||||
const matchStatsRepository = {
|
||||
findUserRecordFragments: jest
|
||||
.fn()
|
||||
.mockResolvedValue([{ matchId, records: [participant] }]),
|
||||
findDetailsByMatches: jest
|
||||
.fn()
|
||||
.mockResolvedValue([{ matchId, gameDetails: [] }]),
|
||||
} as unknown as jest.Mocked<MatchStatsRepository>;
|
||||
const service = new CommunityStatsService(
|
||||
{} as ConnectionService,
|
||||
cacheService,
|
||||
{} as MatchStatsRepository,
|
||||
matchStatsRepository,
|
||||
{} as MatchStatsService,
|
||||
);
|
||||
(service as unknown as { buildBaseDataset: jest.Mock }).buildBaseDataset =
|
||||
(service as unknown as { resolveContext: jest.Mock }).resolveContext =
|
||||
jest.fn().mockResolvedValue({
|
||||
community: {},
|
||||
season: {
|
||||
id: 'all',
|
||||
label: '전체',
|
||||
startTime: null,
|
||||
endTime: null,
|
||||
startedAt: null,
|
||||
endedAt: null,
|
||||
},
|
||||
matches: [],
|
||||
records: [participant],
|
||||
detailsByMatchId: new Map(),
|
||||
from: null,
|
||||
to: null,
|
||||
});
|
||||
const buildBaseDataset = jest.spyOn(
|
||||
service as unknown as { buildBaseDataset: jest.Mock },
|
||||
'buildBaseDataset',
|
||||
);
|
||||
|
||||
const response = await service.getUserInternalHistory({
|
||||
communityId,
|
||||
@@ -236,9 +248,85 @@ describe('CommunityStatsService', () => {
|
||||
gameNumber: 2,
|
||||
queueLabel: '내전',
|
||||
});
|
||||
expect(matchStatsRepository.findUserRecordFragments).toHaveBeenCalledWith({
|
||||
communityId,
|
||||
userId,
|
||||
fromGameCreation: undefined,
|
||||
toGameCreation: undefined,
|
||||
limit: 300,
|
||||
});
|
||||
expect(
|
||||
matchStatsRepository.findDetailsByMatches.mock.calls[0][0].matchIds.map(
|
||||
(id) => id.toString(),
|
||||
),
|
||||
).toEqual([matchId.toString()]);
|
||||
expect(buildBaseDataset).not.toHaveBeenCalled();
|
||||
expect(cacheService.set).toHaveBeenCalledWith(
|
||||
expect.stringContaining(`userInternal:v2:${userId.toString()}`),
|
||||
expect.stringContaining(`userInternal:v3:${userId.toString()}`),
|
||||
response,
|
||||
);
|
||||
});
|
||||
|
||||
it('coalesces concurrent user internal history cache misses', async () => {
|
||||
const communityId = new mongoose.Types.ObjectId();
|
||||
const matchId = new mongoose.Types.ObjectId();
|
||||
const userId = new mongoose.Types.ObjectId();
|
||||
const participant = record(matchId);
|
||||
participant.userId = userId.toString();
|
||||
|
||||
let resolveFragments:
|
||||
| ((fragments: Array<{ matchId: mongoose.Types.ObjectId; records: InternalRecord[] }>) => void)
|
||||
| undefined;
|
||||
const fragmentsPromise = new Promise<
|
||||
Array<{ matchId: mongoose.Types.ObjectId; records: InternalRecord[] }>
|
||||
>((resolve) => {
|
||||
resolveFragments = resolve;
|
||||
});
|
||||
const cacheService = {
|
||||
get: jest.fn().mockResolvedValue(null),
|
||||
set: jest.fn().mockResolvedValue(undefined),
|
||||
} as unknown as jest.Mocked<CommunityStatsCacheService>;
|
||||
const matchStatsRepository = {
|
||||
findUserRecordFragments: jest.fn().mockReturnValue(fragmentsPromise),
|
||||
findDetailsByMatches: jest.fn().mockResolvedValue([]),
|
||||
} as unknown as jest.Mocked<MatchStatsRepository>;
|
||||
const service = new CommunityStatsService(
|
||||
{} as ConnectionService,
|
||||
cacheService,
|
||||
matchStatsRepository,
|
||||
{} as MatchStatsService,
|
||||
);
|
||||
(service as unknown as { resolveContext: jest.Mock }).resolveContext =
|
||||
jest.fn().mockResolvedValue({
|
||||
community: {},
|
||||
season: {
|
||||
id: 'all',
|
||||
label: '전체',
|
||||
startedAt: null,
|
||||
endedAt: null,
|
||||
},
|
||||
from: null,
|
||||
to: null,
|
||||
});
|
||||
|
||||
const first = service.getUserInternalHistory({
|
||||
communityId,
|
||||
userId,
|
||||
filters: { limit: 5 },
|
||||
});
|
||||
const second = service.getUserInternalHistory({
|
||||
communityId,
|
||||
userId,
|
||||
filters: { limit: 5 },
|
||||
});
|
||||
await Promise.resolve();
|
||||
resolveFragments?.([{ matchId, records: [participant] }]);
|
||||
const [firstResponse, secondResponse] = await Promise.all([first, second]);
|
||||
|
||||
expect(firstResponse).toBe(secondResponse);
|
||||
expect(matchStatsRepository.findUserRecordFragments).toHaveBeenCalledTimes(
|
||||
1,
|
||||
);
|
||||
expect(cacheService.set).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user