|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + * @oncall web_perf_infra |
| 9 | + */ |
| 10 | + |
| 11 | +import {config, E2EStepInfo, RunMetaInfo} from '@memlab/core'; |
| 12 | + |
| 13 | +import fs from 'fs-extra'; |
| 14 | +import {FileManager, RunMetaInfoManager, utils} from '@memlab/core'; |
| 15 | +import BaseResultReader from './BaseResultReader'; |
| 16 | + |
| 17 | +/** |
| 18 | + * A utility entity to read all MemLab files generated from |
| 19 | + * baseline, target and final heap snapshots. |
| 20 | + * |
| 21 | + * The most useful feature of this class is when you have |
| 22 | + * three separate snapshots (baseline, target, and final) |
| 23 | + * that are not taken from MemLab, but you still would |
| 24 | + * like to use the `findLeaks` to detect memory leaks: |
| 25 | + * |
| 26 | + * ```javascript |
| 27 | + * const {SnapshotResultReader, findLeaks} = require('@memlab/api'); |
| 28 | + * |
| 29 | + * // baseline, target, and final are file paths of heap snapshot files |
| 30 | + * const reader = SnapshotResultReader.fromSnapshots(baseline, target, final); |
| 31 | + * const leaks = await findLeaks(reader); |
| 32 | + * ``` |
| 33 | + */ |
| 34 | +export default class SnapshotResultReader extends BaseResultReader { |
| 35 | + private baselineSnapshot: string; |
| 36 | + private targetSnapshot: string; |
| 37 | + private finalSnapshot: string; |
| 38 | + |
| 39 | + /** |
| 40 | + * build a result reader |
| 41 | + * @param workDir absolute path of the directory where the data |
| 42 | + * and generated files of the memlab run were stored |
| 43 | + */ |
| 44 | + protected constructor( |
| 45 | + baselineSnapshot: string, |
| 46 | + targetSnapshot: string, |
| 47 | + finalSnapshot: string, |
| 48 | + ) { |
| 49 | + const fileManager = new FileManager(); |
| 50 | + const workDir = fileManager.generateTmpHeapDir(); |
| 51 | + fs.ensureDirSync(workDir); |
| 52 | + super(workDir); |
| 53 | + this.baselineSnapshot = baselineSnapshot; |
| 54 | + this.targetSnapshot = targetSnapshot; |
| 55 | + this.finalSnapshot = finalSnapshot; |
| 56 | + this.checkSnapshotFiles(); |
| 57 | + this.createMetaFilesOnDisk(fileManager, workDir); |
| 58 | + } |
| 59 | + |
| 60 | + private createMetaFilesOnDisk( |
| 61 | + fileManager: FileManager, |
| 62 | + workDir: string, |
| 63 | + ): void { |
| 64 | + fileManager.initDirs(config, {workDir}); |
| 65 | + const visitOrder = this.getInteractionSteps(); |
| 66 | + const snapSeqFile = fileManager.getSnapshotSequenceMetaFile({workDir}); |
| 67 | + fs.writeFileSync(snapSeqFile, JSON.stringify(visitOrder, null, 2), 'UTF-8'); |
| 68 | + } |
| 69 | + |
| 70 | + private checkSnapshotFiles(): void { |
| 71 | + if ( |
| 72 | + !fs.existsSync(this.baselineSnapshot) || |
| 73 | + !fs.existsSync(this.targetSnapshot) || |
| 74 | + !fs.existsSync(this.finalSnapshot) |
| 75 | + ) { |
| 76 | + throw utils.haltOrThrow( |
| 77 | + 'invalid file path of baseline, target, or final heap snapshots', |
| 78 | + ); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Build a result reader from baseline, target, and final heap snapshot files. |
| 84 | + * The three snapshot files do not have to be in the same directory. |
| 85 | + * @param baselineSnapshot file path of the baseline heap snapshot |
| 86 | + * @param targetSnapshot file path of the target heap snapshot |
| 87 | + * @param finalSnapshot file path of the final heap snapshot |
| 88 | + * @returns the ResultReader instance |
| 89 | + * |
| 90 | + * * **Examples**: |
| 91 | + * ```javascript |
| 92 | + * const {SnapshotResultReader, findLeaks} = require('@memlab/api'); |
| 93 | + * |
| 94 | + * // baseline, target, and final are file paths of heap snapshot files |
| 95 | + * const reader = SnapshotResultReader.fromSnapshots(baseline, target, final); |
| 96 | + * const leaks = await findLeaks(reader); |
| 97 | + * ``` |
| 98 | + */ |
| 99 | + static fromSnapshots( |
| 100 | + baselineSnapshot: string, |
| 101 | + targetSnapshot: string, |
| 102 | + finalSnapshot: string, |
| 103 | + ): SnapshotResultReader { |
| 104 | + return new SnapshotResultReader( |
| 105 | + baselineSnapshot, |
| 106 | + targetSnapshot, |
| 107 | + finalSnapshot, |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @internal |
| 113 | + */ |
| 114 | + public static from(workDir = ''): BaseResultReader { |
| 115 | + throw utils.haltOrThrow('SnapshotResultReader.from is not supported'); |
| 116 | + return new BaseResultReader(workDir); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * get all snapshot files related to this SnapshotResultReader |
| 121 | + * @returns an array of snapshot file's absolute path |
| 122 | + * |
| 123 | + * * **Examples**: |
| 124 | + * ```javascript |
| 125 | + * const {SnapshotResultReader} = require('@memlab/api'); |
| 126 | + * |
| 127 | + * // baseline, target, and final are file paths of heap snapshot files |
| 128 | + * const reader = SnapshotResultReader.fromSnapshots(baseline, target, final); |
| 129 | + * const paths = reader.getSnapshotFiles(); |
| 130 | + * ``` |
| 131 | + */ |
| 132 | + public getSnapshotFiles(): string[] { |
| 133 | + return [this.baselineSnapshot, this.targetSnapshot, this.finalSnapshot]; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @internal |
| 138 | + */ |
| 139 | + public getSnapshotFileDir(): string { |
| 140 | + throw utils.haltOrThrow( |
| 141 | + 'SnapshotResultReader getSnapshotFileDir() method is not supported', |
| 142 | + ); |
| 143 | + return ''; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * browser interaction step sequence |
| 148 | + * @returns an array of browser interaction step information |
| 149 | + * |
| 150 | + * * **Examples**: |
| 151 | + * ```javascript |
| 152 | + * const {SnapshotResultReader} = require('@memlab/api'); |
| 153 | + * |
| 154 | + * // baseline, target, and final are file paths of heap snapshot files |
| 155 | + * const reader = SnapshotResultReader.fromSnapshots(baseline, target, final); |
| 156 | + * const paths = reader.getInteractionSteps(); |
| 157 | + * ``` |
| 158 | + */ |
| 159 | + public getInteractionSteps(): E2EStepInfo[] { |
| 160 | + return this.fileManager.createVisitOrderWithSnapshots( |
| 161 | + this.baselineSnapshot, |
| 162 | + this.targetSnapshot, |
| 163 | + this.finalSnapshot, |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * @internal |
| 169 | + * general meta data of the browser interaction run |
| 170 | + * @returns meta data about the entire browser interaction |
| 171 | + * |
| 172 | + * * **Examples**: |
| 173 | + * ```javascript |
| 174 | + * const {SnapshotResultReader} = require('@memlab/api'); |
| 175 | + * |
| 176 | + * // baseline, target, and final are file paths of heap snapshot files |
| 177 | + * const reader = SnapshotResultReader.fromSnapshots(baseline, target, final); |
| 178 | + * const metaInfo = reader.getRunMetaInfo(); |
| 179 | + * ``` |
| 180 | + */ |
| 181 | + public getRunMetaInfo(): RunMetaInfo { |
| 182 | + return new RunMetaInfoManager().loadRunMetaExternalTemplate(); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * @internal |
| 187 | + */ |
| 188 | + public cleanup(): void { |
| 189 | + // do nothing |
| 190 | + } |
| 191 | +} |
0 commit comments