From 84b462cecb020273e180994413cab243d90f5aae Mon Sep 17 00:00:00 2001 From: mikestock-nimble Date: Tue, 19 Dec 2023 15:24:01 +0000 Subject: [PATCH] added a basic performance test for v4 establishments and trusts --- .../scripts/get-establishment.js | 47 +++++++++ .../scripts/get-trust.js | 79 +++++++++++++++ .../Integration/V4/GenerateDataTests.cs | 95 +++++++++++++++++++ 3 files changed, 221 insertions(+) create mode 100644 Dfe.Academies.Performance/scripts/get-establishment.js create mode 100644 Dfe.Academies.Performance/scripts/get-trust.js create mode 100644 TramsDataApi.Test/Integration/V4/GenerateDataTests.cs diff --git a/Dfe.Academies.Performance/scripts/get-establishment.js b/Dfe.Academies.Performance/scripts/get-establishment.js new file mode 100644 index 000000000..2f8406a18 --- /dev/null +++ b/Dfe.Academies.Performance/scripts/get-establishment.js @@ -0,0 +1,47 @@ +import http from 'k6/http'; +import { check, sleep } from 'k6'; + +export const options = { + vus: 800, + duration: '30s', + // httpDebug: 'full', +}; + +const baseUrl = "https://localhost:5001/v4"; + +export default function () { + getEstablishmentsForTrust("UKPRN37cda865-dbe0-426f-9485-84c1cf3d8513"); + + getEstablishmentByUkPrn("UKPRN0b2ecaa7-4ec3-4138-805b-3d7d904962a4"); + + sleep(1); +} + +function getEstablishmentsForTrust(ukprn) { + const res = http.get(`${baseUrl}/establishments/trust?trustUkPrn=${ukprn}`, { + headers: getHeaders() + }); + + isStatus200(res); +} + +function getEstablishmentByUkPrn(ukprn) { + const res = http.get(`${baseUrl}/establishment/${ukprn}`, { + headers: getHeaders() + }); + + isStatus200(res); +} + +function isStatus200(res) { + check(res, { + 'status is 200': (r) => r.status === 200, + }); +} + +function getHeaders() { + return { + "ApiKey": "app-key", + "Content-Type": "application/json" + }; +} \ No newline at end of file diff --git a/Dfe.Academies.Performance/scripts/get-trust.js b/Dfe.Academies.Performance/scripts/get-trust.js new file mode 100644 index 000000000..fce8f0322 --- /dev/null +++ b/Dfe.Academies.Performance/scripts/get-trust.js @@ -0,0 +1,79 @@ +import http from 'k6/http'; +import { check, sleep } from 'k6'; + +export const options = { + vus: 400, + duration: '30s', + // httpDebug: 'full', +}; + +const baseUrl = "https://localhost:5001/v4"; + +export default function () { + + searchTrustByName("Name91b71279-6799-4f2c-843d-8e6b35ae7ffa"); + + getTrustByUkPrn("UKPRN692c06a7-089a-46be-ae3b-e4d6fee8126e"); + + searchTrustByUkPrn("UKPRN692c06a7-089a-46be-ae3b-e4d6fee8126e"); + + getTrustByCompaniesHouseNumber("CompaniesHouseNumbercfa7f419-f27e-4f6c-9266-9a9c560ae6a3"); + + getTrustByReferenceNumber("GroupID97be1318-42e1-4527-a75e-595ba4768153"); + + sleep(1); +} + +function searchTrustByName(name) { + const res = http.get(`${baseUrl}/trusts?groupName=${name}&page=1&count=10`, { + headers: getHeaders() + }); + + isStatus200(res); +} + +function searchTrustByUkPrn(ukprn) { + const res = http.get(`${baseUrl}/trusts?ukPrn=${ukprn}&page=1&count=10`, { + headers: getHeaders() + }); + + isStatus200(res); +} + +function getTrustByUkPrn(ukprn) { + const res = http.get(`${baseUrl}/trust/${ukprn}`, { + headers: getHeaders() + }); + + isStatus200(res); +} + +function getTrustByCompaniesHouseNumber(companiesHouseNumber) { + const res = http.get(`${baseUrl}/trust/companiesHouseNumber/${companiesHouseNumber}`, { + headers: getHeaders() + }); + + isStatus200(res); + +} + +function getTrustByReferenceNumber(trustReferenceNumber) { + const res = http.get(`${baseUrl}/trust/trustReferenceNumber/${trustReferenceNumber}`, { + headers: getHeaders() + }); + + isStatus200(res); +} + +function isStatus200(res) { + check(res, { + 'status is 200': (r) => r.status === 200, + }); +} + +function getHeaders() { + return { + "ApiKey": "app-key", + "Content-Type": "application/json" + }; +} \ No newline at end of file diff --git a/TramsDataApi.Test/Integration/V4/GenerateDataTests.cs b/TramsDataApi.Test/Integration/V4/GenerateDataTests.cs new file mode 100644 index 000000000..669f1886e --- /dev/null +++ b/TramsDataApi.Test/Integration/V4/GenerateDataTests.cs @@ -0,0 +1,95 @@ +using Dfe.Academies.Academisation.Data; +using Dfe.Academies.Domain.Establishment; +using Dfe.Academies.Domain.Trust; +using System.Collections.Generic; +using System.Linq; +using TramsDataApi.Test.Fixtures; +using TramsDataApi.Test.Helpers; +using Xunit; + +namespace TramsDataApi.Test.Integration.V4 +{ + [Collection(ApiTestCollection.ApiTestCollectionName)] + public class GenerateDataTests + { + private readonly ApiTestFixture _apiFixture; + + public GenerateDataTests(ApiTestFixture fixture) + { + _apiFixture = fixture; + } + + [Fact(Skip = "Generate data for performance testing on an adhoc basis")] + public void GenerateTrustData() + { + using var context = _apiFixture.GetMstrContext(); + context.ChangeTracker.AutoDetectChangesEnabled = false; + + for (var idx = 0; idx < 4000; idx++) + { + CreateDataSet(context); + } + } + + private static TrustDataSet CreateDataSet(MstrContext context) + { + var trust = DatabaseModelBuilder.BuildTrust(); + context.Add(trust); + context.SaveChanges(); + + var establishments = new List(); + + for (var idx = 0; idx < 3; idx++) + { + var establishment = DatabaseModelBuilder.BuildEstablishment(); + var ifdPipeline = DatabaseModelBuilder.BuildIfdPipeline(); + ifdPipeline.GeneralDetailsUrn = establishment.PK_GIAS_URN; + + var establishmentDataSet = new EstablishmentDataSet() + { + Establishment = establishment, + IfdPipeline = ifdPipeline + }; + + context.Establishments.Add(establishment); + context.IfdPipelines.Add(ifdPipeline); + + establishments.Add(establishmentDataSet); + } + + context.SaveChanges(); + + var trustToEstablishmentLinks = LinkTrustToEstablishments(trust, establishments.Select(d => d.Establishment).ToList()); + + context.EducationEstablishmentTrusts.AddRange(trustToEstablishmentLinks); + + context.SaveChanges(); + + var result = new TrustDataSet() + { + Trust = trust, + Establishments = establishments + }; + + return result; + } + + private static List LinkTrustToEstablishments(Trust trust, List establishments) + { + var result = new List(); + + establishments.ForEach(establishment => + { + var educationEstablishmentTrust = new EducationEstablishmentTrust() + { + TrustId = (int)trust.SK, + EducationEstablishmentId = (int)establishment.SK + }; + + result.Add(educationEstablishmentTrust); + }); + + return result; + } + } +}