@syntest/search
Version:
The common core of the SynTest Framework
123 lines • 3.47 kB
JavaScript
"use strict";
/*
* Copyright 2020-2021 Delft University of Technology and SynTest contributors
*
* This file is part of SynTest Framework - SynTest Core.
*
* Licensed 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 CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SearchTimeBudget = void 0;
const logging_1 = require("@syntest/logging");
/**
* Budget for the search time of the search process.
*
* @author Mitchell Olsthoorn
*/
class SearchTimeBudget {
/**
* Constructor.
*
* @param maxSearchTime The maximum allowed time in seconds this budget should use
*/
constructor(maxSearchTime = Number.MAX_SAFE_INTEGER) {
SearchTimeBudget.LOGGER = (0, logging_1.getLogger)("SearchTimeBudget");
this._currentSearchTime = 0;
this._maxSearchTime = maxSearchTime;
this._counterTime = 0;
this._tracking = false;
}
/**
* @inheritDoc
*/
getRemainingBudget() {
if (this.getUsedBudget() > this._maxSearchTime) {
SearchTimeBudget.LOGGER.info(`Consumed ${this.getUsedBudget() - this._maxSearchTime}s over the allocated search time`);
}
return Math.max(this._maxSearchTime - this.getUsedBudget(), 0);
}
/**
* @inheritDoc
*/
getUsedBudget() {
if (this._tracking) {
const currentTime = Date.now() / 1000;
return this._currentSearchTime + (currentTime - this._counterTime);
}
else {
return this._currentSearchTime;
}
}
/**
* @inheritDoc
*/
getTotalBudget() {
return this._maxSearchTime;
}
/**
* @inheritDoc
*/
reset() {
SearchTimeBudget.LOGGER.silly("reset");
this._currentSearchTime = 0;
this._counterTime = 0;
this._tracking = false;
}
/**
* @inheritDoc
*/
initializationStarted() {
SearchTimeBudget.LOGGER.silly("initializationStarted");
}
/**
* @inheritDoc
*/
initializationStopped() {
SearchTimeBudget.LOGGER.silly("initializationStopped");
}
/**
* @inheritDoc
*/
searchStarted() {
SearchTimeBudget.LOGGER.silly("searchStarted");
if (!this._tracking) {
this._counterTime = Date.now() / 1000;
this._tracking = true;
}
}
/**
* @inheritDoc
*/
searchStopped() {
SearchTimeBudget.LOGGER.silly("searchStopped");
if (this._tracking) {
this._currentSearchTime = this.getUsedBudget();
this._counterTime = 0;
this._tracking = false;
}
}
/**
* @inheritDoc
*/
iteration() {
SearchTimeBudget.LOGGER.silly("iteration");
}
/**
* @inheritDoc
*/
evaluation() {
SearchTimeBudget.LOGGER.silly("evaluation");
}
}
exports.SearchTimeBudget = SearchTimeBudget;
//# sourceMappingURL=SearchTimeBudget.js.map