Skip to content

feat: Initial timed stream implementation for application latencies #1639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/timed-stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {TransformOptions} from 'stream';

const {PassThrough} = require('stream');

interface Event {
name: string;
time: number;
}

export class TimedStream extends PassThrough {
events: Event[] = [];
constructor(options: TransformOptions) {
// highWaterMark of 1 is needed to respond to each row
super({
...options,
highWaterMark: 1,
transform: (chunk: any, encoding: any, callback: any) => {
this.events.push({name: '_read called', time: Date.now()});
callback(null, chunk);
},
});
this.startTime = 0n;
this.totalDuration = 0n;
this.handleBeforeRow = this.handleBeforeRow.bind(this);
this.handleAfterRow = this.handleAfterRow.bind(this);
this.on('before_row', this.handleBeforeRow);
this.on('after_row', this.handleAfterRow);
}

_read(size: any) {
this.events.push({name: '_read called', time: Date.now()});
super._read(size);
this.events.push({name: 'emit before_row', time: Date.now()});
this.emit('before_row');
process.nextTick(() => {
this.events.push({name: 'emit after_row', time: Date.now()});
this.emit('after_row');
});
// Defer the after call to the next tick of the event loop
}

handleBeforeRow() {
this.events.push({name: 'handle before_row', time: Date.now()});
this.startTime = process.hrtime.bigint();
}

handleAfterRow() {
const endTime = process.hrtime.bigint();
const duration = endTime - this.startTime;
this.totalDuration += duration;
this.events.push({name: 'handle after_row', time: Date.now()});
}

getTotalDurationMs() {
return Number(this.totalDuration / 1_000_000n);
}
}
91 changes: 91 additions & 0 deletions test/timed-stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import {describe, it} from 'mocha';
import {PassThrough, Readable} from 'stream';
import {TimedStream} from '../src/timed-stream';
import * as assert from 'assert';

// set up streams
function* numberGenerator(n: number) {
for (let i = 0; i < n; i++) {
yield String(i) + '\n';
}
}

describe.only('Bigtable/TimedStream', () => {

Check failure on line 13 in test/timed-stream.ts

View workflow job for this annotation

GitHub Actions / lint

'describe.only' is restricted from being used
describe('with handlers', () => {
describe('with no delay from server', () => {
it('should measure the total time accurately for a series of 30 rows', done => {
const sourceStream = Readable.from(numberGenerator(30));
const timedStream = new TimedStream({});
// @ts-ignore
sourceStream.pipe(timedStream as unknown as WritableStream);
setTimeout(async () => {
// iterate stream
timedStream.on('data', async (chunk: any) => {
process.stdout.write(chunk.toString());
// Simulate 1 second of busy work
const startTime = Date.now();
while (Date.now() - startTime < 1000) {
/* empty */
}
});
timedStream.on('end', () => {
const totalMilliseconds = timedStream.getTotalDurationMs();
try {
assert(totalMilliseconds > 29000);
assert(totalMilliseconds < 31000);
// TODO: Add check for 30 BEFORE events. I only see a couple
done();
} catch (e) {
done(e);
}
});
}, 500);
});
});
describe('with delay from server', () => {
it('should measure the total time accurately for a series of 10 rows', done => {
const dataEvents = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(i =>
i.toString(),
);
const sourceStream = new PassThrough();
const timedStream = new TimedStream({});
// @ts-ignore
sourceStream.pipe(timedStream);

setTimeout(async () => {
// iterate stream
timedStream.on('data', async (chunk: any) => {
process.stdout.write(chunk.toString());
// Simulate 1 second of busy work
const startTime = Date.now();
while (Date.now() - startTime < 1000) {
/* empty */
}
});
timedStream.on('end', () => {
// print results
try {
const totalMilliseconds = timedStream.getTotalDurationMs();
console.log(totalMilliseconds);
// totalMilliseconds should be around 10 seconds, 1 per row
assert(totalMilliseconds > 9000);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you check out this code locally this assert fails which is a problem.

assert(totalMilliseconds < 11000);
done();
} catch (e) {
done(e);
}
});
}, 500);

setInterval(() => {
if (dataEvents.length > 0) {
const dataEvent = dataEvents.shift();
sourceStream.write(dataEvent);
} else {
sourceStream.emit('end');
}
}, 5000);
});
});
});
});
Loading