|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Bref\Runtime; |
| 4 | + |
| 5 | +/** |
| 6 | + * Tracks cold starts. |
| 7 | + * |
| 8 | + * @internal |
| 9 | + */ |
| 10 | +class ColdStartTracker |
| 11 | +{ |
| 12 | + private const FILE = '/tmp/.bref-cold-start'; |
| 13 | + |
| 14 | + private static bool $currentInvocationIsColdStart = false; |
| 15 | + private static ?float $coldStartBeginningTime = null; |
| 16 | + private static ?float $coldStartEndedTime = null; |
| 17 | + private static bool $hasFirstInvocationStarted = false; |
| 18 | + private static bool $wasProactiveInitialization = false; |
| 19 | + |
| 20 | + public static function init(): void |
| 21 | + { |
| 22 | + self::$coldStartBeginningTime = microtime(true); |
| 23 | + |
| 24 | + // We need to use a file to track cold starts only once. |
| 25 | + // This is because Bref's process can restart between invocations, |
| 26 | + // so we can't rely on static variables to track cold starts. |
| 27 | + self::$currentInvocationIsColdStart = ! file_exists(self::FILE); |
| 28 | + if (self::$currentInvocationIsColdStart) { |
| 29 | + touch(self::FILE); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Signals that the cold start has finished. |
| 35 | + */ |
| 36 | + public static function coldStartFinished(): void |
| 37 | + { |
| 38 | + self::$coldStartEndedTime = microtime(true); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Signals that a Lambda invocation has started. |
| 43 | + */ |
| 44 | + public static function invocationStarted(): void |
| 45 | + { |
| 46 | + // If the first invocation had happened already, then we are starting a 2nd invocation (or more) |
| 47 | + // so we are no longer in the cold start invocation anymore |
| 48 | + if (self::$hasFirstInvocationStarted) { |
| 49 | + self::$currentInvocationIsColdStart = false; |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + self::$hasFirstInvocationStarted = true; |
| 54 | + |
| 55 | + if (self::$currentInvocationIsColdStart) { |
| 56 | + // There was a cold start, let's figure out if it was a proactive initialization |
| 57 | + $timeElapsedSinceColdStartEnded = microtime(true) - self::$coldStartEndedTime; |
| 58 | + // If more than 100ms have passed since the cold start ended, we can assume the |
| 59 | + // Lambda sandbox was paused/frozen between the cold start and the first invocation |
| 60 | + // (100ms is an arbitrary value, we could use a lower value but I want to be conservative) |
| 61 | + // That means the Lambda sandbox was initialized proactively |
| 62 | + self::$wasProactiveInitialization = $timeElapsedSinceColdStartEnded > 0.1; |
| 63 | + } else { |
| 64 | + // There was no cold start, we are in a warm start |
| 65 | + self::$wasProactiveInitialization = false; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Timestamp of the beginning of the cold start. |
| 71 | + */ |
| 72 | + public static function getColdStartBeginningTime(): float |
| 73 | + { |
| 74 | + return self::$coldStartBeginningTime; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Timestamp of the end of the cold start. |
| 79 | + */ |
| 80 | + public static function getColdStartEndedTime(): float |
| 81 | + { |
| 82 | + return self::$coldStartEndedTime; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Returns `true` if the current Lambda invocation contained a cold start. |
| 87 | + * |
| 88 | + * This is `true` even if the cold start was a proactive initialization. |
| 89 | + * |
| 90 | + * This is no longer `true` once the second invocation (and subsequent invocations) start. |
| 91 | + */ |
| 92 | + public static function currentInvocationIsColdStart(): bool |
| 93 | + { |
| 94 | + return self::$currentInvocationIsColdStart; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Returns `true` if the current Lambda invocation contains a cold start that was "user-facing". |
| 99 | + * |
| 100 | + * "User-facing" means that the cold start duration was part of the invocation duration that the |
| 101 | + * invoker of the Lambda function experienced. |
| 102 | + * |
| 103 | + * For example, if the application is a web application, a "user-facing" cold start of 1 second |
| 104 | + * means that the response time of the first request contained a 1 second delay. |
| 105 | + */ |
| 106 | + public static function currentInvocationIsUserFacingColdStart(): bool |
| 107 | + { |
| 108 | + return self::currentInvocationIsColdStart() && ! self::wasProactiveInitialization(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns `true` if this Lambda sandbox was initialized proactively. |
| 113 | + */ |
| 114 | + public static function wasProactiveInitialization(): bool |
| 115 | + { |
| 116 | + return self::$wasProactiveInitialization; |
| 117 | + } |
| 118 | +} |
0 commit comments