跳至內容
Astera Technology

測試用量不只看行數,也要看內容形狀

兩組同行數資料,真正序列化後量位元組。

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

訂判斷及所需輸入

兩個假設陣列各生成一百行。短行只有 f0 值 X;長行有 f0 至 f49 五十欄,再加二十字附件標記。UTF-8 JSON 分別1,101、52,801 字節,行數相同也不是同一內容用量。

已填本地測試工作表
情況輸入 JSON
原例測試{"rows":100}
下載可編輯 CSV ↓

跑所附比較

程序真正展開各行、序列化並量 Buffer 長度。附件只是題內行內文字,結果不是壓縮流量、二進位檔案大小或真實吞吐測試。

錯誤及所需輸出
路徑輸出 JSON
錯誤路徑{"shortBytes":1101,"longBytes":52801,"equalLoad":true}
所需路徑{"shortBytes":1101,"longBytes":52801,"equalLoad":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(!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 字節;空陣列都兩字節,在此序列化形狀相同。

  1. 訂完整內容形狀
  2. 真正序列化兩陣列
  3. 行數及實量一起比

不能用此字節比較推正式頻寬或延遲。

可編輯邊界輸入及結果
條件輸入 JSON預期輸出 JSON
只有一行有內容{"rows":1}{"shortBytes":12,"longBytes":529,"equalLoad":false}
兩個明確空陣列{"rows":0}{"shortBytes":2,"longBytes":2,"equalLoad":true}
下載可編輯 CSV ↓

原例各一百行,可叫測試用量相同嗎?

查看參考答案

不能,真正序列化大小差51,700 字節。行數旁也要留欄位及附件形狀。

相關資源