From 148f6c28c45e6e7dd29e06eae0b030a2a24f5366 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Sat, 21 May 2022 11:54:40 +0800 Subject: [PATCH] feat enable faster load mode Signed-off-by: MiaoWoo --- lerna.json | 4 -- packages/common/src/version.ts | 35 +++++++++++++++ packages/container/src/index.ts | 4 +- packages/core/src/index.ts | 71 +++++++++++++++++------------- packages/nashorn/src/index.ts | 5 ++- packages/polyfill/src/index.ts | 1 + packages/polyfill/src/node-shim.ts | 20 +++++---- 7 files changed, 93 insertions(+), 47 deletions(-) create mode 100644 packages/common/src/version.ts diff --git a/lerna.json b/lerna.json index 95776d95..7fd3ea2b 100644 --- a/lerna.json +++ b/lerna.json @@ -8,10 +8,6 @@ "command": { "run": { "stream": true - }, - "publish": { - "access": "public", - "registry": "https://repo.yumc.pw/repository/npm-hosted/" } } } diff --git a/packages/common/src/version.ts b/packages/common/src/version.ts new file mode 100644 index 00000000..283cecff --- /dev/null +++ b/packages/common/src/version.ts @@ -0,0 +1,35 @@ +export type Version = [string, string, string] + +export class VersionUtils { + static isEqual(version: string, targetVersion: string): boolean { + return version == targetVersion + } + static isGreaterOrEqual(version: string, targetVersion: string): boolean { + const v1 = parseVersion(version) + const v2 = parseVersion(targetVersion) + + return ( + v1[0] > v2[0] || + (v1[0] === v2[0] && v1[1] > v2[1]) || + (v1[0] === v2[0] && v1[1] === v2[1] && v1[2] >= v2[2]) + ) + } + static isGreater(version: string, targetVersion: string): boolean { + const v1 = parseVersion(version) + const v2 = parseVersion(targetVersion) + + return ( + v1[0] > v2[0] || + (v1[0] === v2[0] && v1[1] > v2[1]) || + (v1[0] === v2[0] && v1[1] === v2[1] && v1[2] > v2[2]) + ) + } +} + +function parseVersion(version: string = ""): Version { + const v: Version = ['0', '0', '0'] + + version.split(".").forEach((value, i) => (v[i] = value)) + + return v +} diff --git a/packages/container/src/index.ts b/packages/container/src/index.ts index c5a69e32..07bd752d 100644 --- a/packages/container/src/index.ts +++ b/packages/container/src/index.ts @@ -102,8 +102,8 @@ export const MavenDepend = (groupId: string, artifactId: string, version: string const loadedMavenDepend = new Set() export function loadMavenDepend(groupId: string, artifactId: string, version: string, recursion = false) { + const key = `${groupId}:${artifactId}:${version}` try { - const key = `${groupId}:${artifactId}:${version}` if (loadedMavenDepend.has(key)) { return } console.info('loading maven dependency', key) let [pom, _] = base.loadMavenDepend(groupId, artifactId, version) @@ -137,7 +137,7 @@ export function loadMavenDepend(groupId: string, artifactId: string, version: st } } } catch (error: any) { - console.warn('attachMavenDepend failed. Error: ' + error) + console.warn('load maven dependency', key, 'failed. Error:', error) if (global.debug) { console.ex(error) } diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 870a7b1f..4829d002 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -6,6 +6,7 @@ console.i18n("ms.core.ioc.completed", { scope: global.scope, time: (Date.now() - import * as yaml from 'js-yaml' import http from '@ccms/common/dist/http' import * as fs from '@ccms/common/dist/fs' +import { VersionUtils } from '@ccms/common/dist/version' const UUID = Java.type('java.util.UUID') @@ -25,6 +26,11 @@ class MiaoScriptCore { this.loadServerConsole() this.loadPlugins() process.emit('core.after.enable') + console.i18n("ms.core.engine.completed", { + loader: base.version, + version: 'v' + global.ScriptEngineVersion, + time: (Date.now() - global.ScriptEngineStartTime) / 1000 + }) return () => this.disable() } @@ -123,6 +129,30 @@ function loadMiaoScriptConfig() { global.ScriptSlowExecuteTime = global.ScriptEngineConfig.slow_execute || 50 } +function createCore() { + let corePackageStartTime = new Date().getTime() + container.bind(ContainerInstance).toConstantValue(container) + container.bind(plugin.PluginInstance).toConstantValue(base.getInstance()) + container.bind(plugin.PluginFolder).toConstantValue('plugins') + let type = detectServer() + + process.emit('core.before.initialize.detect') + console.i18n("ms.core.initialize.detect", { scope: global.scope, type }) + container.bind(server.ServerType).toConstantValue(type) + container.bind(server.ServerChecker).toSelf().inSingletonScope() + container.bind(server.NativePluginManager).toSelf().inSingletonScope() + process.emit('core.after.initialize.detect') + + process.emit('core.before.package.initialize') + console.i18n("ms.core.package.initialize", { scope: global.scope, type }) + require(`${global.scope}/${type}`).default(container) + require(`${global.scope}/plugin`) + container.load(buildProviderModule()) + console.i18n("ms.core.package.completed", { scope: global.scope, type, time: (Date.now() - corePackageStartTime) / 1000 }) + process.emit('core.after.package.initialize') + return container.get(MiaoScriptCore) +} + function initialize() { process.emit('core.before.initialize') loadMiaoScriptConfig() @@ -130,43 +160,22 @@ function initialize() { global.setGlobal('loadCoreScript', loadCoreScript) loadCoreScript('initialize') try { - let corePackageStartTime = new Date().getTime() - container.bind(ContainerInstance).toConstantValue(container) - container.bind(plugin.PluginInstance).toConstantValue(base.getInstance()) - container.bind(plugin.PluginFolder).toConstantValue('plugins') - let type = detectServer() - - process.emit('core.before.initialize.detect') - console.i18n("ms.core.initialize.detect", { scope: global.scope, type }) - container.bind(server.ServerType).toConstantValue(type) - container.bind(server.ServerChecker).toSelf().inSingletonScope() - container.bind(server.NativePluginManager).toSelf().inSingletonScope() - process.emit('core.after.initialize.detect') - - process.emit('core.before.package.initialize') - console.i18n("ms.core.package.initialize", { scope: global.scope, type }) - require(`${global.scope}/${type}`).default(container) - require(`${global.scope}/plugin`) - container.load(buildProviderModule()) - console.i18n("ms.core.package.completed", { scope: global.scope, type, time: (Date.now() - corePackageStartTime) / 1000 }) - process.emit('core.after.package.initialize') - - let disable = container.get(MiaoScriptCore).enable() - console.i18n("ms.core.engine.completed", { - loader: base.version, - version: 'v' + global.ScriptEngineVersion, - time: (Date.now() - global.ScriptEngineStartTime) / 1000 - }) - process.emit('core.after.initialize') - return disable + let core = createCore() + if (VersionUtils.isGreaterOrEqual(base.version, '0.22.0')) { return core } + return core.enable() } catch (error: any) { if (console.console) { - console.i18n("ms.core.initialize.error", { error }) + console.i18n("core.initialize.error", { error }) console.ex(error) } else { error.printStackTrace() } - return () => console.i18n('ms.core.engine.disable.abnormal') + process.emit('core.initialize.error') + return { + enable: () => console.i18n('ms.core.engine.disable.abnormal') + } + } finally { + process.emit('core.after.initialize') } } diff --git a/packages/nashorn/src/index.ts b/packages/nashorn/src/index.ts index da5634fe..2ca6c527 100644 --- a/packages/nashorn/src/index.ts +++ b/packages/nashorn/src/index.ts @@ -48,7 +48,10 @@ declare global { scope: string logger: any debug: boolean - level: string + /** + * 引擎日志等级 + */ + ScriptEngineLoggerLevel: string /** * 引擎配置 */ diff --git a/packages/polyfill/src/index.ts b/packages/polyfill/src/index.ts index df76957c..93b5b7b8 100644 --- a/packages/polyfill/src/index.ts +++ b/packages/polyfill/src/index.ts @@ -14,3 +14,4 @@ global.setGlobal('Proxy', require('./proxy').Proxy) global.setGlobal('XMLHttpRequest', require('./xml-http-request').XMLHttpRequest) global.setGlobal('Blob', require('blob-polyfill').Blob) console.i18n("ms.polyfill.completed", { time: (new Date().getTime() - polyfillStartTime) / 1000 }) +export default true diff --git a/packages/polyfill/src/node-shim.ts b/packages/polyfill/src/node-shim.ts index 7f27041f..342c7d05 100644 --- a/packages/polyfill/src/node-shim.ts +++ b/packages/polyfill/src/node-shim.ts @@ -15,7 +15,7 @@ const DelayQueue = Java.type('java.util.concurrent.DelayQueue') const JavaScriptTask = Java.type(base.getJavaScriptTaskClass().name) const threadCount = new AtomicInteger(0) -const threadGroup = new ThreadGroup("@ccms/ployfill-micro-task") +const threadGroup = new ThreadGroup("@ccms/micro-task") const microTaskPool = new ThreadPoolExecutor( 100, 200, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(300), @@ -76,16 +76,16 @@ class Process extends EventEmitter { class EventLoop { private eventLoopMainThread = undefined private eventLoopTaskQueue = new DelayQueue() - private taskExecTimeout = 3 + private taskExecuteTimeout = 3000 private fixedThreadPool = undefined constructor() { - this.taskExecTimeout = parseInt(process.env.MS_NODE_EVENT_LOOP_TIMEOUT) || 3 + this.taskExecuteTimeout = parseInt(process.env.MS_TASK_EXECUTE_TIMEOUT) || 3000 this.fixedThreadPool = new ThreadPoolExecutor( 1, 1, 0, TimeUnit.SECONDS, - new LinkedBlockingQueue(300), + new LinkedBlockingQueue(500), new ThreadFactory((run: any) => { - let thread = new Thread(run, "@ccms/node-shim/event-loop-exec") + let thread = new Thread(run, "@ccms/event-loop") thread.setDaemon(true) return thread })) @@ -115,7 +115,7 @@ class EventLoop { this.intervalTasks = undefined this.eventLoopMainThread = undefined } - }, "@ccms/node-shim/event-loop") + }, "@ccms/event-loop") this.eventLoopMainThread.setDaemon(true) process.on('exit', () => { this.eventLoopMainThread.interrupt() @@ -137,7 +137,9 @@ class EventLoop { if (!callback) { throw new Error(`task ${name} callback function can't be null!`) } - if (this.fixedThreadPool.isShutdown()) { return console.warn(`FixedThreadPool isTerminated! ignore Task ${name}!`) } + if (this.fixedThreadPool.isShutdown()) { + return console.warn(`FixedThreadPool isTerminated! ignore Task ${name}!`) + } try { this.fixedThreadPool.submit(new Callable({ call: () => { @@ -153,13 +155,13 @@ class EventLoop { } } } - })).get(this.taskExecTimeout, TimeUnit.SECONDS) + })).get(this.taskExecuteTimeout, TimeUnit.MILLISECONDS) } catch (error: any) { if (error instanceof InterruptedException) { return console.warn(`FixedThreadPool isInterrupted exit! Task ${name} exec exit!`) } if (error instanceof TimeoutException) { - return console.warn(`Task ${name} => ${callback} exec time greater than ${this.taskExecTimeout}s!`) + return console.warn(`Task ${name} => ${callback} exec time greater than ${this.taskExecuteTimeout}s!`) } throw error.getCause && error.getCause() || error }