payroll-users-benchmark.mjs 2.2 KB

12345
  1. import fs from 'node:fs';import ts from 'typescript';import assert from 'node:assert/strict';import {withGateway} from './server-util.mjs';
  2. const source=fs.readFileSync('scripts/cloud/payroll-integrity.js','utf8');const ast=ts.createSourceFile('x.js',source,ts.ScriptTarget.Latest,true,ts.ScriptKind.JS);const helper=ast.statements.find(n=>ts.isFunctionDeclaration(n)&&n.name.text==='payrollReadUsers').getText(ast);
  3. const gateway=fs.readFileSync('scripts/deploy-admin-functions.mjs','utf8').match(/const operationsGatewayCode = String.raw`([\s\S]*?)\n`;/)[1];const support=['text','legacyOf','displayName','mobileOf'].map(name=>gateway.match(new RegExp('function '+name+'\\([^\\n]+'))?.[0]).join('\n');
  4. async function run(){return Psql.tx(async tx=>{await tx.none(usersSql.replace('CREATE OR REPLACE FUNCTION xs_payroll_read_users','CREATE OR REPLACE FUNCTION pg_temp.xs_payroll_read_users'));const results=[];function business(users){return users.map(u=>[u.objectId,Number(u.legacyUserId||legacyOf(u).UserID)||0,displayName(u),Number(legacyOf(u).ParentUserID)||0,mobileOf(u)]).sort((a,b)=>a[0].localeCompare(b[0]));}for(let i=0;i<10;i++){const rows=[];for(const candidate of i%2?[true,false]:[false,true]){const start=Date.now();const users=candidate?(await tx.query('SELECT pg_temp.xs_payroll_read_users($1) AS row',['7pIbDBJmKx'])).map(x=>x.row):await tx.query('SELECT * FROM "_User" WHERE "company"=$1',['7pIbDBJmKx']);rows.push({candidate,ms:Date.now()-start,bytes:JSON.stringify(users).length,business:business(users)});}if(JSON.stringify(rows[0].business)!==JSON.stringify(rows[1].business))throw Error('User hydration changed');results.push(rows.map(({business,...v})=>({...v,count:business.length})));}await tx.none('DROP FUNCTION pg_temp.xs_payroll_read_users(text)');return results;});}
  5. const result=await withGateway('const usersSql='+JSON.stringify(fs.readFileSync('scripts/sql/payroll-read-snapshot.sql','utf8').slice(fs.readFileSync('scripts/sql/payroll-read-snapshot.sql','utf8').indexOf('CREATE OR REPLACE FUNCTION xs_payroll_read_users')))+';'+support+'\n'+helper+'\n'+run.toString(),c=>c({}));assert.equal(result.length,10);fs.writeFileSync('perf/speed-round3-2026-11-10/payroll-users-benchmark.json',JSON.stringify(result,null,2));console.log(JSON.stringify(result));