跳至內容
Astera Technology

月額足夠,也要核一秒內的上限

同時核短時間集中用量及整段總額,不能互相代替。

下載完整個案資料及參考答案 ↓

訂判斷及所需輸入

假設方案一秒最多五次、每月一千次;需要一秒二十次,整月一百次。月額符合,集中使用卻超額。本例採購要求兩項都符合。

已填本地測試工作表
情況輸入 JSON
原例測試{"perSecond":5,"perMonth":1000,"oneSecondDemand":20,"monthDemand":100}
下載可編輯 CSV ↓

跑所附比較

這是上限核對,沒有假定重試或排隊功能。工作可否等,要另有業務條件;不能自動加延遲便說即時任務已符合。

錯誤及所需輸出
路徑輸出 JSON
錯誤路徑{"monthFits":true,"burstFits":false,"overallFits":true}
所需路徑{"monthFits":true,"burstFits":false,"overallFits":false}
下載可編輯 CSV ↓
完整本地 Node24.14.1 程序
import assert from 'node:assert/strict';
const smallWhole=(x,max)=>Number.isSafeInteger(x)&&x>=0&&x<=max;

const run=function run(i,corrected=true){
    if(![i.perSecond,i.perMonth,i.oneSecondDemand,i.monthDemand].every(x=>smallWhole(x,1000000)))return {held:true};
    const monthFits=i.monthDemand<=i.perMonth,burstFits=i.oneSecondDemand<=i.perSecond;
    return {monthFits,burstFits,overallFits:corrected?monthFits&&burstFits:monthFits};
  };
const cases=[{"input":{"perSecond":5,"perMonth":1000,"oneSecondDemand":20,"monthDemand":100},"corrected":false,"expected":{"monthFits":true,"burstFits":false,"overallFits":true}},{"input":{"perSecond":5,"perMonth":1000,"oneSecondDemand":20,"monthDemand":100},"corrected":true,"expected":{"monthFits":true,"burstFits":false,"overallFits":false}},{"input":{"perSecond":5,"perMonth":1000,"oneSecondDemand":5,"monthDemand":1000},"corrected":true,"expected":{"monthFits":true,"burstFits":true,"overallFits":true}},{"input":{"perSecond":null,"perMonth":1000,"oneSecondDemand":20,"monthDemand":100},"corrected":true,"expected":{"held":true}}];
for(const test of cases){const actual=await run(test.input,test.corrected);assert.deepEqual(actual,test.expected);console.log(actual);}
下載可執行例子 ↓

按邊界依據作判斷

剛好每秒五次、每月一千,符合本例包含邊界的上限。集中用量配額未知,不能判兩項整體符合。

  1. 先核兩個時間尺度
  2. 各自對所需用量
  3. 即時任務須兩者都符合

未提供短時上限便列未明。

可編輯邊界輸入及結果
條件輸入 JSON預期輸出 JSON
兩項剛好上限{"perSecond":5,"perMonth":1000,"oneSecondDemand":5,"monthDemand":1000}{"monthFits":true,"burstFits":true,"overallFits":true}
短時配額未知{"perSecond":null,"perMonth":1000,"oneSecondDemand":20,"monthDemand":100}{"held":true}
下載可編輯 CSV ↓

原例只看月用量,可標方案合適嗎?

查看參考答案

不可。一百未超一千,但所需一秒二十已超五,須另記短時上限不符。

相關資源