File size: 2,197 Bytes
bfaf968 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
import {sleep} from './utils.js';
/**
* JobScheduler emits jobs based on predefined patterns.
* Can be used to simulate different load scenarios like batch processing or on-request per second
*/
export class JobScheduler {
constructor(promptSource = []) {
this.promptSource = promptSource;
this.running = false;
this._onJob = null; // callback
}
onJob(cb) {
this._onJob = cb;
}
/**
* Start emitting jobs based on the selected pattern
* TODO: Implement different patterns to simulate
* TODO: Run different datasets instead of just simple prompts
* @param patternName
* @returns {Promise<void>}
*/
async startPattern(patternName) {
this.running = true;
// once per second until user stopp evaluation
if (patternName === 'once-per-sec') {
let i = 0;
while (this.running) {
this._emit(i++);
await sleep(1000);
}
} else if (patternName === 'ten-per-sec') {
let i = 0;
const interval = 100; // ms
while (this.running) {
this._emit(i++);
await sleep(interval);
}
} else if (patternName === 'batch-10-every-5s') {
let i = 0;
while (this.running) {
for (let j = 0; j < 10 && this.running; j++) this._emit(i++);
await sleep(5000);
}
} else if (patternName === 'burst') {
// single burst
for (let i = 0; i < 50; i++) this._emit(i);
this.running = false;
}
}
/**
* Stop emitting jobs
*/
stop() {
this.running = false;
}
_pickPrompt(id) {
if (this.promptSource.length === 0) return {prompt: `Hello world ${id}`, groundTruth: `Hello world ${id}`};
return this.promptSource[id % this.promptSource.length];
}
_emit(id) {
if (this._onJob) {
const p = this._pickPrompt(id);
const job = {id: `job-${Date.now()}-${id}`, prompt: p.prompt, groundTruth: p.groundTruth};
this._onJob(job);
}
}
} |