-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
205 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright 2024 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
/* eslint-disable no-await-in-loop */ | ||
|
||
import { HelixStorage } from './storage.js'; | ||
import { errorWithResponse } from './util.js'; | ||
|
||
/** | ||
* @param {UniversalContext} ctx | ||
* @param {Object} data | ||
*/ | ||
async function writeLogs(ctx, data) { | ||
const now = ctx.attributes.start; | ||
const day = now.getUTCDate(); | ||
const month = now.getUTCMonth() + 1; | ||
const year = now.getUTCFullYear(); | ||
const { logBucket } = HelixStorage.fromContext(ctx); | ||
// get existing | ||
let existing = {}; | ||
const buf = await logBucket.get(`bundler/${year}/${month}/${day}.json`); | ||
if (buf) { | ||
existing = JSON.parse(buf.toString()); | ||
} | ||
|
||
existing.performance = existing.performance || []; | ||
existing.performance.push(data); | ||
|
||
await logBucket.put(`bundler/${year}/${month}/${day}.json`, JSON.stringify(existing), 'application/json'); | ||
} | ||
|
||
/** | ||
* @typedef {{ limit: number; }} LoopOpts | ||
* @type {< | ||
* TFunc extends (...args: any[]) => boolean|Promise<boolean> | ||
* >( | ||
* fn: TFunc, | ||
* ctx: UniversalContext, | ||
* opts: LoopOpts | ||
* ) => (...args: Parameters<TFunc>) => Promise<void>} | ||
*/ | ||
export const loop = (fn, ctx, opts) => { | ||
const { limit } = opts; | ||
|
||
return async (...args) => { | ||
let done = false; | ||
let before = performance.now(); | ||
|
||
const state = { | ||
timer: 0, | ||
/** @type {number[]} */ | ||
times: [], | ||
average() { | ||
return this.timer / this.times.length; | ||
}, | ||
/** @param {number} t */ | ||
push(t) { | ||
this.times.push(t); | ||
this.timer += t; | ||
}, | ||
}; | ||
|
||
while (!done) { | ||
done = await fn(...args); | ||
|
||
const marks = performance.getEntriesByType('mark'); | ||
const starts = marks.filter((m) => m.name.startsWith('start')); | ||
const measures = starts.reduce((acc, start) => { | ||
const name = start.name.replace('start:', ''); | ||
const end = marks.find((m) => m.name === `end:${name}`); | ||
if (end) { | ||
acc[name] = end.startTime - start.startTime; | ||
} | ||
return acc; | ||
}, {}); | ||
ctx.log.info(JSON.stringify({ | ||
message: 'performance', | ||
measures, | ||
stats: ctx.attributes.stats, | ||
})); | ||
if ([true, 'true'].includes(ctx.env.WRITE_PERF_LOGS)) { | ||
await writeLogs(ctx, { measures, stats: ctx.attributes.stats }); | ||
} | ||
performance.clearMarks(); | ||
ctx.attributes.stats = {}; | ||
|
||
const after = performance.now(); | ||
const dur = after - before; | ||
before = after; | ||
state.push(dur); | ||
|
||
if (state.timer + state.average() >= limit) { | ||
throw errorWithResponse( | ||
504, | ||
`timeout after ${state.times.length} runs (${Math.round(state.timer)} + ${Math.round(state.average())} >= ${limit})`, | ||
); | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2024 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
import assert from 'assert'; | ||
import { DEFAULT_CONTEXT, assertRejectsWithResponse, sleep } from '../util.js'; | ||
import { loop } from '../../src/support/loop.js'; | ||
|
||
describe('loop()', () => { | ||
it('passes arguments to function', async () => { | ||
const fn = (arg1, arg2) => { | ||
assert.deepStrictEqual(arg1, 'foo'); | ||
assert.deepStrictEqual(arg2, 'bar'); | ||
return true; | ||
}; | ||
const wrapped = loop(fn, DEFAULT_CONTEXT(), { limit: 100 }); | ||
await assert.doesNotReject(wrapped('foo', 'bar')); | ||
}); | ||
|
||
it('throws timeout response if function loop will exceed timeout', async () => { | ||
let count = 0; | ||
const fn = async () => { | ||
await sleep(10); | ||
count += 1; | ||
return count > 10; | ||
}; | ||
const wrapped = loop(fn, DEFAULT_CONTEXT(), { limit: 50 }); | ||
await assertRejectsWithResponse(wrapped, 504, /^timeout after/); | ||
}); | ||
}); |
Oops, something went wrong.