mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-01-14 00:27:55 +00:00
Docs: Update benchmark results
This commit is contained in:
@@ -1,68 +1,20 @@
|
||||
async function createStreamVisualizer(asyncIterable, options) {
|
||||
const { maxPoints, alpha, width, height, yDomain } = options;
|
||||
const d3 = await import('https://cdn.jsdelivr.net/npm/d3@7/+esm');
|
||||
|
||||
if (!Number.isFinite(maxPoints) || maxPoints < 1) {
|
||||
throw new Error('maxPoints must be a positive integer');
|
||||
}
|
||||
if (!Number.isFinite(alpha) || alpha < 0 || alpha > 1) {
|
||||
throw new Error('alpha must be between 0 and 1');
|
||||
}
|
||||
if (!Array.isArray(yDomain) || yDomain.length !== 2) {
|
||||
throw new Error('yDomain must be an array of [min, max]');
|
||||
}
|
||||
if (!Number.isFinite(width) || width <= 0) {
|
||||
throw new Error('width must be a positive number');
|
||||
}
|
||||
if (!Number.isFinite(height) || height <= 0) {
|
||||
throw new Error('height must be a positive number');
|
||||
}
|
||||
|
||||
export async function createStreamVisualizer(stream, { maxPoints, alpha, width, height, yDomain }) {
|
||||
const d3 = await import('d3');
|
||||
const data = [];
|
||||
let prevEma = null;
|
||||
let itemCount = 0;
|
||||
|
||||
for await (const value of asyncIterable) {
|
||||
if (!Number.isFinite(value)) {
|
||||
throw new Error('Stream must yield finite numeric values');
|
||||
}
|
||||
|
||||
const timestamp = Date.now();
|
||||
const ema = prevEma === null ? value : alpha * value + (1 - alpha) * prevEma;
|
||||
const point = { timestamp, value, ema };
|
||||
|
||||
if (itemCount < maxPoints) {
|
||||
data.push(point);
|
||||
itemCount++;
|
||||
} else {
|
||||
data.shift();
|
||||
data.push(point);
|
||||
}
|
||||
|
||||
prevEma = ema;
|
||||
let ema = null;
|
||||
|
||||
for await (const { timestamp, value } of stream) {
|
||||
ema = ema === null ? value : alpha * value + (1 - alpha) * ema;
|
||||
data.push({ timestamp, value, ema });
|
||||
if (data.length > maxPoints) data.shift();
|
||||
}
|
||||
|
||||
let path = '';
|
||||
if (data.length > 0) {
|
||||
const xDomain = data.length > 1 ? [data[0].timestamp, data[data.length - 1].timestamp] : [data[0].timestamp - 1000, data[0].timestamp];
|
||||
|
||||
const xScale = d3.scaleLinear()
|
||||
.domain(xDomain)
|
||||
.range([0, width]);
|
||||
|
||||
const yScale = d3.scaleLinear()
|
||||
.domain(yDomain)
|
||||
.range([height, 0]);
|
||||
|
||||
const line = d3.line()
|
||||
.x(d => xScale(d.timestamp))
|
||||
.y(d => yScale(d.ema))
|
||||
.curve(d3.curveMonotoneX)
|
||||
.defined(d => Number.isFinite(d.ema));
|
||||
|
||||
path = line(data) || '';
|
||||
}
|
||||
|
||||
|
||||
if (!data.length) return { data, path: '' };
|
||||
|
||||
const x = d3.scaleLinear().domain([data[0].timestamp, data[data.length - 1].timestamp]).range([0, width]);
|
||||
const y = d3.scaleLinear().domain(yDomain).range([height, 0]);
|
||||
const path = d3.line().x(d => x(d.timestamp)).y(d => y(d.ema))(data);
|
||||
|
||||
return { data, path };
|
||||
}
|
||||
export default createStreamVisualizer;
|
||||
Reference in New Issue
Block a user