From afe014c311ebf161076305515bb6c16295fa1cdb Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 12 Nov 2018 01:03:45 +0800 Subject: [PATCH] benchmark: try building the addons in napi benchmarks --- benchmark/common.js | 23 +++++++++++++++++++++++ benchmark/napi/function_args/index.js | 22 ++++++++++++++++++---- benchmark/napi/function_call/index.js | 26 ++++++++++++++++++++------ 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/benchmark/common.js b/benchmark/common.js index c76831b573dde6..460e4459fb9784 100644 --- a/benchmark/common.js +++ b/benchmark/common.js @@ -2,6 +2,29 @@ const child_process = require('child_process'); const http_benchmarkers = require('./_http-benchmarkers.js'); +const path = require('path'); + +exports.buildAddon = function(directory) { + const root = path.join(__dirname, '../'); + const gyp = path.join(root, 'deps/npm/node_modules/node-gyp/bin/node-gyp'); + const python = process.env.PYTHON || 'python'; + const ret = child_process.spawnSync(process.execPath, [ + gyp, + 'rebuild', + `--python=${python}`, + `--directory=${path.join(root, directory)}`, + `--nodedir=${root}` + ], { env: { ...process.env, MAKEFLAGS: '-j1' }}); + if (ret.error || ret.status !== 0) { + console.error('ERROR', ret.error); + console.error('STATUS', ret.status); + console.error('---- stdout ----'); + console.error(ret.stdout.toString()); + console.error('---- stderr ----'); + console.error(ret.stderr.toString()); + throw new Error(`Failed to build ${directory}`); + } +}; exports.buildType = process.features.debug ? 'Debug' : 'Release'; diff --git a/benchmark/napi/function_args/index.js b/benchmark/napi/function_args/index.js index df567dcfcc55bc..ee06bbc749a9c5 100644 --- a/benchmark/napi/function_args/index.js +++ b/benchmark/napi/function_args/index.js @@ -12,15 +12,29 @@ let napi; try { v8 = require(`./build/${common.buildType}/binding`); } catch { - console.error(`${__filename}: V8 Binding failed to load`); - process.exit(0); + try { + common.buildAddon('benchmark/napi/function_args'); + // eslint-disable-next-line node-core/no-duplicate-requires + v8 = require(`./build/${common.buildType}/binding`); + } catch (e) { + console.error(`${__filename}: V8 Binding failed to load`); + console.error(e); + process.exit(0); + } } try { napi = require(`./build/${common.buildType}/napi_binding`); } catch { - console.error(`${__filename}: NAPI-Binding failed to load`); - process.exit(0); + try { + common.buildAddon('benchmark/napi/function_args'); + // eslint-disable-next-line node-core/no-duplicate-requires + napi = require(`./build/${common.buildType}/napi_binding`); + } catch (e) { + console.error(`${__filename}: N-API Binding failed to load`); + console.error(e); + process.exit(0); + } } const argsTypes = ['String', 'Number', 'Object', 'Array', 'Typedarray', diff --git a/benchmark/napi/function_call/index.js b/benchmark/napi/function_call/index.js index 59063e500f7a84..41f0cc95eec047 100644 --- a/benchmark/napi/function_call/index.js +++ b/benchmark/napi/function_call/index.js @@ -6,16 +6,23 @@ const assert = require('assert'); const common = require('../../common.js'); - // this fails when we try to open with a different version of node, // which is quite common for benchmarks. so in that case, just // abort quietly. +let binding; try { - var binding = require(`./build/${common.buildType}/binding`); + binding = require(`./build/${common.buildType}/binding`); } catch { - console.error('misc/function_call.js Binding failed to load'); - process.exit(0); + try { + common.buildAddon('benchmark/napi/function_call'); + // eslint-disable-next-line node-core/no-duplicate-requires + binding = require(`./build/${common.buildType}/binding`); + } catch (e) { + console.error(`${__filename}: V8 Binding failed to load`); + console.error(e); + process.exit(0); + } } const cxx = binding.hello; @@ -23,8 +30,15 @@ let napi_binding; try { napi_binding = require(`./build/${common.buildType}/napi_binding`); } catch { - console.error('misc/function_call/index.js NAPI-Binding failed to load'); - process.exit(0); + try { + common.buildAddon('benchmark/misc/function_call'); + // eslint-disable-next-line node-core/no-duplicate-requires + napi_binding = require(`./build/${common.buildType}/napi_binding`); + } catch (e) { + console.error(`${__filename}: N-API Binding failed to load`); + console.error(e); + process.exit(0); + } } const napi = napi_binding.hello;