mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-01-14 08:37:56 +00:00
Docs: Update benchmark results
This commit is contained in:
@@ -1,9 +1,68 @@
|
||||
export async function createStreamVisualizer(i,o){
|
||||
const d=await import('d3'),{maxPoints:m,alpha:a,width:w,height:h,yDomain:y}=o,p=[],c=d.scaleLinear,f=d.line;
|
||||
let e;
|
||||
for await(const{timestamp:t,value:v}of i){e=e===undefined?v:a*v+(1-a)*e;p.push({timestamp:t,value:v,ema:e});p.length>m&&p.shift()}
|
||||
if(!p.length)return{data:[],path:''};
|
||||
const x=c().domain([p[0].timestamp,p.at(-1).timestamp]).range([0,w]),z=c().domain(y).range([h,0]),g=f().x(d=>x(d.timestamp)).y(d=>z(d.ema)).curve(d.curveLinear);
|
||||
return{data:p,path:g(p)}
|
||||
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');
|
||||
}
|
||||
|
||||
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 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) || '';
|
||||
}
|
||||
|
||||
return { data, path };
|
||||
}
|
||||
export default createStreamVisualizer;
|
||||
Reference in New Issue
Block a user