Skip to content

Commit 777eb40

Browse files
working prepared letter performance test
1 parent 2162ff5 commit 777eb40

14 files changed

Lines changed: 2064 additions & 302 deletions

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ config:: _install-dependencies version # Configure development environment (main
103103
test-component:
104104
(cd tests && npm install && npm run test:component)
105105

106+
test-performance:
107+
(cd tests && npm install && npm run test:performance)
108+
106109
version:
107110
rm -f .version
108111
make version-create-effective-file dir=.

package-lock.json

Lines changed: 1751 additions & 204 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/component-tests/testCases/create-letter-request.spec.ts

Lines changed: 0 additions & 98 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import { envName } from "tests/constants/api-constants";
4+
import { getLambdaEnv, updateLambdaEnv } from "tests/helpers/aws-lambda-helper";
5+
import { logger } from "tests/helpers/pino-logger";
6+
7+
const ORIGINAL_ENV = path.join(__dirname, ".lambda-env.json");
8+
const UPSERT_LETTER_LAMBDA_ARN = `arn:aws:lambda:eu-west-2:820178564574:function:nhs-${envName}-supapi-upsertletter`;
9+
10+
export default async function performanceSetup() {
11+
const currentEnv = await getLambdaEnv(UPSERT_LETTER_LAMBDA_ARN);
12+
if (Object.keys(currentEnv).length === 0) {
13+
return;
14+
}
15+
16+
// Persist original env for teardown
17+
fs.writeFileSync(ORIGINAL_ENV, JSON.stringify(currentEnv, null, 2));
18+
19+
await updateLambdaEnv(UPSERT_LETTER_LAMBDA_ARN, {
20+
...currentEnv,
21+
LETTER_TTL_HOURS: "1",
22+
});
23+
24+
logger.info("LETTER_TTL_HOURS set to 1");
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import fs from "node:fs";
2+
import { envName } from "tests/constants/api-constants";
3+
import { updateLambdaEnv } from "tests/helpers/aws-lambda-helper";
4+
import path from "node:path";
5+
import { logger } from "tests/helpers/pino-logger";
6+
7+
const ORIGINAL_ENV = path.join(__dirname, ".lambda-env.json");
8+
const UPSERT_LETTER_LAMBDA_ARN = `arn:aws:lambda:eu-west-2:820178564574:function:nhs-${envName}-supapi-upsertletter`;
9+
10+
export default async function performanceTeardown() {
11+
if (!fs.existsSync(ORIGINAL_ENV)) {
12+
return;
13+
}
14+
15+
const originalEnv = JSON.parse(fs.readFileSync(ORIGINAL_ENV, "utf-8"));
16+
17+
await updateLambdaEnv(UPSERT_LETTER_LAMBDA_ARN, originalEnv);
18+
fs.unlinkSync(ORIGINAL_ENV);
19+
20+
logger.info("Original value of LETTER_TTL_HOURS restored");
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { PlaywrightTestConfig } from "@playwright/test";
2+
import path from "node:path";
3+
import { config as baseConfig } from "../playwright.base.config";
4+
import { getReporters } from "../reporters";
5+
6+
const localConfig: PlaywrightTestConfig = {
7+
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
8+
reporter: getReporters("api-test"),
9+
...baseConfig,
10+
globalSetup: path.resolve(__dirname, "./performance-setup.ts"),
11+
globalTeardown: path.resolve(__dirname, "./performance-teardown.ts"),
12+
projects: [
13+
{
14+
name: "performance",
15+
testDir: path.resolve(__dirname, "../../performance"),
16+
testMatch: "*.spec.ts",
17+
},
18+
],
19+
};
20+
21+
export default localConfig;

tests/generateAllureReport.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/bash
2+
3+
allure generate allure-results --clean
4+
allure open allure-report
5+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import {
2+
GetRecordsCommand,
3+
GetRecordsCommandOutput,
4+
GetShardIteratorCommand,
5+
KinesisClient,
6+
ListShardsCommand,
7+
Shard,
8+
ShardIteratorType,
9+
} from "@aws-sdk/client-kinesis";
10+
import { AWS_REGION } from "tests/constants/api-constants";
11+
12+
export const kinesisClient = new KinesisClient({ region: AWS_REGION });
13+
14+
/**
15+
* Wait for a matching record on a Kinesis stream.
16+
*
17+
* @param streamARN - existing Kinesis stream ARN
18+
* @param opts - optional settings
19+
*/
20+
export async function retrieveKinesisRecordsAtTimestamp(
21+
streamARN: string,
22+
startTimeMs: number,
23+
): Promise<number> {
24+
let recordsFound = 0;
25+
26+
const shards: Shard[] = await getShards(streamARN);
27+
for (const shard of shards) {
28+
recordsFound += await getAllShardRecords(shard, streamARN, startTimeMs);
29+
}
30+
return recordsFound;
31+
}
32+
33+
async function getShards(streamARN: string) {
34+
const shardsResp = await kinesisClient.send(
35+
new ListShardsCommand({ StreamARN: streamARN }),
36+
);
37+
const shards: Shard[] = shardsResp.Shards ?? [];
38+
if (shards.length === 0) {
39+
throw new Error(`No shards found for stream ${streamARN}`);
40+
}
41+
return shards;
42+
}
43+
44+
async function getAllShardRecords(
45+
shard: Shard,
46+
streamARN: string,
47+
startTimeMs: number,
48+
) {
49+
let shardRecords = 0;
50+
const iteratorResponse = await kinesisClient.send(
51+
new GetShardIteratorCommand({
52+
StreamARN: streamARN,
53+
ShardId: shard.ShardId,
54+
ShardIteratorType: ShardIteratorType.AT_TIMESTAMP,
55+
Timestamp: new Date(startTimeMs),
56+
}),
57+
);
58+
59+
const shardIterator = iteratorResponse.ShardIterator;
60+
if (!shardIterator) {
61+
throw new Error("Failed to obtain shard iterator");
62+
}
63+
64+
const recResp: GetRecordsCommandOutput = await kinesisClient.send(
65+
new GetRecordsCommand({
66+
ShardIterator: shardIterator,
67+
StreamARN: streamARN,
68+
}),
69+
);
70+
shardRecords += recResp.Records?.length ?? 0;
71+
72+
return shardRecords;
73+
}

tests/helpers/aws-lambda-helper.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {
2+
LambdaClient,
3+
GetFunctionConfigurationCommandOutput,
4+
GetFunctionConfigurationCommand,
5+
UpdateFunctionConfigurationCommand,
6+
} from "@aws-sdk/client-lambda";
7+
import { AWS_REGION } from "tests/constants/api-constants";
8+
9+
const lambdaClient = new LambdaClient({ region: AWS_REGION });
10+
11+
export async function getLambdaEnv(
12+
functionArn: string,
13+
): Promise<Record<string, string>> {
14+
const response: GetFunctionConfigurationCommandOutput =
15+
await lambdaClient.send(
16+
new GetFunctionConfigurationCommand({
17+
FunctionName: functionArn,
18+
}),
19+
);
20+
return response.Environment?.Variables ?? {};
21+
}
22+
23+
export async function updateLambdaEnv(
24+
functionArn: string,
25+
variables: Record<string, string>,
26+
) {
27+
await lambdaClient.send(
28+
new UpdateFunctionConfigurationCommand({
29+
FunctionName: functionArn,
30+
Environment: { Variables: variables },
31+
}),
32+
);
33+
}

tests/helpers/aws-sns-helper.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { SNSClient } from "@aws-sdk/client-sns";
2+
import { AWS_REGION } from "tests/constants/api-constants";
3+
import { logger } from "./pino-logger";
4+
5+
export const snsClient = new SNSClient({ region: AWS_REGION });

0 commit comments

Comments
 (0)