Skip to content

Commit

Permalink
[BUG] Performance improvements when calling newGuid multiple times (l…
Browse files Browse the repository at this point in the history
…ike 10,000) #1870
  • Loading branch information
MSNev committed Aug 1, 2022
1 parent 200e0f1 commit d73e913
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
69 changes: 69 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,71 @@ export class W3cTraceParentTests extends AITestClass {
} as ITraceParent));
}
});

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

let orgTotal = dateNow() - start;

Assert.ok(orgTotal < 200, "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 start = dateNow();
for (let lp = 0; lp < 10000; lp++) {
let newId = newGuid();
}

let guidTotal = dateNow() - start;

Assert.ok(guidTotal < 200, "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

0 comments on commit d73e913

Please sign in to comment.