Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Performance improvements when calling newGuid multiple times #1871

Merged
merged 1 commit into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions shared/AppInsightsCore/Tests/Unit/src/W3cTraceParentTests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Assert, AITestClass } from "@microsoft/ai-test-framework";
import { ITraceParent } from "../../../src/JavaScriptSDK.Interfaces/ITraceParent";
import { generateW3CId, newGuid } from "../../../src/JavaScriptSDK/CoreUtils";
import { dateNow } from "../../../src/JavaScriptSDK/HelperFuncs";
import { random32, randomValue } from "../../../src/JavaScriptSDK/RandomHelper";
import { formatTraceParent, isSampledFlag, isValidSpanId, isValidTraceId, isValidTraceParent, parseTraceParent } from "../../../src/JavaScriptSDK/W3cTraceParent";

export class W3cTraceParentTests extends AITestClass {
Expand Down Expand Up @@ -206,5 +209,83 @@ export class W3cTraceParentTests extends AITestClass {
} as ITraceParent));
}
});

this.testCase({
name: "org generate guid timings",
useFakeTimers: false,
test: () => {
let maxAttempts = 3;
let orgTotal = -1;
do {
let start = dateNow();
for (let lp = 0; lp < 10000; lp++) {
let newId = generateW3CId();
}

orgTotal = dateNow() - start;
maxAttempts--;
// Virtualized test servers are slooooow, so try and perform a couple of tests before failing
} while (orgTotal >= 300 && maxAttempts > 0);

Assert.ok(orgTotal < 300, "Total time for org generateW3cID = " + JSON.stringify({
orgTotal
}));
}
});

this.testCase({
name: "Validate generation",
useFakeTimers: false,
test: () => {
let key1 = generateW3CId();
let key2 = generateW3CId();

Assert.notEqual(key1, key2, "Make sure they are not the same");
Assert.equal(32, key1.length, "Key1 length: " + key1);
Assert.equal(32, key2.length, "Key2 length: " + key2);

Assert.equal("4", key1[12]);
Assert.equal("4", key2[12]);
}
});

this.testCase({
name: "generate newGuid timings",
useFakeTimers: false,
test: () => {
let maxAttempts = 3;
let guidTotal = -1;
do {
let start = dateNow();
for (let lp = 0; lp < 10000; lp++) {
let newId = newGuid();
}

guidTotal = dateNow() - start;
maxAttempts--;
// Virtualized test servers are slooooow, so try and perform a couple of tests before failing
} while (guidTotal >= 300 && maxAttempts > 0);

Assert.ok(guidTotal < 300, "Total time for org newGuid = " + JSON.stringify({
guidTotal
}));
}
});

this.testCase({
name: "Validate newGuid",
useFakeTimers: false,
test: () => {
let key1 = newGuid();
let key2 = newGuid();

Assert.notEqual(key1, key2, "Make sure they are not the same");
Assert.equal(36, key1.length, "Key1 length: " + key1);
Assert.equal(36, key2.length, "Key2 length: " + key2);

Assert.equal("4", key1[14]);
Assert.equal("4", key2[14]);
}
});
}
}
10 changes: 3 additions & 7 deletions shared/AppInsightsCore/src/JavaScriptSDK/CoreUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@ let _canUseCookies: boolean; // legacy supported config

// Added to help with minfication
export const Undefined = strShimUndefined;

export function newGuid(): string {
function randomHexDigit() {
return randomValue(15); // Get a random value from 0..15
}
const uuid = generateW3CId();

return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(GuidRegex, (c) => {
const r = (randomHexDigit() | 0), v = (c === "x" ? r : r & 0x3 | 0x8);
return v.toString(16);
});
return uuid.substring(0, 8) + "-" + uuid.substring(8, 12) + "-" + uuid.substring(12, 16) + "-" + uuid.substring(16, 20) + "-" + uuid.substring(20);
}

/**
Expand Down