訂判斷及所需輸入
假設方案一秒最多五次、每月一千次;需要一秒二十次,整月一百次。月額符合,集中使用卻超額。本例採購要求兩項都符合。
| 情況 | 輸入 JSON |
|---|---|
| 原例測試 | {"perSecond":5,"perMonth":1000,"oneSecondDemand":20,"monthDemand":100} |
跑所附比較
這是上限核對,沒有假定重試或排隊功能。工作可否等,要另有業務條件;不能自動加延遲便說即時任務已符合。
| 路徑 | 輸出 JSON |
|---|---|
| 錯誤路徑 | {"monthFits":true,"burstFits":false,"overallFits":true} |
| 所需路徑 | {"monthFits":true,"burstFits":false,"overallFits":false} |
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);}
按邊界依據作判斷
剛好每秒五次、每月一千,符合本例包含邊界的上限。集中用量配額未知,不能判兩項整體符合。
- 先核兩個時間尺度
- 各自對所需用量
- 即時任務須兩者都符合
未提供短時上限便列未明。
| 條件 | 輸入 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} |
原例只看月用量,可標方案合適嗎?
查看參考答案
不可。一百未超一千,但所需一秒二十已超五,須另記短時上限不符。