-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
70 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,48 @@ | ||
import { _resetColdStart, didFunctionColdStart, setColdStart } from "./cold-start"; | ||
import { _resetColdStart, didFunctionColdStart, setSandboxInit, isProactiveInitialization } from "./cold-start"; | ||
|
||
beforeEach(_resetColdStart); | ||
afterAll(_resetColdStart); | ||
|
||
describe("cold-start", () => { | ||
it("identifies cold starts on the first execution", () => { | ||
setColdStart(); | ||
setSandboxInit(0, 1); | ||
expect(didFunctionColdStart()).toEqual(true); | ||
}); | ||
|
||
it("identifies non-cold starts on subsequent executions", () => { | ||
setColdStart(); | ||
setSandboxInit(0, 1); | ||
expect(didFunctionColdStart()).toEqual(true); | ||
|
||
setColdStart(); | ||
setSandboxInit(0, 1); | ||
expect(didFunctionColdStart()).toEqual(false); | ||
|
||
setColdStart(); | ||
setSandboxInit(0, 1); | ||
expect(didFunctionColdStart()).toEqual(false); | ||
}); | ||
|
||
it("identifies proactive invocations on the first execution", () => { | ||
setSandboxInit(0, 100000); | ||
expect(didFunctionColdStart()).toEqual(false); | ||
expect(isProactiveInitialization()).toEqual(true); | ||
|
||
setSandboxInit(0, 1); | ||
expect(didFunctionColdStart()).toEqual(false); | ||
|
||
setSandboxInit(0, 1); | ||
expect(didFunctionColdStart()).toEqual(false); | ||
}); | ||
|
||
it("identifies non-proactive invocations on subsequent invocations", () => { | ||
setSandboxInit(0, 100000); | ||
expect(didFunctionColdStart()).toEqual(false); | ||
expect(isProactiveInitialization()).toEqual(true); | ||
|
||
setSandboxInit(0, 100000); | ||
expect(didFunctionColdStart()).toEqual(false); | ||
expect(isProactiveInitialization()).toEqual(false); | ||
|
||
setSandboxInit(0, 100000); | ||
expect(didFunctionColdStart()).toEqual(false); | ||
expect(isProactiveInitialization()).toEqual(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,45 @@ | ||
let functionDidColdStart = true; | ||
let proactiveInitialization = false; | ||
|
||
let isColdStartSet = false; | ||
|
||
/** | ||
* Use global variables to determine whether the container cold started | ||
* and if the start was proactively initialized | ||
* On the first container run, isColdStartSet and functionDidColdStart are true | ||
* For subsequent executions isColdStartSet will be true and functionDidColdStart will be false | ||
*/ | ||
export function setColdStart() { | ||
functionDidColdStart = !isColdStartSet; | ||
export function setSandboxInit(initTime: number, invocationStartTime: number) { | ||
if (!isColdStartSet && invocationStartTime - initTime > 10_000) { | ||
proactiveInitialization = true; | ||
functionDidColdStart = false; | ||
} else { | ||
functionDidColdStart = !isColdStartSet; | ||
proactiveInitialization = false; | ||
} | ||
isColdStartSet = true; | ||
} | ||
|
||
export function didFunctionColdStart() { | ||
export function didFunctionColdStart(): boolean { | ||
return functionDidColdStart; | ||
} | ||
|
||
export function getColdStartTag() { | ||
return `cold_start:${didFunctionColdStart()}`; | ||
export function isProactiveInitialization(): boolean { | ||
return proactiveInitialization; | ||
} | ||
|
||
export function getSandboxInitTags(): string[] { | ||
const tags = [`cold_start:${didFunctionColdStart()}`]; | ||
if (isProactiveInitialization()) { | ||
tags.push("proactive_initialization:true"); | ||
} | ||
|
||
return tags; | ||
} | ||
|
||
// For testing, reset the globals to their original values | ||
export function _resetColdStart() { | ||
functionDidColdStart = true; | ||
proactiveInitialization = false; | ||
isColdStartSet = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters