@lensmcp/core 1.25.49 → 1.25.50
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.
|
@@ -95,6 +95,34 @@ export interface PerformanceBreach {
|
|
|
95
95
|
evidence: string[];
|
|
96
96
|
/** Waived by the person: why. A waived breach does not fail the window. */
|
|
97
97
|
waived?: string;
|
|
98
|
+
/**
|
|
99
|
+
* What it is about — its route (`GET /x`), statement key and page path — and its number: a timing's p95 ms, an N+1's
|
|
100
|
+
* worst count, a loop's DB calls. A QA gate compares both with main's (issues/closed/foodguard-181).
|
|
101
|
+
*/
|
|
102
|
+
about?: {
|
|
103
|
+
route?: string;
|
|
104
|
+
sql?: string;
|
|
105
|
+
page?: string;
|
|
106
|
+
};
|
|
107
|
+
value?: number;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* A window's numbers by subject, kept small: what a later window is compared with — main's, for a QA run's breaches
|
|
111
|
+
* (issues/closed/foodguard-181). Timings are p95 ms; `repeats` the most runs of a statement in one request of a route
|
|
112
|
+
* (`<route>\0<sql>`), at any count; `loops` the most DB calls in one loop of a route.
|
|
113
|
+
*/
|
|
114
|
+
export interface PerformanceBaseline {
|
|
115
|
+
limits: PerformanceLimits;
|
|
116
|
+
window: {
|
|
117
|
+
from: number;
|
|
118
|
+
to: number;
|
|
119
|
+
};
|
|
120
|
+
events: number;
|
|
121
|
+
routes: Record<string, number>;
|
|
122
|
+
sql: Record<string, number>;
|
|
123
|
+
pages: Record<string, number>;
|
|
124
|
+
repeats: Record<string, number>;
|
|
125
|
+
loops: Record<string, number>;
|
|
98
126
|
}
|
|
99
127
|
export interface PerformanceReport {
|
|
100
128
|
window: {
|
|
@@ -143,12 +171,20 @@ export declare class PerformanceWindow {
|
|
|
143
171
|
/** N+1 per route and statement: the worst count, and where — and how many of its requests were streams. */
|
|
144
172
|
private readonly repeated;
|
|
145
173
|
private readonly loops;
|
|
174
|
+
/** Where each statement runs (planning/code-locations `raw.site`): the files of its frames, and whether a run carried none. */
|
|
175
|
+
private readonly sqlFiles;
|
|
176
|
+
private readonly sqlUnlocated;
|
|
177
|
+
/** The statements each route's requests ran, and the most runs of each in one request — at any count. */
|
|
178
|
+
private readonly routeSql;
|
|
179
|
+
private readonly mostRuns;
|
|
146
180
|
constructor(limits: PerformanceLimits, waivers?: readonly PerformanceWaiver[]);
|
|
147
181
|
add(event: BaseEvent): void;
|
|
148
182
|
private request;
|
|
149
183
|
/** The statement `key`'s entry, made from what `raw` says of it; undefined past the cap. */
|
|
150
184
|
private sqlEntry;
|
|
151
185
|
private statement;
|
|
186
|
+
/** The files of a run's `raw.site` frames, kept per statement; a run that carried none leaves it unlocated. */
|
|
187
|
+
private located;
|
|
152
188
|
/**
|
|
153
189
|
* A service's drop record: each statement's dropped runs are counted — runs, failures, the slowest — but not timed. Its
|
|
154
190
|
* slow runs are slow by the window's limit when that is at or under the record's `slowMs`; the slowest run says at
|
|
@@ -169,6 +205,21 @@ export declare class PerformanceWindow {
|
|
|
169
205
|
from: number;
|
|
170
206
|
to: number;
|
|
171
207
|
}): PerformanceReport;
|
|
208
|
+
/**
|
|
209
|
+
* The files a breach's subject runs from — a statement's site files; a route's, every statement its requests ran —
|
|
210
|
+
* or undefined when that is not known: a statement run without a site, a route that ran none, a page (its own code
|
|
211
|
+
* is not mapped). Read after `report()` (issues/closed/foodguard-181: a breach outside what the goal reaches).
|
|
212
|
+
*/
|
|
213
|
+
locate(about: {
|
|
214
|
+
route?: string;
|
|
215
|
+
sql?: string;
|
|
216
|
+
page?: string;
|
|
217
|
+
} | undefined): string[] | undefined;
|
|
218
|
+
/** The window's numbers by subject — what a later window's breaches are compared with. Read after `report()`. */
|
|
219
|
+
baseline(window?: {
|
|
220
|
+
from: number;
|
|
221
|
+
to: number;
|
|
222
|
+
}): PerformanceBaseline;
|
|
172
223
|
}
|
|
173
224
|
/** A report in a few lines — what the QA gate answers and the archive's performance.md opens with. */
|
|
174
225
|
export declare function describePerformance(report: PerformanceReport, max?: number): string;
|
|
@@ -1,2 +1,2 @@
|
|
|
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
|
-
`)}
|
|
1
|
+
"use strict";var E=Object.defineProperty;var M=(i,t)=>E(i,"name",{value:t,configurable:!0});var I=Object.defineProperty,c=M((i,t)=>I(i,"name",{value:t,configurable:!0}),"f");import{loopDbCalls as L}from"./patterns.js";import{resolveThresholds as F}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(...i){let t={};for(const r of i)r&&(t={...t,...S(r)});const e=F(t);return{slowRouteMs:e.slowRouteMs,slowQueryMs:e.slowQueryMs,nPlusOne:e.nPlusOne,dbInLoop:e.dbInLoop,slowPageMs:e.slowPageMs}}M(performanceLimits,"performanceLimits"),c(performanceLimits,"performanceLimits");function S(i){const t={};for(const[e,r]of Object.entries(i))typeof r=="number"&&Number.isFinite(r)&&r>=0&&(t[e]=r);return t}M(S,"P"),c(S,"resolveThresholdsOnly");const q=c(i=>i&&typeof i=="object"?i:{},"obj"),m=c(i=>typeof i=="string"&&i?i:void 0,"str"),f=c(i=>typeof i=="number"&&Number.isFinite(i)?i:void 0,"num"),N=2e3,b=5e3,k=2e4,D=1e3,Q=1024,T=12,A=200;function z(i){let t=0;for(const[e,r]of i)t=Math.max(t,r+(i.get(e+1)??0));return t}M(z,"L"),c(z,"burstOf");const O=c(()=>({count:0,times:[],slow:0,max:0,evidence:[]}),"newTiming");function y(i,t,e,r){if(i.max=Math.max(i.max,t),t>=e&&(i.slow+=1,i.evidence.length<5&&i.evidence.push(r)),i.times.length<N)i.times.push(t);else{let o=0;for(let n=1;n<i.times.length;n++)i.times[n]<i.times[o]&&(o=n);t>i.times[o]&&(i.times[o]=t)}}M(y,"S"),c(y,"time");function P(i,t){return i.length?i[Math.min(i.length-1,Math.floor(i.length*t))]:0}M(P,"j"),c(P,"percentile");function x(i){const t=[...i.times].sort((e,r)=>e-r);return{count:i.count,timed:i.times.length,p50Ms:Math.round(P(t,.5)),p95Ms:Math.round(P(t,.95)),maxMs:Math.round(i.max),slow:i.slow}}M(x,"k"),c(x,"timing");function $(i,t,e){return i.includes("*")?new RegExp(`^${i.split("*").map(r=>r.replace(/[.+?^${}()|[\]\\]/g,"\\$&")).join(".*")}$`).test(t):e?t===i:t.includes(i)}M($,"y"),c($,"matches");export class PerformanceWindow{static{M(this,"PerformanceWindow")}static{c(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.sqlFiles=new Map,this.sqlUnlocated=new Set,this.routeSql=new Map,this.mostRuns=new Map,this.limits=t,this.waivers=e.filter(r=>typeof r?.why=="string"&&(r.route||r.sql||r.page))}add(t){const e=q(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 o=q(t.context),n=m(o.projectName)??m(e.project),u=m(o.requestId);r==="server-request"?this.request(t,e,n,u):r==="db-query"?this.statement(t,e,n,u):r===SQL_DROPS_KIND?this.dropped(t,e,n):r==="loop"?this.loop(t,e,u):this.page(t,e)}request(t,e,r,o){const n=q(e.request),u=m(n.method)??"?",l=m(n.route)??"?",d=`${u} ${l}`;let p=this.routes.get(d);if(!p){if(this.routes.size>=b)return;p={...O(),method:u,route:l,...r?{project:r}:{},errors:0,streaming:0},this.routes.set(d,p)}p.count+=1;const h=f(n.status);h!==void 0&&h>=500&&(p.errors+=1);const w=f(n.durationMs);n.streaming===!0?p.streaming+=1:w!==void 0&&y(p,w,this.limits.slowRouteMs,t.id),o&&(this.routeOf.set(o,d),this.routeOf.size>k&&this.routeOf.delete(this.routeOf.keys().next().value),n.streaming===!0&&(this.streams.add(o),this.streams.size>k&&this.streams.delete(this.streams.keys().next().value)),this.close(o))}sqlEntry(t,e,r){let o=this.sql.get(t);if(!o&&this.sql.size<b){o={...O(),failed:0,dropped:0};const n=m(e.sqlName),u=m(e.op),l=m(e.table);n&&(o.name=n),r&&(o.project=r),u&&(o.op=u),l&&(o.table=l),this.sql.set(t,o)}return o}statement(t,e,r,o){const n=m(e.statement)??m(e.query),u=m(e.sqlId)??n?.slice(0,300);if(!u)return;const l=1+Math.max(0,f(e.floodSuppressed)??0),d=this.sqlEntry(u,e,r);if(d){this.located(u,e.site),d.count+=l,!d.text&&n&&(d.text=n.slice(0,300)),e.error!==void 0&&e.error!==null&&(d.failed+=1);const a=f(e.durationMs);a!==void 0&&y(d,a,this.limits.slowQueryMs,t.id)}if(!o)return;let p=this.open.get(o);p||(this.open.size>=k&&this.close(this.open.keys().next().value),p=new Map,this.open.set(o,p));let h=p.get(u);h?h.n+=l:p.set(u,h={n:l,first:t.id,seconds:new Map});const w=Math.floor(t.timestamp/D),s=h.seconds.get(w);s!==void 0?h.seconds.set(w,s+l):h.seconds.size<Q&&h.seconds.set(w,l)}located(t,e){const r=(Array.isArray(q(e).frames)?q(e).frames:[]).slice(0,3).map(n=>m(q(n).file)).filter(n=>!!n);if(!r.length){this.sqlUnlocated.add(t);return}let o=this.sqlFiles.get(t);o||this.sqlFiles.set(t,o=new Set);for(const n of r)o.size<T&&o.add(n.slice(0,1024))}dropped(t,e,r){const o=f(e.slowMs)??-1/0,n=c((p,h)=>Math.max(this.limits.slowQueryMs<=o?Math.max(0,f(p.slow)??0):0,h>=this.limits.slowQueryMs?1:0),"slowOf"),u=Array.isArray(e.shapes)?e.shapes:[];for(const p of u){const h=q(p),w=m(h.sqlId),s=Math.max(0,f(h.runs)??0);if(!w||!s)continue;const a=Math.max(0,f(h.maxMs)??0),g=Math.max(0,f(h.failed)??0),j=n(h,a);this.drops.runs+=s,this.drops.slow+=j,this.drops.failed+=g;const v=this.sqlEntry(w,h,r);if(v){if(v.count+=s,v.dropped+=s,v.failed+=g,v.max=Math.max(v.max,a),!v.text){const R=m(h.query);R&&(v.text=R.slice(0,300))}j&&(v.slow+=j,v.evidence.length<5&&v.evidence.push(t.id))}}const l=q(e.overflow),d=Math.max(0,f(l.runs)??0);if(d){const p=Math.max(0,f(l.maxMs)??0);this.drops.runs+=d,this.drops.slow+=n(l,p),this.drops.failed+=Math.max(0,f(l.failed)??0)}}close(t){const e=this.open.get(t);if(!e)return;this.open.delete(t);const r=this.routeOf.get(t),o=r??"a request with no route seen",n=this.streams.delete(t);let u=r?this.routeSql.get(r):void 0;r&&!u&&this.routeSql.size<b&&this.routeSql.set(r,u=new Set);for(const[l,{n:d,first:p,seconds:h}]of e){const w=n?z(h):d;u&&u.size<A&&u.add(l);const s=`${o}\0${l}`;if(w>(this.mostRuns.get(s)??0)&&(this.mostRuns.has(s)||this.mostRuns.size<b*4)&&this.mostRuns.set(s,w),w<this.limits.nPlusOne)continue;const a=`${o}\0${l}`,g=this.repeated.get(a)??{route:o,sql:l,worst:0,requests:0,streams:0,evidence:[]};g.worst=Math.max(g.worst,w),g.requests+=1,n&&(g.streams+=1),g.evidence.length<5&&g.evidence.push(p),this.repeated.set(a,g)}}loop(t,e,r){const o=q(e.loop),n=L(o);if(n<=this.limits.dbInLoop)return;const u=f(o.dbCallsInsideLoop)??0,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:n<u?`${n} DB calls within two seconds of one loop of a stream (${u} over its life)`:`${n} DB calls inside one loop (${f(o.iterations)??"?"} iterations)`,limit:`more than ${this.limits.dbInLoop} (dbInLoop)`,evidence:[t.id],about:{route:l},value:n})}page(t,e){if(e.phase!=="ready"||e.cause==="hmr")return;const r=m(e.app)??"?",o=m(e.pattern)??m(e.path)??"?",n=`${r} ${o}`;let u=this.pages.get(n);if(!u){if(this.pages.size>=b)return;u={...O(),app:r,path:o},this.pages.set(n,u)}u.count+=1;const l=e.nav==="hard"?f(e.appReadyMs)??f(e.readyMs):f(e.readyMs);l!==void 0&&y(u,l,this.limits.slowPageMs,t.id)}waiverOf(t){for(const e of this.waivers)if(e.route&&t.route&&$(e.route,t.route,!0)||e.sql&&t.sql&&($(e.sql,t.sql,!1)||t.sqlText!==void 0&&$(e.sql,t.sqlText,!1))||e.page&&t.page&&$(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=[],o=c((s,a="runs")=>`p95 ${s.p95Ms} ms, max ${s.maxMs} ms \u2014 ${s.slow} of ${s.timed} ${a} at or above`,"slowWords"),n=[...this.routes.entries()].map(([s,a])=>({key:s,method:a.method,route:a.route,...a.project?{project:a.project}:{},errors:a.errors,streaming:a.streaming,...x(a),evidence:a.evidence}));for(const s of n)s.slow>1&&r.push({breach:{id:`slow-route:${s.key}`,kind:"slow-route",subject:s.key,measured:o(s),limit:`${e.slowRouteMs} ms (slowRouteMs)`,evidence:s.evidence,about:{route:s.key},value:s.p95Ms},about:{route:s.key}});const u=[...this.sql.entries()].map(([s,a])=>({key:s,...a.name?{name:a.name}:{},...a.project?{project:a.project}:{},...a.op?{op:a.op}:{},...a.table?{table:a.table}:{},...a.text?{text:a.text}:{},failed:a.failed,...a.dropped?{dropped:a.dropped}:{},...x(a),evidence:a.evidence}));for(const s of u){const a=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)`:o(s);s.slow>1&&r.push({breach:{id:`slow-query:${s.key}`,kind:"slow-query",subject:s.name??s.key,measured:a,limit:`${e.slowQueryMs} ms (slowQueryMs)`,evidence:s.evidence,about:{sql:s.key},value:s.p95Ms},about:{sql:s.key,...s.text?{sqlText:s.text}:{}}})}for(const s of this.repeated.values()){const a=this.sql.get(s.sql);r.push({breach:{id:`n+1-query:${s.route}:${s.sql}`,kind:"n+1-query",subject:`${s.route} runs ${a?.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},value:s.worst},about:{route:s.route,sql:s.sql,...a?.text?{sqlText:a.text}:{}}})}for(const s of this.loops)r.push({breach:s,about:{route:s.subject}});const l=[...this.pages.entries()].map(([s,a])=>({key:s,app:a.app,path:a.path,...x(a),evidence:a.evidence}));for(const s of l)s.slow>1&&r.push({breach:{id:`slow-page:${s.key}`,kind:"slow-page",subject:s.key,measured:o(s,"visits"),limit:`${e.slowPageMs} ms (slowPageMs)`,evidence:s.evidence,about:{page:s.key},value:s.p95Ms},about:{page:s.path}});const d=[],p=[];for(const{breach:s,about:a}of r){const g=this.waiverOf(a);g?p.push({...s,waived:g}):d.push(s)}const h=c(s=>{const{evidence:a,...g}=s;return g},"strip"),w=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":w?"pass":"unmeasured",breaches:d,waived:p,...this.drops.runs?{dropped:{...this.drops}}:{},routes:n.map(h).sort((s,a)=>a.p95Ms-s.p95Ms),sql:u.map(h).sort((s,a)=>a.p95Ms-s.p95Ms),pages:l.map(h).sort((s,a)=>a.p95Ms-s.p95Ms)}}locate(t){if(!t||t.page)return;const e=c(n=>this.sqlUnlocated.has(n)||!this.sqlFiles.get(n)?.size?void 0:[...this.sqlFiles.get(n)],"ofSql");if(t.sql)return e(t.sql);if(!t.route)return;const r=this.routeSql.get(t.route);if(!r?.size)return;const o=new Set;for(const n of r){const u=e(n);if(!u)return;for(const l of u)o.add(l)}return[...o]}baseline(t){const e=this.report(t),r={};for(const o of this.loops)o.about?.route&&(o.value??0)>(r[o.about.route]??0)&&(r[o.about.route]=o.value??0);return{limits:{...this.limits},window:e.window,events:e.events,routes:Object.fromEntries(e.routes.filter(o=>o.timed).map(o=>[o.key,o.p95Ms])),sql:Object.fromEntries(e.sql.filter(o=>o.timed).map(o=>[o.key,o.p95Ms])),pages:Object.fromEntries(e.pages.filter(o=>o.timed).map(o=>[o.key,o.p95Ms])),repeats:Object.fromEntries(this.mostRuns),loops:r}}}export function describePerformance(i,t=12){const e=i.limits,r=[i.verdict==="unmeasured"?"Performance: unmeasured \u2014 no request, statement or page ran in the window.":`Performance: ${i.verdict==="pass"?"PASS":`FAIL \u2014 ${i.breaches.length} breach${i.breaches.length===1?"":"es"}`} over ${i.routes.length} route${i.routes.length===1?"":"s"}, ${i.sql.length} statement${i.sql.length===1?"":"s"}, ${i.pages.length} page${i.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).`],o=i.dropped;o&&r.push(`${o.runs} statement run${o.runs===1?"":"s"} reached the lens only as counts (the services' flood guards): ${o.slow} slow, ${o.failed} failed \u2014 counted above, not in the percentiles.`);for(const n of i.breaches.slice(0,t))r.push(`- ${n.kind} \xB7 ${n.subject}: ${n.measured}; limit ${n.limit}`);return i.breaches.length>t&&r.push(`- \u2026 ${i.breaches.length-t} more`),i.waived.length&&r.push(`Waived by .lensmcp/team/performance.json: ${i.waived.map(n=>`${n.kind} \xB7 ${n.subject} (${n.waived})`).slice(0,t).join("; ")}`),r.join(`
|
|
2
|
+
`)}M(describePerformance,"describePerformance"),c(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.50",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -94,7 +94,7 @@
|
|
|
94
94
|
}
|
|
95
95
|
},
|
|
96
96
|
"dependencies": {
|
|
97
|
-
"@lensmcp/protocol-types": "1.25.
|
|
97
|
+
"@lensmcp/protocol-types": "1.25.50",
|
|
98
98
|
"tslib": "^2.3.0"
|
|
99
99
|
},
|
|
100
100
|
"license": "Apache-2.0",
|