Skip to content
This repository has been archived by the owner on Jul 2, 2020. It is now read-only.

Commit

Permalink
fix: cli performance (#92)
Browse files Browse the repository at this point in the history
* fix: fix invoke lock

* fix: change dynamic require

* refactor: dynamic load plugins

* test: benchmark
  • Loading branch information
echosoar committed Mar 18, 2020
1 parent 84f0b05 commit 1416f97
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 19 deletions.
Empty file added benchmark/bc.test.ts
Empty file.
35 changes: 35 additions & 0 deletions benchmark/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { Suite } = require('benchmark');
const { resolve } = require('path');
const cp = require('child_process');
const cli = resolve(__dirname, '../packages/faas-cli/bin/fun.js');
const baseApp = resolve(
__dirname,
'../packages/serverless-invoke/test/fixtures/baseApp'
);

const func = () => {
cp.execSync(`cd ${baseApp};${cli} invoke -f http --clean=false`);
};
const funcHelp = () => {
cp.execSync(`${cli} -h`);
};
const funcTest = () => {
cp.execSync(`${cli} test -f ${resolve(__dirname, './bc.test.ts')}`);
};

const contenders = {
'faas-cli invoke': func,
'faas-cli help': funcHelp,
'faas-cli test': funcTest,
};

console.log('\nBenchmark:');
const bench = new Suite().on('cycle', e => {
console.log(' ' + e.target);
});

Object.keys(contenders).forEach(name => {
bench.add(name.padEnd(22, ' '), () => contenders[name]());
});

bench.run();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"@types/lodash": "^4.14.119",
"@types/mocha": "^5.2.5",
"@types/node": "^10.12.18",
"benchmark": "^2.1.4",
"gh-pages": "^1.2.0",
"lerna": "3",
"lerna-relinker": "^1.4.0",
Expand Down
71 changes: 52 additions & 19 deletions packages/faas-cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,54 @@
import { BaseCLI, getSpecFile } from '@midwayjs/fcli-command-core';
import { TestPlugin } from '@midwayjs/fcli-plugin-test';
import { InvokePlugin } from '@midwayjs/fcli-plugin-invoke';
import { PackagePlugin } from '@midwayjs/fcli-plugin-package';
import { DeployPlugin } from '@midwayjs/fcli-plugin-deploy';
import { AliyunFCPlugin } from '@midwayjs/fcli-plugin-fc';
import { CreatePlugin } from '@midwayjs/fcli-plugin-create';
import { saveYaml } from '@midwayjs/serverless-spec-builder';
import { execSync } from 'child_process';

const plugins = {
create: { mod: '@midwayjs/fcli-plugin-create', name: 'CreatePlugin' },
invoke: { mod: '@midwayjs/fcli-plugin-invoke', name: 'InvokePlugin' },
test: { mod: '@midwayjs/fcli-plugin-test', name: 'TestPlugin' },
package: [
{ mod: '@midwayjs/fcli-plugin-package', name: 'PackagePlugin' },
{ mod: '@midwayjs/fcli-plugin-fc', name: 'AliyunFCPlugin' },
],
deploy: [
{ mod: '@midwayjs/fcli-plugin-deploy', name: 'DeployPlugin' },
{ mod: '@midwayjs/fcli-plugin-fc', name: 'AliyunFCPlugin' },
],
};

const { Select } = require('enquirer');
export class CLI extends BaseCLI {
loadDefaultPlugin() {
this.core.addPlugin(CreatePlugin);
this.core.addPlugin(InvokePlugin);
this.core.addPlugin(TestPlugin);
this.core.addPlugin(PackagePlugin);
this.core.addPlugin(DeployPlugin);
this.core.addPlugin(AliyunFCPlugin);
const command = this.commands && this.commands[0];
// version not load plugin
if (this.argv.v || this.argv.version) {
return;
}
let needLoad = [];
if (!this.argv.h && command) {
if (plugins[command]) {
needLoad = needLoad.concat(plugins[command]);
}
} else {
// load all
Object.keys(plugins).forEach((cmd: string) => {
needLoad = needLoad.concat(plugins[cmd]);
});
}
needLoad.forEach(pluginInfo => {
try {
const mod = require(pluginInfo.mod);
if (mod[pluginInfo.name]) {
this.core.addPlugin(mod[pluginInfo.name]);
}
} catch (e) {}
});
}

async loadPlugins() {
await this.checkProvider();
await super.loadPlugins();
await this.loadDefaultOptions();
await super.loadPlugins();
}

async loadDefaultOptions() {
Expand All @@ -31,19 +58,23 @@ export class CLI extends BaseCLI {

if (this.argv.v || this.argv.version) {
this.displayVersion();
} else { // 默认没有command的时候展示帮助
} else {
// 默认没有command的时候展示帮助
this.argv.h = true;
}
}

displayVersion() {
const log = this.loadLog();
try {
const nodeVersion = execSync('node -v').toString().replace('\n', '');
const nodeVersion = execSync('node -v')
.toString()
.replace('\n', '');
log.log('Node.js'.padEnd(20) + nodeVersion);
} catch (E) {}

try { // midway-faas version
try {
// midway-faas version
const cliVersion = require('../package.json').version;
log.log('@midwayjs/faas-cli'.padEnd(20) + `v${cliVersion}`);
} catch (E) {}
Expand All @@ -69,16 +100,18 @@ export class CLI extends BaseCLI {
if (!this.spec.provider.name || this.argv.platform) {
let platform = this.argv.platform;
let needSelectPlatform = false;
if (!this.spec.provider.name) { // 未标明哪个平台
if (!this.spec.provider.name) {
// 未标明哪个平台
needSelectPlatform = true;
} else if (this.argv.platform === true) { // 使用 f xxx --platform
} else if (this.argv.platform === true) {
// 使用 f xxx --platform
needSelectPlatform = true;
}
if (needSelectPlatform) {
const prompt = new Select({
name: 'provider',
message: 'Which platform do you want to use?',
choices: ['阿里云函数计算 aliyun fc', '腾讯云函数 tencent scf']
choices: ['阿里云函数计算 aliyun fc', '腾讯云函数 tencent scf'],
});
const answers = await prompt.run();
platform = answers.split(' ')[1];
Expand Down

0 comments on commit 1416f97

Please sign in to comment.