-
Notifications
You must be signed in to change notification settings - Fork 62
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
danieljbruce
wants to merge
1
commit into
main
Choose a base branch
from
359913994-timed-stream-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+148
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', () => { | ||
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); | ||
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); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.