-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): support configuring console mode in APIs (#110)
Summary: Support setting the console output mode in MemLab programming API: ``` const {findLeaks, takeSnapshots} = require('memlab/api'); (async function () { const scenario = { url: () => 'https://www.facebook.com', }; const result = await takeSnapshots({scenario, consoleMode: 'SILENT'}); const leaks = findLeaks(result, {consoleMode: 'CONTINUOUS_TEST'}); })(); ``` Differential Revision: D53361876 fbshipit-source-id: 10f8b501a1253173571da49f106c0819ca66e898
- Loading branch information
1 parent
d8b8311
commit f5ab171
Showing
21 changed files
with
454 additions
and
178 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @oncall web_perf_infra | ||
*/ | ||
|
||
import type {MemLabConfig, Optional, PuppeteerConfig} from '@memlab/core'; | ||
import type {ConsoleMode} from './ConsoleModeManager'; | ||
import type {RunOptions} from '../API'; | ||
|
||
import consoleModeManager from './ConsoleModeManager'; | ||
import puppeteerConfigManager from './PuppeteerConfigManager'; | ||
|
||
export class APIState { | ||
modes: Optional<Set<ConsoleMode>>; | ||
puppeteerConfig: Optional<PuppeteerConfig>; | ||
} | ||
|
||
/** | ||
* Manage, save, and restore the current state of the API. | ||
*/ | ||
class APIStateManager { | ||
getAndUpdateState(config: MemLabConfig, options: RunOptions = {}) { | ||
const state = new APIState(); | ||
state.modes = consoleModeManager.getAndUpdateState(config, options); | ||
state.puppeteerConfig = puppeteerConfigManager.getAndUpdateState( | ||
config, | ||
options, | ||
); | ||
return state; | ||
} | ||
|
||
restoreState(config: MemLabConfig, state: APIState) { | ||
if (state.modes) { | ||
consoleModeManager.restoreState(config, state.modes); | ||
} | ||
if (state.puppeteerConfig) { | ||
puppeteerConfigManager.restoreState(config, state.puppeteerConfig); | ||
} | ||
} | ||
} | ||
|
||
export default new APIStateManager(); |
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 |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @oncall web_perf_infra | ||
*/ | ||
|
||
import {MemLabConfig, Nullable, Optional, utils} from '@memlab/core'; | ||
import type {RunOptions} from '../API'; | ||
|
||
/** | ||
* enum of all console mode options | ||
*/ | ||
export enum ConsoleMode { | ||
/** | ||
* mute all terminal output, equivalent to using `--silent` | ||
*/ | ||
SILENT = 'SILENT', | ||
/** | ||
* continuous test mode, no terminal output overwrite or animation, | ||
* equivalent to using `--sc` | ||
*/ | ||
CONTINUOUS_TEST = 'CONTINUOUS_TEST', | ||
/** | ||
* the default mode, there could be terminal output overwrite and animation, | ||
*/ | ||
DEFAULT = 'DEFAULT', | ||
/** | ||
* verbose mode, there could be terminal output overwrite and animation | ||
*/ | ||
VERBOSE = 'VERBOSE', | ||
} | ||
|
||
/** | ||
* Manage, save, and restore the current state of the Console modes. | ||
* @internal | ||
*/ | ||
class ConsoleModeManager { | ||
getAndUpdateState(config: MemLabConfig, options: RunOptions = {}) { | ||
return this.setConsoleMode(config, options.consoleMode, true); | ||
} | ||
|
||
restoreState(config: MemLabConfig, modes: Nullable<Set<ConsoleMode>>) { | ||
if (modes == null) { | ||
return; | ||
} | ||
this.resetConsoleMode(config); | ||
for (const mode of modes) { | ||
this.setConsoleMode(config, mode, false); | ||
} | ||
} | ||
|
||
private resetConsoleMode(config: MemLabConfig): void { | ||
config.muteConsole = false; | ||
config.isContinuousTest = false; | ||
config.verbose = false; | ||
} | ||
|
||
private setConsoleMode( | ||
config: MemLabConfig, | ||
mode: Optional<ConsoleMode>, | ||
reset: boolean, | ||
): Nullable<Set<ConsoleMode>> { | ||
let existingModes: Nullable<Set<ConsoleMode>> = | ||
this.getExistingConsoleModes(config); | ||
switch (mode) { | ||
case ConsoleMode.SILENT: | ||
reset && this.resetConsoleMode(config); | ||
config.muteConsole = true; | ||
break; | ||
case ConsoleMode.CONTINUOUS_TEST: | ||
reset && this.resetConsoleMode(config); | ||
config.isContinuousTest = true; | ||
break; | ||
case ConsoleMode.DEFAULT: | ||
reset && this.resetConsoleMode(config); | ||
config.muteConsole = false; | ||
config.isContinuousTest = false; | ||
break; | ||
case ConsoleMode.VERBOSE: | ||
reset && this.resetConsoleMode(config); | ||
config.verbose = true; | ||
break; | ||
default: | ||
if (mode == null) { | ||
existingModes = null; | ||
} else { | ||
throw utils.haltOrThrow(`Unknown console mode: ${mode}`); | ||
} | ||
} | ||
return existingModes; | ||
} | ||
|
||
private getExistingConsoleModes(config: MemLabConfig): Set<ConsoleMode> { | ||
const modes = new Set<ConsoleMode>([ConsoleMode.DEFAULT]); | ||
if (config.muteConsole) { | ||
modes.add(ConsoleMode.SILENT); | ||
modes.delete(ConsoleMode.DEFAULT); | ||
} | ||
if (config.isContinuousTest) { | ||
modes.add(ConsoleMode.CONTINUOUS_TEST); | ||
modes.delete(ConsoleMode.DEFAULT); | ||
} | ||
if (config.verbose) { | ||
modes.add(ConsoleMode.VERBOSE); | ||
} | ||
return modes; | ||
} | ||
} | ||
|
||
/** @internal */ | ||
export default new ConsoleModeManager(); |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @oncall web_perf_infra | ||
*/ | ||
|
||
import {MemLabConfig, PuppeteerConfig} from '@memlab/core'; | ||
import {RunOptions} from '../API'; | ||
|
||
/** | ||
* Manage, save, and restore the current state of the PuppeteerConfig. | ||
*/ | ||
class PuppeteerStateManager { | ||
getAndUpdateState(config: MemLabConfig, options: RunOptions = {}) { | ||
const existing = config.puppeteerConfig; | ||
config.puppeteerConfig = {...config.puppeteerConfig}; | ||
config.externalCookiesFile = options.cookiesFile; | ||
config.scenario = options.scenario; | ||
return existing; | ||
} | ||
|
||
restoreState(config: MemLabConfig, puppeteerConfig: PuppeteerConfig) { | ||
config.puppeteerConfig = puppeteerConfig; | ||
} | ||
} | ||
|
||
export default new PuppeteerStateManager(); |
Oops, something went wrong.