訂判斷及所需輸入
兩個假設陣列各生成一百行。短行只有 f0 值 X;長行有 f0 至 f49 五十欄,再加二十字附件標記。UTF-8 JSON 分別1,101、52,801 字節,行數相同也不是同一內容用量。
| 情況 | 輸入 JSON |
|---|---|
| 原例測試 | {"rows":100} |
跑所附比較
程序真正展開各行、序列化並量 Buffer 長度。附件只是題內行內文字,結果不是壓縮流量、二進位檔案大小或真實吞吐測試。
| 路徑 | 輸出 JSON |
|---|---|
| 錯誤路徑 | {"shortBytes":1101,"longBytes":52801,"equalLoad":true} |
| 所需路徑 | {"shortBytes":1101,"longBytes":52801,"equalLoad":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(!smallWhole(i.rows,100))return {held:true};
const generate=(fields,attachment)=>Array.from({length:i.rows},()=>({...Object.fromEntries(Array.from({length:fields},(_,n)=>['f'+n,'X'])),...(attachment?{attachment:'X'.repeat(attachment)}:{})}));
const short=generate(1,0),long=generate(50,20),bytes=x=>Buffer.byteLength(JSON.stringify(x));
return {shortBytes:bytes(short),longBytes:bytes(long),equalLoad:corrected?bytes(short)===bytes(long):short.length===long.length};
};
const cases=[{"input":{"rows":100},"corrected":false,"expected":{"shortBytes":1101,"longBytes":52801,"equalLoad":true}},{"input":{"rows":100},"corrected":true,"expected":{"shortBytes":1101,"longBytes":52801,"equalLoad":false}},{"input":{"rows":1},"corrected":true,"expected":{"shortBytes":12,"longBytes":529,"equalLoad":false}},{"input":{"rows":0},"corrected":true,"expected":{"shortBytes":2,"longBytes":2,"equalLoad":true}}];
for(const test of cases){const actual=await run(test.input,test.corrected);assert.deepEqual(actual,test.expected);console.log(actual);}
按邊界依據作判斷
一行連陣列括號是12、529 字節;空陣列都兩字節,在此序列化形狀相同。
- 訂完整內容形狀
- 真正序列化兩陣列
- 行數及實量一起比
不能用此字節比較推正式頻寬或延遲。
| 條件 | 輸入 JSON | 預期輸出 JSON |
|---|---|---|
| 只有一行有內容 | {"rows":1} | {"shortBytes":12,"longBytes":529,"equalLoad":false} |
| 兩個明確空陣列 | {"rows":0} | {"shortBytes":2,"longBytes":2,"equalLoad":true} |
原例各一百行,可叫測試用量相同嗎?
查看參考答案
不能,真正序列化大小差51,700 字節。行數旁也要留欄位及附件形狀。