Skip to content

Commit

Permalink
Isolate benchmarking code because npm run is erroneous and sadly runs…
Browse files Browse the repository at this point in the history
… other code too
  • Loading branch information
odeke-em committed Oct 28, 2024
1 parent fbc460b commit 2488550
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const lessComparator = (a, b) => {
* a map of functionNames to the percentiles of RAM usage and time spent.
*/
export async function runBenchmarks(runners: Function[], done: Function) {
const nRuns = 10000;
const nRuns = 20000;
const nWarmups = Math.round(nRuns / 8);
const benchmarkValues = {_totalRuns: nRuns, _warmRuns: nWarmups};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,15 @@ const {
} = require('@opentelemetry/sdk-trace-node');
const {SimpleSpanProcessor} = require('@opentelemetry/sdk-trace-base');
import {humanizeBytes, humanizeTime, runBenchmarks} from './benchmark';
const {diag, DiagConsoleLogger, DiagLogLevel} = require('@opentelemetry/api');
// diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);

const {ObservabilityOptions} = require('../src/instrument');
const {
getTracer,
startTrace,
traceConfig,
ObservabilityOptions,
} = require('../src/instrument');

const selectSql = 'SELECT 1';
const updateSql = 'UPDATE FOO SET BAR=1 WHERE BAZ=2';
Expand Down Expand Up @@ -124,6 +131,16 @@ async function setup(
});
}

interface percentiles {
p50: number;
p50_s: string;
}

interface description {
ram: percentiles;
timeSpent: percentiles;
}

describe('Benchmarking Database', () => {
if (!process.env.SPANNER_RUN_BENCHMARKS) {
console.log(
Expand Down Expand Up @@ -207,23 +224,6 @@ describe('Benchmarking Database', () => {
});
},

/*
async function databaseGetTransactionAsync() {
const tx = await database.getTransction();
try {
await tx!.begin();
return await tx!.runUpdate(updateSql);
} catch (e) {
console.log(e);
return null;
} finally {
console.log('tx.end');
tx!.end();
console.log('exiting');
}
},
*/
async function databaseRunTransactionAsyncTxRunUpdate() {
const withTx = async tx => {
await tx!.begin();
Expand Down Expand Up @@ -274,16 +274,6 @@ describe('Benchmarking Database', () => {
});
}

interface percentiles {
p50: number;
p50_s: string;
}

interface description {
ram: percentiles;
timeSpent: percentiles;
}

it('Database runs compared', async () => {
const traced = await setItUp(true);
const untraced = await setItUp(false);
Expand Down Expand Up @@ -327,3 +317,151 @@ describe('Benchmarking Database', () => {
function percentDiff(orig, fresh) {
return ((Number(fresh) - Number(orig)) * 100.0) / Number(orig);
}

describe('Benchmark getTracer', () => {
it('No tracerProvider/global tracerProvider', async () => {
console.log('tracerProvider');
const results = await benchmarkStartTrace();

console.log(
`Total Runs: ${results['_totalRuns']}\nWarm up runs: ${results['_warmRuns']}`
);

for (const method in results) {
const values = results[method];
if (typeof values !== 'object') {
continue;
}
const desc = values as description;
const ram = desc.ram;
const timeSpent = desc!.timeSpent;
console.log(`${method}`);
console.log(`\tRAM (${ram.p50_s})`);
console.log(`\tTimeSpent (${timeSpent.p50_s})`);
}
});
});

function benchmarkGetTracer(): Promise<Map<string, unknown>> {
const customTracerProvider = new NodeTracerProvider();
let trapDoorCalled = false;

const runners: Function[] = [
function getTracerNullTracerProviderUnsetGlobalTracerProvider() {
return getTracer(null);
},

function getTracerDefinedTracerProvider() {
return getTracer(customTracerProvider);
},

function getTracerRegisteredGlobally() {
if (!trapDoorCalled) {
customTracerProvider.register();
trapDoorCalled = true;
}
return getTracer(null);
},
];

return new Promise(resolve => {
runBenchmarks(runners, results => {
resolve(results);
});
});
}

function benchmarkStartSpan(): Promise<Map<string, unknown>> {
const customTracerProvider = new NodeTracerProvider();
let trapDoorCalled = false;

const runners: Function[] = [
function withNullTracerProviderUnsetGlobalTracerProvider() {
return new Promise(resolve => {
getTracer(null).startActiveSpan('aSpan', {}, span => {
resolve(span);
});
});
},

function withTracerDefinedTracerProvider() {
return new Promise(resolve => {
getTracer(customTracerProvider).startActiveSpan('aSpan', {}, span => {
resolve(span);
});
});
},

function getTracerRegisteredGlobally() {
if (!trapDoorCalled) {
customTracerProvider.register();
trapDoorCalled = true;
}
return new Promise(resolve => {
getTracer(null).startActiveSpan('aSpan', {}, span => {
resolve(span);
});
});
},
];

return new Promise(resolve => {
runBenchmarks(runners, results => {
resolve(results);
});
});
}

function benchmarkStartTrace(): Promise<Map<string, unknown>> {
const customTracerProvider = new NodeTracerProvider();
let trapDoorCalled = false;

const runners: Function[] = [
async function withNullTracerProviderUnsetGlobalTracerProvider() {
const promise = new Promise((resolve, reject) => {
const opts: typeof traceConfig = {
opts: {tracerProvider: null},
};
startTrace('aspan', null, span => {
span.end();
resolve(span);
});
});
return promise;
},

function withTracerDefinedTracerProvider() {
return new Promise(resolve => {
const opts: typeof traceConfig = {
opts: {tracerProvider: customTracerProvider},
};
startTrace('aspan', opts, span => {
span.end();
resolve(span);
});
});
},

function withTracerRegisteredGlobally() {
if (!trapDoorCalled) {
customTracerProvider.register();
trapDoorCalled = true;
}
return new Promise(resolve => {
const opts: typeof traceConfig = {
opts: {tracerProvider: null},
};
startTrace('aspan', opts, span => {
span.end();
resolve(span);
});
});
},
];

return new Promise(resolve => {
runBenchmarks(runners, results => {
resolve(results);
});
});
}
1 change: 0 additions & 1 deletion observability-test/spanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const {
AsyncHooksContextManager,
} = require('@opentelemetry/context-async-hooks');
import {promisify} from '@google-cloud/promisify';
import {runBenchmarks} from './benchmark';

const {ObservabilityOptions} = require('../src/instrument');
import {SessionPool} from '../src/session-pool';
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"samples-test-with-archived": "cd samples/ && npm link ../ && npm test-with-archived && cd ../",
"samples-test": "cd samples/ && npm link ../ && npm test && cd ../",
"system-test": "mocha build/system-test --timeout 1600000",
"observability-benchmark": "mocha build/observability-benchmark --timeout 1600000",
"observability-test": "mocha build/observability-test --timeout 1600000",
"cleanup": "mocha scripts/cleanup.js --timeout 30000",
"test": "mocha build/test build/test/common build/observability-test",
Expand All @@ -43,6 +44,7 @@
"prepare": "npm run compile-protos && npm run compile",
"pretest": "npm run compile",
"presystem-test": "npm run compile",
"preobservability-benchmark": "npm run compile",
"preobservability-test": "npm run compile",
"proto": "compileProtos src",
"docs-test": "linkinator docs",
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"test/**/*.ts",
"system-test/*.ts",
"benchmark/*.ts",
"observability-benchmark/*.ts",
"observability-test/*.ts"
]
}

0 comments on commit 2488550

Please sign in to comment.