Skip to content

Commit

Permalink
feat: agoric run --verbose
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Jun 13, 2024
1 parent 276f555 commit 7162b28
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/agoric-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"@endo/patterns": "^1.4.0",
"@endo/promise-kit": "^1.1.2",
"@iarna/toml": "^2.2.3",
"@zip.js/zip.js": "^2.7.45",
"anylogger": "^0.21.0",
"chalk": "^5.2.0",
"commander": "^11.1.0",
Expand Down
70 changes: 70 additions & 0 deletions packages/agoric-cli/src/lib/bundles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* global Buffer */
import { BlobReader, TextWriter, ZipReader } from '@zip.js/zip.js';
import assert from 'node:assert/strict';
import fs from 'node:fs';

/** @import {Bundle} from '@agoric/swingset-vat'; */

const PACKAGE_NAME_RE = /(?<packageName>.*-v[\d.]+)\//;

/**
* @typedef CompartmentMap
* @property {string[]} tags
* @property {{compartment: string, module: string}} entry
* @property {Record<string, {name: string, label: string, location: string, modules: Record<string, {compartment: string, module: string}>}} compartments
*/

/** @param {Bundle} bundleObj*/
export const extractBundleInfo = async bundleObj => {
await null;
if (bundleObj.moduleFormat === 'endoZipBase64') {
const contents = Buffer.from(bundleObj.endoZipBase64, 'base64');
const zipBlob = new Blob([contents], { type: 'application/zip' });

const zipFileReader = new BlobReader(zipBlob);
const writer = new TextWriter('utf-8');

const zipReader = new ZipReader(zipFileReader);
const entries = await zipReader.getEntries();
await zipReader.close();

assert(entries[0].filename, 'compartment-map.json');
const cmapStr = await entries[0].getData(writer);
/** @type {CompartmentMap} */
const compartmentMap = JSON.parse(cmapStr);

const fileSizes = Object.fromEntries(
entries.map(e => [e.filename, e.compressedSize]),
);

return { compartmentMap, fileSizes };
}
};

// UNTIL https://github.com/endojs/endo/issues/1656
/** @param {string} bundleFilename */
export const statBundle = async bundleFilename => {
const bundle = fs.readFileSync(bundleFilename, 'utf8');
/** @type {Bundle} */
const bundleObj = JSON.parse(bundle);
console.log('\nBUNDLE', bundleObj.moduleFormat, bundleFilename);

const info = await extractBundleInfo(bundleObj);

/** @type {Record<string, number>} */
const byPackage = {};
let totalSize = 0;
for (const [filename, size] of Object.entries(info.fileSizes)) {
totalSize += size;
if (filename === 'compartment-map.json') {
continue;
}
const { packageName } = filename.match(PACKAGE_NAME_RE).groups;
byPackage[packageName] ||= 0;
byPackage[packageName] += size;
}

console.table(byPackage);

console.log('total size:', totalSize);
};
25 changes: 24 additions & 1 deletion packages/agoric-cli/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import startMain from './start.js';
import followMain from './follow.js';
import walletMain from './open.js';
import { makeWalletCommand } from './commands/wallet.js';
import { statBundle } from './lib/bundles.js';

const DEFAULT_DAPP_TEMPLATE = 'dapp-offer-up';
const DEFAULT_DAPP_URL_BASE = 'https://github.com/Agoric/';
Expand All @@ -28,6 +29,8 @@ const STAMP = '_agstate';
const filename = url.fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);

/** @import {CoreEvalPlan} from '@agoric/deploy-script-support/src/writeCoreEvalParts.js' */

const main = async (progname, rawArgs, powers) => {
const { anylogger, fs } = powers;
const log = anylogger('agoric');
Expand All @@ -46,6 +49,7 @@ const main = async (progname, rawArgs, powers) => {
return true;
}

// XXX exits process when fn resolves
function subMain(fn, args, options) {
return fn(progname, args, powers, options).then(
// This seems to be the only way to propagate the exit code.
Expand Down Expand Up @@ -287,7 +291,26 @@ const main = async (progname, rawArgs, powers) => {
.passThroughOptions(true)
.action(async (script, scriptArgs, _options, cmd) => {
const opts = { ...program.opts(), ...cmd.opts(), ...cmdOpts, scriptArgs };
return subMain(runMain, ['run', script], opts);
/** @type {Array<undefined | CoreEvalPlan | CoreEvalPlan[]>} */
const results = await runMain(progname, ['run', script], powers, opts);

if (opts.verbose) {
console.log(results.length, `script results`);
for (const result of results) {
const plans = result instanceof Array ? result : [result];
for (const plan of plans) {
if (plan) {
console.log('plan', plan.name);
for (const bundle of plan.bundles) {
await statBundle(bundle.fileName);
}
} else {
console.warn('no plan for script result');
}
}
}
}
return results;
});

baseCmd('deploy [script...]')
Expand Down
7 changes: 5 additions & 2 deletions packages/agoric-cli/src/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export const makeScriptLoader =
throw Error(`Installing unsafe plugins disabled; no pluginDir detected`);
}

const promises = [];
for await (const script of scripts) {
const moduleFile = path.resolve(process.cwd(), script);
const pathResolve = (...paths) => {
Expand Down Expand Up @@ -171,7 +172,7 @@ export { bootPlugin } from ${JSON.stringify(absPath)};

const main = mainNS.main;
if (typeof main === 'function') {
await main(allEndowments);
promises.push(main(allEndowments));
continue;
}

Expand All @@ -185,6 +186,8 @@ export { bootPlugin } from ${JSON.stringify(absPath)};

// Backward-compatibility: if the module doesn't export 'main', then
// assume it has a default export using the old 'deploy' API.
await defaultMain(allEndowments.home, allEndowments);
promises.push(defaultMain(allEndowments.home, allEndowments));
}

return Promise.all(promises);
};
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3785,6 +3785,11 @@
js-yaml "^3.10.0"
tslib "^2.4.0"

"@zip.js/zip.js@^2.7.45":
version "2.7.45"
resolved "https://registry.yarnpkg.com/@zip.js/zip.js/-/zip.js-2.7.45.tgz#823fe2789401d8c1d836ce866578379ec1bd6f0b"
integrity sha512-Mm2EXF33DJQ/3GWWEWeP1UCqzpQ5+fiMvT3QWspsXY05DyqqxWu7a9awSzU4/spHMHVFrTjani1PR0vprgZpow==

"@zkochan/js-yaml@0.0.6":
version "0.0.6"
resolved "https://registry.yarnpkg.com/@zkochan/js-yaml/-/js-yaml-0.0.6.tgz#975f0b306e705e28b8068a07737fa46d3fc04826"
Expand Down

0 comments on commit 7162b28

Please sign in to comment.