| import {sleep} from './utils.js'; |
| import {DatasetLoader} from "./datasetLoader.js"; |
|
|
| |
| |
| |
| |
| export class JobScheduler { |
| constructor(datasetName = 'boolq_validation') { |
| this.running = false; |
| this._dataset = null; |
| this._onJob = null; |
| this._datasetName = datasetName |
| this._interArrivalTimeLambda = 2; |
| this.datasetLoader = new DatasetLoader(this._datasetName) |
| this.datasetLoader.loadDataset(this._datasetName).then((dataset) => { |
| this._dataset = dataset; |
| }); |
| } |
|
|
| setDatasetName(datasetName) { |
| this._datasetName = datasetName; |
| } |
|
|
|
|
| onJob(cb) { |
| this._onJob = cb; |
| } |
|
|
|
|
| |
| |
| |
| |
| |
| |
| async startPattern(patternName, maxJobs = Infinity) { |
| this.running = true; |
| let jobsEmitted = 0; |
|
|
| if (maxJobs !== Infinity) { |
| console.log(`π Starting limited run: ${maxJobs} jobs with pattern '${patternName}'`); |
| } |
|
|
| if (patternName === 'once-per-sec') { |
| while (this._dataset.length > 0 && this.running && jobsEmitted < maxJobs) { |
| const item = this._dataset.shift(); |
| this._emit(item); |
| jobsEmitted++; |
| if (jobsEmitted < maxJobs && this._dataset.length > 0 && this.running) { |
| await sleep(1000); |
| } |
| } |
| } else if (patternName === 'every-ten-sec') { |
| while (this._dataset.length > 0 && this.running && jobsEmitted < maxJobs) { |
| const item = this._dataset.shift(); |
| this._emit(item); |
| jobsEmitted++; |
| if (jobsEmitted < maxJobs && this._dataset.length > 0 && this.running) { |
| await sleep(10000); |
| } |
| } |
| } else if (patternName === 'exponential-arrival') { |
| while (this._dataset.length > 0 && this.running && jobsEmitted < maxJobs) { |
| const item = this._dataset.shift(); |
| this._emit(item); |
| jobsEmitted++; |
| if (jobsEmitted < maxJobs && this._dataset.length > 0 && this.running) { |
| const timeToNextArrival = this._generateExponentialInterarrivalTime(this._interArrivalTimeLambda); |
| await sleep(timeToNextArrival); |
| } |
| } |
| } |
|
|
| if (maxJobs !== Infinity) { |
| console.log(`β
Limited run complete: ${jobsEmitted} jobs emitted.`); |
| } else { |
| console.log(`π Job emission stopped. Total jobs emitted: ${jobsEmitted}`); |
| } |
|
|
| return jobsEmitted; |
| } |
|
|
|
|
| |
| |
| |
| stop() { |
| this.running = false; |
| } |
|
|
| |
| |
| |
| async reloadDataset() { |
| new Promise(async (resolve, reject) => { |
| this._dataset = await this.datasetLoader.loadDataset(this._datasetName); |
|
|
| |
| const checkLoaded = setInterval(() => { |
| if (this._dataset && this._dataset.length > 0) { |
| clearInterval(checkLoaded); |
| resolve(); |
| } |
| }, 100); |
| |
| setTimeout(() => { |
| clearInterval(checkLoaded); |
| reject(new Error('Dataset loading timeout')); |
| }, 10000); |
| }); |
| } |
|
|
|
|
|
|
| |
| |
| |
| |
| |
| |
| _emit(item) { |
| if (this._onJob) { |
| const job = { |
| id: item.id, |
| prompt: item.prompt, |
| groundTruth: item.groundTruth, |
| dataset: this._datasetName, |
| timestamps: { |
| jobStart: Date.now(), |
| inferenceStart: null, |
| inferenceEnd: null |
| } |
| }; |
| this._onJob(job); |
| } |
| } |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| _generateExponentialInterarrivalTime(lambda) { |
| const u = Math.random(); |
| return -Math.log(u) / lambda * 1000; |
| } |
| } |