UNPKG

@featurevisor/core

Version:

Core package of Featurevisor for Node.js usage

133 lines 5.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.benchmarkPlugin = void 0; exports.benchmarkFeatureFlag = benchmarkFeatureFlag; exports.benchmarkFeatureVariation = benchmarkFeatureVariation; exports.benchmarkFeatureVariable = benchmarkFeatureVariable; exports.benchmarkFeature = benchmarkFeature; const sdk_1 = require("@featurevisor/sdk"); const config_1 = require("../config"); const builder_1 = require("../builder"); const prettyDuration_1 = require("../tester/prettyDuration"); function benchmarkFeatureFlag(f, featureKey, context, n) { const start = Date.now(); let value; for (let i = 0; i < n; i++) { value = f.isEnabled(featureKey, context); } const duration = Date.now() - start; return { value, duration, }; } function benchmarkFeatureVariation(f, featureKey, context, n) { const start = Date.now(); let value; for (let i = 0; i < n; i++) { value = f.getVariation(featureKey, context); } const duration = Date.now() - start; return { value, duration, }; } function benchmarkFeatureVariable(f, featureKey, variableKey, context, n) { const start = Date.now(); let value; for (let i = 0; i < n; i++) { value = f.getVariable(featureKey, variableKey, context); } const duration = Date.now() - start; return { value, duration, }; } async function benchmarkFeature(deps, options) { const { datasource, projectConfig } = deps; console.log(""); console.log(`Running benchmark for feature "${options.feature}"...`); console.log(""); console.log(`Building datafile containing all features for "${options.environment}"...`); const datafileBuildStart = Date.now(); const existingState = await datasource.readState(options.environment || false); const datafileContent = await (0, builder_1.buildDatafile)(projectConfig, datasource, { schemaVersion: options.schemaVersion || config_1.SCHEMA_VERSION, revision: "include-all-features", environment: options.environment || false, inflate: options.inflate, }, existingState); const datafileBuildDuration = Date.now() - datafileBuildStart; console.log(`Datafile build duration: ${datafileBuildDuration}ms`); console.log(`Datafile size: ${(JSON.stringify(datafileContent).length / 1024).toFixed(2)} kB`); if (options.inflate) { console.log(""); console.log("Features count:", Object.keys(datafileContent.features).length); console.log("Segments count:", Object.keys(datafileContent.segments).length); } console.log(""); const f = (0, sdk_1.createInstance)({ datafile: datafileContent, logLevel: "warn", }); console.log("...SDK initialized"); console.log(""); console.log(`Against context: ${JSON.stringify(options.context)}`); let output; if (options.variable) { // variable console.log(`Evaluating variable "${options.variable}" ${options.n} times...`); output = benchmarkFeatureVariable(f, options.feature, options.variable, options.context, options.n); } else if (options.variation) { // variation console.log(`Evaluating variation ${options.n} times...`); output = benchmarkFeatureVariation(f, options.feature, options.context, options.n); } else { // flag console.log(`Evaluating flag ${options.n} times...`); output = benchmarkFeatureFlag(f, options.feature, options.context, options.n); } console.log(""); console.log(`Evaluated value : ${JSON.stringify(output.value)}`); console.log(`Total duration : ${(0, prettyDuration_1.prettyDuration)(output.duration)}`); console.log(`Average duration: ${(0, prettyDuration_1.prettyDuration)(output.duration / options.n)}`); } exports.benchmarkPlugin = { command: "benchmark", handler: async ({ rootDirectoryPath, projectConfig, datasource, parsed }) => { await benchmarkFeature({ rootDirectoryPath, projectConfig, datasource, options: parsed, }, { environment: parsed.environment, feature: parsed.feature, n: parseInt(parsed.n, 10) || 1, context: parsed.context ? JSON.parse(parsed.context) : {}, variation: parsed.variation || undefined, variable: parsed.variable || undefined, schemaVersion: parsed.schemaVersion || undefined, inflate: parseInt(parsed.inflate, 10) || undefined, }); }, examples: [ { command: 'benchmark --environment=production --feature=my_feature -n=1000 --context=\'{"userId": "123"}\'', description: "Benchmark a feature flag", }, { command: 'benchmark --environment=production --feature=my_feature -n=1000 --context=\'{"userId": "123"}\' --variation', description: "Benchmark a feature variation", }, { command: 'benchmark --environment=production --feature=my_feature -n=1000 --context=\'{"userId": "123"}\' --variable=my-variable', description: "Benchmark a feature variable", }, ], }; //# sourceMappingURL=index.js.map