Skip to content

Commit

Permalink
informational desc in perf test file
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Carpenito authored and Joe Carpenito committed Sep 13, 2022
1 parent ca7a1b1 commit 60a4dd2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
17 changes: 14 additions & 3 deletions src/index.perf.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/**
* This file is used and has been used a _flexible_ in order to
* constantly test different scenarios for performance. Since the
* level of performance we are overall concerned with here is
* effectively academic (gains measured in nanoseconds in some
* cases), there hasn't been a major focus on this being a robust
* suite of tests.
*
* More info in ./src/perf.md
*/

import AuthorizationService from "./index";

const printPerfMeasure = (startTime, endTime, unitMeasured) => {
Expand All @@ -11,6 +22,8 @@ const printPerfMeasure = (startTime, endTime, unitMeasured) => {
};

describe("Performance Benchmark", () => {
afterEach(() => AuthorizationService.resetPolicyMap());

it("useCan operation: 1 roles - 1,000,000 policies", () => {
/**
* Setup the auth service with a very performance intesive role config
Expand All @@ -33,11 +46,9 @@ describe("Performance Benchmark", () => {
const end = process.hrtime.bigint();
printPerfMeasure(start, end, "userCan()");
expect(can).toEqual(true);

AuthorizationService.resetPolicyMap();
});

it.only("useCan operation: 10,000 roles - 1,000 policies", () => {
it("useCan operation: 10,000 roles - 1,000 policies", () => {
/**
* Setup the auth service with a very performance intesive role config
*/
Expand Down
23 changes: 10 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,17 @@ const AuthorizationService = {
const rolesFound = policyMap.filter((policy) =>
roles.includes(policy.role)
);
const policiesFound = rolesFound.flatMap((role) => role.policies);

// const policiesFound = []
// let rolesFoundIterator = rolesFound.length
// while (rolesFoundIterator--) {
// let policiesIterator = rolesFound[rolesFoundIterator].policies.length
// while (policiesIterator--) {
// policiesFound.push(rolesFound[rolesFoundIterator].policies[policiesIterator])
// }
// }

// const policySet = [...new Set(policiesFound)]

// return policySet.includes(policy)
const policiesFound = [];
let rolesFoundIterator = rolesFound.length;
while (rolesFoundIterator--) {
let policiesIterator = rolesFound[rolesFoundIterator].policies.length;
while (policiesIterator--) {
policiesFound.push(
rolesFound[rolesFoundIterator].policies[policiesIterator]
);
}
}

return policiesFound.includes(policy);
},
Expand Down

0 comments on commit 60a4dd2

Please sign in to comment.