Skip to content

Commit

Permalink
Merge pull request #498 from gemini-testing/HERMIONE-1138.perf_net
Browse files Browse the repository at this point in the history
perf: optimize network load (preload plugins, dbUrls, split sql.js)
  • Loading branch information
KuznetsovRoman authored Sep 11, 2023
2 parents 77750a9 + 204462a commit 123ba6e
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 22 deletions.
4 changes: 2 additions & 2 deletions lib/server-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ export async function saveStaticFilesToReportDir(htmlReporter: HtmlReporter, plu
copyToReportDir(destPath, ['report.min.js', 'report.min.css'], staticFolder),
fs.copy(path.resolve(staticFolder, 'index.html'), path.resolve(destPath, 'index.html')),
fs.copy(path.resolve(staticFolder, 'icons'), path.resolve(destPath, 'icons')),
fs.copy(require.resolve('@gemini-testing/sql.js'), path.resolve(destPath, 'sql-wasm.js')),
fs.copy(require.resolve('@gemini-testing/sql.js/dist/sql-wasm.js'), path.resolve(destPath, 'sql-wasm.js')),
fs.copy(require.resolve('@gemini-testing/sql.js/dist/sql-wasm.wasm'), path.resolve(destPath, 'sql-wasm.wasm')),
copyPlugins(pluginConfig, destPath)
]);
}
Expand Down Expand Up @@ -173,7 +174,6 @@ export function urlPathNameEndsWith(currentUrl: string, searchString: string): b
export async function writeDatabaseUrlsFile(destPath: string, srcPaths: string[]): Promise<void> {
const jsonUrls = srcPaths.filter(p => urlPathNameEndsWith(p, '.json'));
const dbUrls = srcPaths.filter(p => urlPathNameEndsWith(p, '.db'));

const data = {
dbUrls,
jsonUrls
Expand Down
2 changes: 2 additions & 0 deletions lib/static/modules/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export const initStaticReport = () => {

performance?.mark?.(performanceMarks.DBS_LOADED);

plugins.preloadAll(dataFromStaticFile.config);

fetchDbDetails = fetchDbResponses.map(({url, status, data}) => ({url, status, success: !!data}));

const dataForDbs = fetchDbResponses.map(({data}) => data).filter(data => data);
Expand Down
22 changes: 15 additions & 7 deletions lib/static/modules/load-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,23 @@ const whitelistedDeps = {
// - an array with the string list of required dependencies and a function as the last item.
// The function will be called with the dependencies as arguments plus `options` arg.

const loadingPlugins = {};
const pendingPlugins = {};

export default async function loadPlugin(pluginName, pluginConfig) {
const getPluginScriptPath = pluginName => `plugins/${encodeURIComponent(pluginName)}/plugin.js`;

export function preloadPlugin(pluginName) {
loadingPlugins[pluginName] = loadingPlugins[pluginName] || getScriptText(pluginName);
}

export async function loadPlugin(pluginName, pluginConfig) {
if (pendingPlugins[pluginName]) {
return pendingPlugins[pluginName];
}

const pluginScriptPath = `plugins/${encodeURIComponent(pluginName)}/plugin.js`;
const scriptTextPromise = loadingPlugins[pluginName] || getScriptText(pluginName);

return pendingPlugins[pluginName] = Promise.resolve(pluginScriptPath)
.then(getScriptText)
return pendingPlugins[pluginName] = scriptTextPromise
.then(executePluginCode)
.then(plugin => initPlugin(plugin, pluginName, pluginConfig))
.then(null, err => {
Expand Down Expand Up @@ -103,7 +109,9 @@ function executePluginCode(code) {
return exec();
}

async function getScriptText(scriptUrl) {
const result = await axios.get(scriptUrl);
return result.data;
async function getScriptText(pluginName) {
const scriptUrl = getPluginScriptPath(pluginName);
const {data} = await axios.get(scriptUrl);

return data;
}
12 changes: 10 additions & 2 deletions lib/static/modules/plugins.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import loadPlugin from './load-plugin';
import {loadPlugin, preloadPlugin} from './load-plugin';

const plugins = Object.create(null);
const loadedPluginConfigs = [];

function preloadAll(config) {
if (!config || !config.pluginsEnabled || !Array.isArray(config.plugins)) {
return;
}

config.plugins.forEach(plugin => preloadPlugin(plugin.name));
}

async function loadAll(config) {
// if plugins are disabled, act like there are no plugins defined
if (!config || !config.pluginsEnabled || !Array.isArray(config.plugins)) {
Expand Down Expand Up @@ -49,4 +57,4 @@ function getLoadedConfigs() {
return loadedPluginConfigs;
}

module.exports = {loadAll, getLoadedConfigs, forEach, get};
module.exports = {preloadAll, loadAll, getLoadedConfigs, forEach, get};
4 changes: 3 additions & 1 deletion lib/static/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
<meta charset="utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.css"/>
<link id="dynamic-favicon" rel="icon" type="image/png" href="icons/favicon.png" />
<link id="dynamic-favicon" rel="icon" type="image/png" href="icons/favicon.png"/>
<link rel="preload" href="databaseUrls.json" as="fetch" crossorigin="anonymous"/>
<link rel="preload" href="sql-wasm.wasm" as="fetch" crossorigin="anonymous"/>
</head>
<body class="report">
<div id="app"></div>
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
"dependencies": {
"@babel/runtime": "^7.22.5",
"@gemini-testing/sql.js": "^1.0.1",
"@gemini-testing/sql.js": "^2.0.0",
"axios": "^0.18.1",
"better-sqlite3": "^8.5.0",
"bluebird": "^3.5.3",
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/static/modules/load-plugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import axios from 'axios';
import loadPlugin from 'lib/static/modules/load-plugin';
import {loadPlugin} from 'lib/static/modules/load-plugin';
import actionNames from 'lib/static/modules/action-names';
import * as actions from 'lib/static/modules/actions';
import * as selectors from 'lib/static/modules/selectors';
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/static/modules/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('static/modules/plugins', () => {
beforeEach(() => {
loadPluginStub = sandbox.stub();
plugins = proxyquire('lib/static/modules/plugins', {
'./load-plugin': {default: loadPluginStub}
'./load-plugin': {loadPlugin: loadPluginStub}
});
});

Expand Down

0 comments on commit 123ba6e

Please sign in to comment.