@lensmcp/core 1.25.43 → 1.25.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/patterns.d.ts +11 -1
- package/lib/patterns.js +1 -1
- package/lib/performance-window.d.ts +11 -5
- package/lib/performance-window.js +1 -1
- package/package.json +2 -2
package/lib/patterns.d.ts
CHANGED
|
@@ -28,7 +28,17 @@ export interface DbInLoopOptions {
|
|
|
28
28
|
threshold?: number;
|
|
29
29
|
}
|
|
30
30
|
/**
|
|
31
|
-
*
|
|
31
|
+
* The DB calls a loop is judged by (`dbInLoop`): all it made — or, for a loop of a stream that says how many fell within
|
|
32
|
+
* two neighbouring seconds (`dbBurst`), those. A stream's pump reads every few seconds for as long as the stream is open:
|
|
33
|
+
* only a burst inside it is a loop's work (issues/closed/foodguard-148).
|
|
34
|
+
*/
|
|
35
|
+
export declare function loopDbCalls(loop: {
|
|
36
|
+
dbCallsInsideLoop?: unknown;
|
|
37
|
+
dbBurst?: unknown;
|
|
38
|
+
stream?: unknown;
|
|
39
|
+
}): number;
|
|
40
|
+
/**
|
|
41
|
+
* Flag `loop` events whose DB calls (`loopDbCalls`) exceed the threshold.
|
|
32
42
|
* Events are expected to carry `raw.kind === 'loop'` with a `loop`
|
|
33
43
|
* attribute matching `LoopAttrs`.
|
|
34
44
|
*/
|
package/lib/patterns.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var g=Object.defineProperty;var l=(n,o)=>g(n,"name",{value:o,configurable:!0});var y=Object.defineProperty,p=l((n,o)=>y(n,"name",{value:o,configurable:!0}),"l");export function loopDbCalls(n){const o=typeof n.dbCallsInsideLoop=="number"?n.dbCallsInsideLoop:0;return n.stream===!0&&typeof n.dbBurst=="number"?Math.min(o,n.dbBurst):o}l(loopDbCalls,"loopDbCalls"),p(loopDbCalls,"loopDbCalls");export function detectDbInLoop(n,o={}){const a=o.threshold??5,d=[];for(const t of n){const e=t.raw??null;if(e?.kind!=="loop"||!e.loop)continue;const i=loopDbCalls(e.loop);if(i>a){const r=e.loop.dbCallsInsideLoop??0,s=i<r;d.push({kind:"db-in-loop",severity:"warning",title:s?`${i} DB calls within two seconds of a stream's loop (${r} over its life)`:`${i} DB calls inside a loop (${e.loop.iterations??"?"} iterations)`,evidence:[t.id],details:{dbCallsInsideLoop:r,...s?{dbBurst:i,stream:!0}:{},iterations:e.loop.iterations,durationMs:e.loop.durationMs,threshold:a},detectedAt:t.timestamp})}}return d}l(detectDbInLoop,"detectDbInLoop"),p(detectDbInLoop,"detectDbInLoop");const I=new Set(["egress","db-query","redis-op","ai-call"]);function M(n){const o=n.raw??null;if(!o||typeof o.kind!="string"||!I.has(o.kind))return;const a=typeof o.durationMs=="number"?o.durationMs:void 0;if(a===void 0)return;const d=typeof o.startedAt=="number"?o.startedAt:n.timestamp-a,t=n.context??{};return{id:n.id,start:d,end:d+a,label:n.title,...typeof t.requestId=="string"?{requestId:t.requestId}:{},...typeof o.project=="string"?{project:o.project}:typeof t.projectName=="string"?{project:t.projectName}:{}}}l(M,"M"),p(M,"timedCall");function w(n){const o=n.raw??null,a=o?.kind==="span"?o.span:void 0;if(!a||typeof a.name!="string"||a.name.startsWith("nest.request "))return;const d=typeof a.durationMs=="number"?a.durationMs:void 0;if(d===void 0||d<=0)return;const t=typeof a.startTime=="number"?a.startTime:n.timestamp-d,e=n.context??{};return{id:n.id,start:t,end:t+d,label:a.name,...typeof e.requestId=="string"?{requestId:e.requestId}:{},...typeof o?.project=="string"?{project:o.project}:typeof e.projectName=="string"?{project:e.projectName}:{}}}l(w,"g"),p(w,"timedSpan");export function abandonedAwaitOf(n,o,a={}){const d=a.thresholdMs??250,t=M(n);if(!t||!t.requestId||t.end-t.start<d)return;let e;for(const r of o){const s=w(r);!s||s.requestId!==t.requestId||s.project!==void 0&&t.project!==void 0&&s.project!==t.project||s.start>t.start||s.end<=t.start||(!e||s.start>e.start||s.start===e.start&&s.end<e.end)&&(e=s)}if(!e)return;const i=Math.round(t.end-e.end);if(!(i<d))return{kind:"abandoned-await",severity:"warning",title:`${e.label} ended ${i}ms before a call it started had finished (${t.label})`,evidence:[e.id,t.id],details:{span:e.label,call:t.label,ranOnMs:i,spanMs:Math.round(e.end-e.start),callMs:Math.round(t.end-t.start),thresholdMs:d,...t.project?{project:t.project}:{}},detectedAt:n.timestamp}}l(abandonedAwaitOf,"abandonedAwaitOf"),p(abandonedAwaitOf,"abandonedAwaitOf");export function detectAbandonedAwait(n,o={}){const a=[];for(const d of n){const t=abandonedAwaitOf(d,n,o);t&&a.push(t)}return a}l(detectAbandonedAwait,"detectAbandonedAwait"),p(detectAbandonedAwait,"detectAbandonedAwait");export function detectNPlusOne(n,o={}){const a=o.threshold??3,d=new Map;for(const e of n){const i=e.raw??null;if(i?.kind!=="db-query"||!i.query?.signature)continue;const r=i.query.signature;let s=d.get(r);s||(s=[],d.set(r,s)),s.push(e)}const t=[];for(const[e,i]of d)i.length>=a&&t.push({kind:"n+1-query",severity:"warning",title:`Query repeated ${i.length}\xD7 \u2014 likely N+1`,evidence:i.map(r=>r.id),details:{signature:e,count:i.length,threshold:a},detectedAt:i[i.length-1].timestamp});return t}l(detectNPlusOne,"detectNPlusOne"),p(detectNPlusOne,"detectNPlusOne");export function detectRenderStorm(n,o={}){const a=o.threshold??5,d=o.windowMs??250,t=new Map;for(const i of n){if(i.source!=="react")continue;const r=i.raw??null;if(r?.kind!=="render"||!r.render?.componentInstanceId)continue;const s=r.render.componentInstanceId;let c=t.get(s);c||(c=[],t.set(s,c)),c.push(i)}const e=[];for(const[i,r]of t){r.sort((c,f)=>c.timestamp-f.timestamp);let s=0;for(let c=0;c<r.length;c++){for(;r[c].timestamp-r[s].timestamp>d;)s++;const f=c-s+1;if(f>a){const u=r.slice(s,c+1),m=u[0].raw.render?.componentName??i;e.push({kind:"render-storm",severity:"warning",title:`${m} rendered ${f}\xD7 in ${d}ms`,evidence:u.map(h=>h.id),details:{componentInstanceId:i,count:f,windowMs:d,threshold:a},detectedAt:u[u.length-1].timestamp});break}}}return e}l(detectRenderStorm,"detectRenderStorm"),p(detectRenderStorm,"detectRenderStorm");export function findSlowPath(n,o,a){const d=new Map,t=new Set;function e(r){const s=d.get(r);if(s)return s;if(t.has(r))return{path:[r],totalDurationMs:a(r)};t.add(r);const c=a(r),f=o.get(r)??[];let u;for(const h of f){const b=e(h);(!u||b.totalDurationMs>u.totalDurationMs)&&(u=b)}t.delete(r);const m=u?{path:[r,...u.path],totalDurationMs:c+u.totalDurationMs}:{path:[r],totalDurationMs:c};return d.set(r,m),m}l(e,"e"),p(e,"best");let i={path:[],totalDurationMs:0};for(const r of n){const s=e(r);s.totalDurationMs>i.totalDurationMs&&(i=s)}return i}l(findSlowPath,"findSlowPath"),p(findSlowPath,"findSlowPath");
|
|
@@ -11,8 +11,9 @@ import { type LensmcpThresholds } from './thresholds.js';
|
|
|
11
11
|
* The rules are the lens's own, so the gate and the dashboard never disagree about what is slow:
|
|
12
12
|
* - a route, a statement or a page is slow when it ran at or above its limit MORE THAN ONCE (the perf reducer's rule —
|
|
13
13
|
* a one-off spike, the first call after a rebuild, is not a breach; it is still in the numbers);
|
|
14
|
-
* - N+1: one request ran the same statement `nPlusOne` times or more
|
|
15
|
-
*
|
|
14
|
+
* - N+1: one request ran the same statement `nPlusOne` times or more — a streaming one, within one burst: its reads
|
|
15
|
+
* seconds apart are a stream's periodic work over its life, not a loop (issues/closed/foodguard-147);
|
|
16
|
+
* - DB in a loop: more than `dbInLoop` DB calls inside one loop — a stream's loop, within two seconds (`loopDbCalls`).
|
|
16
17
|
* A streaming response's time is how long it stayed open, not how slow it was: it is counted, never timed. A page the dev
|
|
17
18
|
* server reloaded (`cause: 'hmr'`) is left out, and a hard load is timed from the app's entry (`appReadyMs`), not from
|
|
18
19
|
* the dev server serving it.
|
|
@@ -133,11 +134,13 @@ export declare class PerformanceWindow {
|
|
|
133
134
|
private readonly sql;
|
|
134
135
|
private readonly drops;
|
|
135
136
|
private readonly pages;
|
|
136
|
-
/** Per open request: its statements' runs, until its `server-request` closes it. */
|
|
137
|
+
/** Per open request: its statements' runs, and in which seconds, until its `server-request` closes it. */
|
|
137
138
|
private readonly open;
|
|
138
139
|
/** A request's route once its `server-request` is seen. */
|
|
139
140
|
private readonly routeOf;
|
|
140
|
-
/**
|
|
141
|
+
/** The requests whose `server-request` said they streamed: their statements are judged by burst. */
|
|
142
|
+
private readonly streams;
|
|
143
|
+
/** N+1 per route and statement: the worst count, and where — and how many of its requests were streams. */
|
|
141
144
|
private readonly repeated;
|
|
142
145
|
private readonly loops;
|
|
143
146
|
constructor(limits: PerformanceLimits, waivers?: readonly PerformanceWaiver[]);
|
|
@@ -153,7 +156,10 @@ export declare class PerformanceWindow {
|
|
|
153
156
|
* told apart.
|
|
154
157
|
*/
|
|
155
158
|
private dropped;
|
|
156
|
-
/**
|
|
159
|
+
/**
|
|
160
|
+
* A request ended (or the window did): its repeated statements become N+1 records — a stream's by its worst burst. A
|
|
161
|
+
* request not seen ending is judged as a request: what it was is not known (a stream says so only when it closes).
|
|
162
|
+
*/
|
|
157
163
|
private close;
|
|
158
164
|
private loop;
|
|
159
165
|
private page;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var L=Object.defineProperty;var v=(o,t)=>L(o,"name",{value:t,configurable:!0});var N=Object.defineProperty,f=v((o,t)=>N(o,"name",{value:t,configurable:!0}),"f");import{resolveThresholds as E}from"./thresholds.js";export const SQL_DROPS_KIND="db-dropped",PERFORMANCE_EVENT_KINDS=["server-request","db-query",SQL_DROPS_KIND,"loop","page-visit"];export function performanceLimits(...o){let t={};for(const r of o)r&&(t={...t,...I(r)});const e=E(t);return{slowRouteMs:e.slowRouteMs,slowQueryMs:e.slowQueryMs,nPlusOne:e.nPlusOne,dbInLoop:e.dbInLoop,slowPageMs:e.slowPageMs}}v(performanceLimits,"performanceLimits"),f(performanceLimits,"performanceLimits");function I(o){const t={};for(const[e,r]of Object.entries(o))typeof r=="number"&&Number.isFinite(r)&&r>=0&&(t[e]=r);return t}v(I,"S"),f(I,"resolveThresholdsOnly");const $=f(o=>o&&typeof o=="object"?o:{},"obj"),m=f(o=>typeof o=="string"&&o?o:void 0,"str"),c=f(o=>typeof o=="number"&&Number.isFinite(o)?o:void 0,"num"),Q=2e3,j=5e3,R=2e4,k=f(()=>({count:0,times:[],slow:0,max:0,evidence:[]}),"newTiming");function q(o,t,e,r){if(o.max=Math.max(o.max,t),t>=e&&(o.slow+=1,o.evidence.length<5&&o.evidence.push(r)),o.times.length<Q)o.times.push(t);else{let n=0;for(let a=1;a<o.times.length;a++)o.times[a]<o.times[n]&&(n=a);t>o.times[n]&&(o.times[n]=t)}}v(q,"v"),f(q,"time");function P(o,t){return o.length?o[Math.min(o.length-1,Math.floor(o.length*t))]:0}v(P,"P"),f(P,"percentile");function x(o){const t=[...o.times].sort((e,r)=>e-r);return{count:o.count,timed:o.times.length,p50Ms:Math.round(P(t,.5)),p95Ms:Math.round(P(t,.95)),maxMs:Math.round(o.max),slow:o.slow}}v(x,"k"),f(x,"timing");function b(o,t,e){return o.includes("*")?new RegExp(`^${o.split("*").map(r=>r.replace(/[.+?^${}()|[\]\\]/g,"\\$&")).join(".*")}$`).test(t):e?t===o:t.includes(o)}v(b,"y"),f(b,"matches");export class PerformanceWindow{static{v(this,"PerformanceWindow")}static{f(this,"PerformanceWindow")}constructor(t,e=[]){this.events=0,this.from=1/0,this.to=0,this.routes=new Map,this.sql=new Map,this.drops={runs:0,slow:0,failed:0},this.pages=new Map,this.open=new Map,this.routeOf=new Map,this.repeated=new Map,this.loops=[],this.limits=t,this.waivers=e.filter(r=>typeof r?.why=="string"&&(r.route||r.sql||r.page))}add(t){const e=$(t.raw),r=e.kind;if(r!=="server-request"&&r!=="db-query"&&r!==SQL_DROPS_KIND&&r!=="loop"&&r!=="page-visit")return;this.events+=1,this.from=Math.min(this.from,t.timestamp),this.to=Math.max(this.to,t.timestamp);const n=$(t.context),a=m(n.projectName)??m(e.project),l=m(n.requestId);r==="server-request"?this.request(t,e,a,l):r==="db-query"?this.statement(t,e,a,l):r===SQL_DROPS_KIND?this.dropped(t,e,a):r==="loop"?this.loop(t,e,l):this.page(t,e)}request(t,e,r,n){const a=$(e.request),l=m(a.method)??"?",p=m(a.route)??"?",u=`${l} ${p}`;let h=this.routes.get(u);if(!h){if(this.routes.size>=j)return;h={...k(),method:l,route:p,...r?{project:r}:{},errors:0,streaming:0},this.routes.set(u,h)}h.count+=1;const d=c(a.status);d!==void 0&&d>=500&&(h.errors+=1);const g=c(a.durationMs);a.streaming===!0?h.streaming+=1:g!==void 0&&q(h,g,this.limits.slowRouteMs,t.id),n&&(this.routeOf.set(n,u),this.routeOf.size>R&&this.routeOf.delete(this.routeOf.keys().next().value),this.close(n))}sqlEntry(t,e,r){let n=this.sql.get(t);if(!n&&this.sql.size<j){n={...k(),failed:0,dropped:0};const a=m(e.sqlName),l=m(e.op),p=m(e.table);a&&(n.name=a),r&&(n.project=r),l&&(n.op=l),p&&(n.table=p),this.sql.set(t,n)}return n}statement(t,e,r,n){const a=m(e.statement)??m(e.query),l=m(e.sqlId)??a?.slice(0,300);if(!l)return;const p=1+Math.max(0,c(e.floodSuppressed)??0),u=this.sqlEntry(l,e,r);if(u){u.count+=p,!u.text&&a&&(u.text=a.slice(0,300)),e.error!==void 0&&e.error!==null&&(u.failed+=1);const g=c(e.durationMs);g!==void 0&&q(u,g,this.limits.slowQueryMs,t.id)}if(!n)return;let h=this.open.get(n);h||(this.open.size>=R&&this.close(this.open.keys().next().value),h=new Map,this.open.set(n,h));const d=h.get(l);d?d.n+=p:h.set(l,{n:p,first:t.id})}dropped(t,e,r){const n=c(e.slowMs)??-1/0,a=f((h,d)=>Math.max(this.limits.slowQueryMs<=n?Math.max(0,c(h.slow)??0):0,d>=this.limits.slowQueryMs?1:0),"slowOf"),l=Array.isArray(e.shapes)?e.shapes:[];for(const h of l){const d=$(h),g=m(d.sqlId),s=Math.max(0,c(d.runs)??0);if(!g||!s)continue;const i=Math.max(0,c(d.maxMs)??0),M=Math.max(0,c(d.failed)??0),y=a(d,i);this.drops.runs+=s,this.drops.slow+=y,this.drops.failed+=M;const w=this.sqlEntry(g,d,r);if(w){if(w.count+=s,w.dropped+=s,w.failed+=M,w.max=Math.max(w.max,i),!w.text){const O=m(d.query);O&&(w.text=O.slice(0,300))}y&&(w.slow+=y,w.evidence.length<5&&w.evidence.push(t.id))}}const p=$(e.overflow),u=Math.max(0,c(p.runs)??0);if(u){const h=Math.max(0,c(p.maxMs)??0);this.drops.runs+=u,this.drops.slow+=a(p,h),this.drops.failed+=Math.max(0,c(p.failed)??0)}}close(t){const e=this.open.get(t);if(!e)return;this.open.delete(t);const r=this.routeOf.get(t)??"a request with no route seen";for(const[n,{n:a,first:l}]of e){if(a<this.limits.nPlusOne)continue;const p=`${r}\0${n}`,u=this.repeated.get(p)??{route:r,sql:n,worst:0,requests:0,evidence:[]};u.worst=Math.max(u.worst,a),u.requests+=1,u.evidence.length<5&&u.evidence.push(l),this.repeated.set(p,u)}}loop(t,e,r){const n=$(e.loop),a=c(n.dbCallsInsideLoop)??0;if(a<=this.limits.dbInLoop)return;const l=(r&&this.routeOf.get(r))??t.title??"a loop";this.loops.length>=200||this.loops.push({id:`db-in-loop:${l}`,kind:"db-in-loop",subject:l,measured:`${a} DB calls inside one loop (${c(n.iterations)??"?"} iterations)`,limit:`more than ${this.limits.dbInLoop} (dbInLoop)`,evidence:[t.id]})}page(t,e){if(e.phase!=="ready"||e.cause==="hmr")return;const r=m(e.app)??"?",n=m(e.pattern)??m(e.path)??"?",a=`${r} ${n}`;let l=this.pages.get(a);if(!l){if(this.pages.size>=j)return;l={...k(),app:r,path:n},this.pages.set(a,l)}l.count+=1;const p=e.nav==="hard"?c(e.appReadyMs)??c(e.readyMs):c(e.readyMs);p!==void 0&&q(l,p,this.limits.slowPageMs,t.id)}waiverOf(t){for(const e of this.waivers)if(e.route&&t.route&&b(e.route,t.route,!0)||e.sql&&t.sql&&(b(e.sql,t.sql,!1)||t.sqlText!==void 0&&b(e.sql,t.sqlText,!1))||e.page&&t.page&&b(e.page,t.page,!0))return e.why}report(t){for(const s of[...this.open.keys()])this.close(s);const e=this.limits,r=[],n=f((s,i="runs")=>`p95 ${s.p95Ms} ms, max ${s.maxMs} ms \u2014 ${s.slow} of ${s.timed} ${i} at or above`,"slowWords"),a=[...this.routes.entries()].map(([s,i])=>({key:s,method:i.method,route:i.route,...i.project?{project:i.project}:{},errors:i.errors,streaming:i.streaming,...x(i),evidence:i.evidence}));for(const s of a)s.slow>1&&r.push({breach:{id:`slow-route:${s.key}`,kind:"slow-route",subject:s.key,measured:n(s),limit:`${e.slowRouteMs} ms (slowRouteMs)`,evidence:s.evidence},about:{route:s.key}});const l=[...this.sql.entries()].map(([s,i])=>({key:s,...i.name?{name:i.name}:{},...i.project?{project:i.project}:{},...i.op?{op:i.op}:{},...i.table?{table:i.table}:{},...i.text?{text:i.text}:{},failed:i.failed,...i.dropped?{dropped:i.dropped}:{},...x(i),evidence:i.evidence}));for(const s of l){const i=s.dropped?`p95 ${s.p95Ms} ms, max ${s.maxMs} ms \u2014 ${s.slow} of ${s.count} runs at or above (${s.dropped} counted from the service's drop record, not timed)`:n(s);s.slow>1&&r.push({breach:{id:`slow-query:${s.key}`,kind:"slow-query",subject:s.name??s.key,measured:i,limit:`${e.slowQueryMs} ms (slowQueryMs)`,evidence:s.evidence},about:{sql:s.key,...s.text?{sqlText:s.text}:{}}})}for(const s of this.repeated.values()){const i=this.sql.get(s.sql);r.push({breach:{id:`n+1-query:${s.route}:${s.sql}`,kind:"n+1-query",subject:`${s.route} runs ${i?.name??s.sql}`,measured:`up to ${s.worst}\xD7 in one request (${s.requests} request${s.requests===1?"":"s"})`,limit:`fewer than ${e.nPlusOne} (nPlusOne)`,evidence:s.evidence},about:{route:s.route,sql:s.sql,...i?.text?{sqlText:i.text}:{}}})}for(const s of this.loops)r.push({breach:s,about:{route:s.subject}});const p=[...this.pages.entries()].map(([s,i])=>({key:s,app:i.app,path:i.path,...x(i),evidence:i.evidence}));for(const s of p)s.slow>1&&r.push({breach:{id:`slow-page:${s.key}`,kind:"slow-page",subject:s.key,measured:n(s,"visits"),limit:`${e.slowPageMs} ms (slowPageMs)`,evidence:s.evidence},about:{page:s.path}});const u=[],h=[];for(const{breach:s,about:i}of r){const M=this.waiverOf(i);M?h.push({...s,waived:M}):u.push(s)}const d=f(s=>{const{evidence:i,...M}=s;return M},"strip"),g=this.routes.size+this.sql.size+this.pages.size+this.drops.runs>0;return{window:t??{from:Number.isFinite(this.from)?this.from:0,to:this.to},events:this.events,limits:{...e},verdict:u.length?"fail":g?"pass":"unmeasured",breaches:u,waived:h,...this.drops.runs?{dropped:{...this.drops}}:{},routes:a.map(d).sort((s,i)=>i.p95Ms-s.p95Ms),sql:l.map(d).sort((s,i)=>i.p95Ms-s.p95Ms),pages:p.map(d).sort((s,i)=>i.p95Ms-s.p95Ms)}}}export function describePerformance(o,t=12){const e=o.limits,r=[o.verdict==="unmeasured"?"Performance: unmeasured \u2014 no request, statement or page ran in the window.":`Performance: ${o.verdict==="pass"?"PASS":`FAIL \u2014 ${o.breaches.length} breach${o.breaches.length===1?"":"es"}`} over ${o.routes.length} route${o.routes.length===1?"":"s"}, ${o.sql.length} statement${o.sql.length===1?"":"s"}, ${o.pages.length} page${o.pages.length===1?"":"s"} (limits: route ${e.slowRouteMs} ms, query ${e.slowQueryMs} ms, N+1 ${e.nPlusOne}, DB in a loop ${e.dbInLoop}, page ${e.slowPageMs} ms).`],n=o.dropped;n&&r.push(`${n.runs} statement run${n.runs===1?"":"s"} reached the lens only as counts (the services' flood guards): ${n.slow} slow, ${n.failed} failed \u2014 counted above, not in the percentiles.`);for(const a of o.breaches.slice(0,t))r.push(`- ${a.kind} \xB7 ${a.subject}: ${a.measured}; limit ${a.limit}`);return o.breaches.length>t&&r.push(`- \u2026 ${o.breaches.length-t} more`),o.waived.length&&r.push(`Waived by .lensmcp/team/performance.json: ${o.waived.map(a=>`${a.kind} \xB7 ${a.subject} (${a.waived})`).slice(0,t).join("; ")}`),r.join(`
|
|
1
|
+
"use strict";var z=Object.defineProperty;var v=(o,t)=>z(o,"name",{value:t,configurable:!0});var N=Object.defineProperty,f=v((o,t)=>N(o,"name",{value:t,configurable:!0}),"f");import{loopDbCalls as D}from"./patterns.js";import{resolveThresholds as E}from"./thresholds.js";export const SQL_DROPS_KIND="db-dropped",PERFORMANCE_EVENT_KINDS=["server-request","db-query",SQL_DROPS_KIND,"loop","page-visit"];export function performanceLimits(...o){let t={};for(const r of o)r&&(t={...t,...L(r)});const e=E(t);return{slowRouteMs:e.slowRouteMs,slowQueryMs:e.slowQueryMs,nPlusOne:e.nPlusOne,dbInLoop:e.dbInLoop,slowPageMs:e.slowPageMs}}v(performanceLimits,"performanceLimits"),f(performanceLimits,"performanceLimits");function L(o){const t={};for(const[e,r]of Object.entries(o))typeof r=="number"&&Number.isFinite(r)&&r>=0&&(t[e]=r);return t}v(L,"E"),f(L,"resolveThresholdsOnly");const $=f(o=>o&&typeof o=="object"?o:{},"obj"),w=f(o=>typeof o=="string"&&o?o:void 0,"str"),m=f(o=>typeof o=="number"&&Number.isFinite(o)?o:void 0,"num"),Q=2e3,j=5e3,k=2e4,T=1e3,S=1024;function R(o){let t=0;for(const[e,r]of o)t=Math.max(t,r+(o.get(e+1)??0));return t}v(R,"L"),f(R,"burstOf");const O=f(()=>({count:0,times:[],slow:0,max:0,evidence:[]}),"newTiming");function q(o,t,e,r){if(o.max=Math.max(o.max,t),t>=e&&(o.slow+=1,o.evidence.length<5&&o.evidence.push(r)),o.times.length<Q)o.times.push(t);else{let a=0;for(let n=1;n<o.times.length;n++)o.times[n]<o.times[a]&&(a=n);t>o.times[a]&&(o.times[a]=t)}}v(q,"k"),f(q,"time");function P(o,t){return o.length?o[Math.min(o.length-1,Math.floor(o.length*t))]:0}v(P,"P"),f(P,"percentile");function x(o){const t=[...o.times].sort((e,r)=>e-r);return{count:o.count,timed:o.times.length,p50Ms:Math.round(P(t,.5)),p95Ms:Math.round(P(t,.95)),maxMs:Math.round(o.max),slow:o.slow}}v(x,"j"),f(x,"timing");function b(o,t,e){return o.includes("*")?new RegExp(`^${o.split("*").map(r=>r.replace(/[.+?^${}()|[\]\\]/g,"\\$&")).join(".*")}$`).test(t):e?t===o:t.includes(o)}v(b,"y"),f(b,"matches");export class PerformanceWindow{static{v(this,"PerformanceWindow")}static{f(this,"PerformanceWindow")}constructor(t,e=[]){this.events=0,this.from=1/0,this.to=0,this.routes=new Map,this.sql=new Map,this.drops={runs:0,slow:0,failed:0},this.pages=new Map,this.open=new Map,this.routeOf=new Map,this.streams=new Set,this.repeated=new Map,this.loops=[],this.limits=t,this.waivers=e.filter(r=>typeof r?.why=="string"&&(r.route||r.sql||r.page))}add(t){const e=$(t.raw),r=e.kind;if(r!=="server-request"&&r!=="db-query"&&r!==SQL_DROPS_KIND&&r!=="loop"&&r!=="page-visit")return;this.events+=1,this.from=Math.min(this.from,t.timestamp),this.to=Math.max(this.to,t.timestamp);const a=$(t.context),n=w(a.projectName)??w(e.project),l=w(a.requestId);r==="server-request"?this.request(t,e,n,l):r==="db-query"?this.statement(t,e,n,l):r===SQL_DROPS_KIND?this.dropped(t,e,n):r==="loop"?this.loop(t,e,l):this.page(t,e)}request(t,e,r,a){const n=$(e.request),l=w(n.method)??"?",u=w(n.route)??"?",d=`${l} ${u}`;let h=this.routes.get(d);if(!h){if(this.routes.size>=j)return;h={...O(),method:l,route:u,...r?{project:r}:{},errors:0,streaming:0},this.routes.set(d,h)}h.count+=1;const p=m(n.status);p!==void 0&&p>=500&&(h.errors+=1);const c=m(n.durationMs);n.streaming===!0?h.streaming+=1:c!==void 0&&q(h,c,this.limits.slowRouteMs,t.id),a&&(this.routeOf.set(a,d),this.routeOf.size>k&&this.routeOf.delete(this.routeOf.keys().next().value),n.streaming===!0&&(this.streams.add(a),this.streams.size>k&&this.streams.delete(this.streams.keys().next().value)),this.close(a))}sqlEntry(t,e,r){let a=this.sql.get(t);if(!a&&this.sql.size<j){a={...O(),failed:0,dropped:0};const n=w(e.sqlName),l=w(e.op),u=w(e.table);n&&(a.name=n),r&&(a.project=r),l&&(a.op=l),u&&(a.table=u),this.sql.set(t,a)}return a}statement(t,e,r,a){const n=w(e.statement)??w(e.query),l=w(e.sqlId)??n?.slice(0,300);if(!l)return;const u=1+Math.max(0,m(e.floodSuppressed)??0),d=this.sqlEntry(l,e,r);if(d){d.count+=u,!d.text&&n&&(d.text=n.slice(0,300)),e.error!==void 0&&e.error!==null&&(d.failed+=1);const i=m(e.durationMs);i!==void 0&&q(d,i,this.limits.slowQueryMs,t.id)}if(!a)return;let h=this.open.get(a);h||(this.open.size>=k&&this.close(this.open.keys().next().value),h=new Map,this.open.set(a,h));let p=h.get(l);p?p.n+=u:h.set(l,p={n:u,first:t.id,seconds:new Map});const c=Math.floor(t.timestamp/T),s=p.seconds.get(c);s!==void 0?p.seconds.set(c,s+u):p.seconds.size<S&&p.seconds.set(c,u)}dropped(t,e,r){const a=m(e.slowMs)??-1/0,n=f((h,p)=>Math.max(this.limits.slowQueryMs<=a?Math.max(0,m(h.slow)??0):0,p>=this.limits.slowQueryMs?1:0),"slowOf"),l=Array.isArray(e.shapes)?e.shapes:[];for(const h of l){const p=$(h),c=w(p.sqlId),s=Math.max(0,m(p.runs)??0);if(!c||!s)continue;const i=Math.max(0,m(p.maxMs)??0),M=Math.max(0,m(p.failed)??0),y=n(p,i);this.drops.runs+=s,this.drops.slow+=y,this.drops.failed+=M;const g=this.sqlEntry(c,p,r);if(g){if(g.count+=s,g.dropped+=s,g.failed+=M,g.max=Math.max(g.max,i),!g.text){const I=w(p.query);I&&(g.text=I.slice(0,300))}y&&(g.slow+=y,g.evidence.length<5&&g.evidence.push(t.id))}}const u=$(e.overflow),d=Math.max(0,m(u.runs)??0);if(d){const h=Math.max(0,m(u.maxMs)??0);this.drops.runs+=d,this.drops.slow+=n(u,h),this.drops.failed+=Math.max(0,m(u.failed)??0)}}close(t){const e=this.open.get(t);if(!e)return;this.open.delete(t);const r=this.routeOf.get(t)??"a request with no route seen",a=this.streams.delete(t);for(const[n,{n:l,first:u,seconds:d}]of e){const h=a?R(d):l;if(h<this.limits.nPlusOne)continue;const p=`${r}\0${n}`,c=this.repeated.get(p)??{route:r,sql:n,worst:0,requests:0,streams:0,evidence:[]};c.worst=Math.max(c.worst,h),c.requests+=1,a&&(c.streams+=1),c.evidence.length<5&&c.evidence.push(u),this.repeated.set(p,c)}}loop(t,e,r){const a=$(e.loop),n=D(a);if(n<=this.limits.dbInLoop)return;const l=m(a.dbCallsInsideLoop)??0,u=(r&&this.routeOf.get(r))??t.title??"a loop";this.loops.length>=200||this.loops.push({id:`db-in-loop:${u}`,kind:"db-in-loop",subject:u,measured:n<l?`${n} DB calls within two seconds of one loop of a stream (${l} over its life)`:`${n} DB calls inside one loop (${m(a.iterations)??"?"} iterations)`,limit:`more than ${this.limits.dbInLoop} (dbInLoop)`,evidence:[t.id]})}page(t,e){if(e.phase!=="ready"||e.cause==="hmr")return;const r=w(e.app)??"?",a=w(e.pattern)??w(e.path)??"?",n=`${r} ${a}`;let l=this.pages.get(n);if(!l){if(this.pages.size>=j)return;l={...O(),app:r,path:a},this.pages.set(n,l)}l.count+=1;const u=e.nav==="hard"?m(e.appReadyMs)??m(e.readyMs):m(e.readyMs);u!==void 0&&q(l,u,this.limits.slowPageMs,t.id)}waiverOf(t){for(const e of this.waivers)if(e.route&&t.route&&b(e.route,t.route,!0)||e.sql&&t.sql&&(b(e.sql,t.sql,!1)||t.sqlText!==void 0&&b(e.sql,t.sqlText,!1))||e.page&&t.page&&b(e.page,t.page,!0))return e.why}report(t){for(const s of[...this.open.keys()])this.close(s);const e=this.limits,r=[],a=f((s,i="runs")=>`p95 ${s.p95Ms} ms, max ${s.maxMs} ms \u2014 ${s.slow} of ${s.timed} ${i} at or above`,"slowWords"),n=[...this.routes.entries()].map(([s,i])=>({key:s,method:i.method,route:i.route,...i.project?{project:i.project}:{},errors:i.errors,streaming:i.streaming,...x(i),evidence:i.evidence}));for(const s of n)s.slow>1&&r.push({breach:{id:`slow-route:${s.key}`,kind:"slow-route",subject:s.key,measured:a(s),limit:`${e.slowRouteMs} ms (slowRouteMs)`,evidence:s.evidence},about:{route:s.key}});const l=[...this.sql.entries()].map(([s,i])=>({key:s,...i.name?{name:i.name}:{},...i.project?{project:i.project}:{},...i.op?{op:i.op}:{},...i.table?{table:i.table}:{},...i.text?{text:i.text}:{},failed:i.failed,...i.dropped?{dropped:i.dropped}:{},...x(i),evidence:i.evidence}));for(const s of l){const i=s.dropped?`p95 ${s.p95Ms} ms, max ${s.maxMs} ms \u2014 ${s.slow} of ${s.count} runs at or above (${s.dropped} counted from the service's drop record, not timed)`:a(s);s.slow>1&&r.push({breach:{id:`slow-query:${s.key}`,kind:"slow-query",subject:s.name??s.key,measured:i,limit:`${e.slowQueryMs} ms (slowQueryMs)`,evidence:s.evidence},about:{sql:s.key,...s.text?{sqlText:s.text}:{}}})}for(const s of this.repeated.values()){const i=this.sql.get(s.sql);r.push({breach:{id:`n+1-query:${s.route}:${s.sql}`,kind:"n+1-query",subject:`${s.route} runs ${i?.name??s.sql}`,measured:s.streams===s.requests?`up to ${s.worst}\xD7 in one burst of a stream, within two seconds (${s.streams} stream${s.streams===1?"":"s"})`:`up to ${s.worst}\xD7 in one request (${s.requests} request${s.requests===1?"":"s"})`,limit:`fewer than ${e.nPlusOne} (nPlusOne)`,evidence:s.evidence},about:{route:s.route,sql:s.sql,...i?.text?{sqlText:i.text}:{}}})}for(const s of this.loops)r.push({breach:s,about:{route:s.subject}});const u=[...this.pages.entries()].map(([s,i])=>({key:s,app:i.app,path:i.path,...x(i),evidence:i.evidence}));for(const s of u)s.slow>1&&r.push({breach:{id:`slow-page:${s.key}`,kind:"slow-page",subject:s.key,measured:a(s,"visits"),limit:`${e.slowPageMs} ms (slowPageMs)`,evidence:s.evidence},about:{page:s.path}});const d=[],h=[];for(const{breach:s,about:i}of r){const M=this.waiverOf(i);M?h.push({...s,waived:M}):d.push(s)}const p=f(s=>{const{evidence:i,...M}=s;return M},"strip"),c=this.routes.size+this.sql.size+this.pages.size+this.drops.runs>0;return{window:t??{from:Number.isFinite(this.from)?this.from:0,to:this.to},events:this.events,limits:{...e},verdict:d.length?"fail":c?"pass":"unmeasured",breaches:d,waived:h,...this.drops.runs?{dropped:{...this.drops}}:{},routes:n.map(p).sort((s,i)=>i.p95Ms-s.p95Ms),sql:l.map(p).sort((s,i)=>i.p95Ms-s.p95Ms),pages:u.map(p).sort((s,i)=>i.p95Ms-s.p95Ms)}}}export function describePerformance(o,t=12){const e=o.limits,r=[o.verdict==="unmeasured"?"Performance: unmeasured \u2014 no request, statement or page ran in the window.":`Performance: ${o.verdict==="pass"?"PASS":`FAIL \u2014 ${o.breaches.length} breach${o.breaches.length===1?"":"es"}`} over ${o.routes.length} route${o.routes.length===1?"":"s"}, ${o.sql.length} statement${o.sql.length===1?"":"s"}, ${o.pages.length} page${o.pages.length===1?"":"s"} (limits: route ${e.slowRouteMs} ms, query ${e.slowQueryMs} ms, N+1 ${e.nPlusOne}, DB in a loop ${e.dbInLoop}, page ${e.slowPageMs} ms).`],a=o.dropped;a&&r.push(`${a.runs} statement run${a.runs===1?"":"s"} reached the lens only as counts (the services' flood guards): ${a.slow} slow, ${a.failed} failed \u2014 counted above, not in the percentiles.`);for(const n of o.breaches.slice(0,t))r.push(`- ${n.kind} \xB7 ${n.subject}: ${n.measured}; limit ${n.limit}`);return o.breaches.length>t&&r.push(`- \u2026 ${o.breaches.length-t} more`),o.waived.length&&r.push(`Waived by .lensmcp/team/performance.json: ${o.waived.map(n=>`${n.kind} \xB7 ${n.subject} (${n.waived})`).slice(0,t).join("; ")}`),r.join(`
|
|
2
2
|
`)}v(describePerformance,"describePerformance"),f(describePerformance,"describePerformance");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lensmcp/core",
|
|
3
|
-
"version": "1.25.
|
|
3
|
+
"version": "1.25.45",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -84,7 +84,7 @@
|
|
|
84
84
|
}
|
|
85
85
|
},
|
|
86
86
|
"dependencies": {
|
|
87
|
-
"@lensmcp/protocol-types": "1.25.
|
|
87
|
+
"@lensmcp/protocol-types": "1.25.45",
|
|
88
88
|
"tslib": "^2.3.0"
|
|
89
89
|
},
|
|
90
90
|
"license": "Apache-2.0",
|