viewof chunkSizeKB = Inputs.range([1, 256], {value: 64, step: 1, label: "Chunk size (KB)"})
viewof totalReadings = Inputs.range([100, 50000], {value: 5000, step: 100, label: "Total sensor readings"})
viewof readingSizeBytes = Inputs.range([20, 500], {value: 80, step: 10, label: "Avg reading size (bytes)"})
viewof gwAvailableRam = Inputs.range([16, 512], {value: 64, step: 8, label: "Gateway available RAM (MB)"})Chunk Size Optimizer
Chunk Size Optimizer
animation
http
app-protocols
Try Change Chunk size (KB) while keeping the readings fixed. Then press Calculate chunk overhead.
Observe After pressing Calculate chunk overhead, watch memory per stream and chunk overhead as Chunk size (KB) changes.
Explain Explain why Chunk size (KB) changes concurrent uploads and chunk overhead after you press Calculate chunk overhead.
Technical boundaries. Only 1 chunk is buffered at a time. HTTP overhead is fixed at 50 bytes per chunk.
NoteTry It: Chunk Size Optimizer
{
const chunkBytes = chunkSizeKB * 1024;
const totalBytes = totalReadings * readingSizeBytes;
const totalMB = (totalBytes / (1024 * 1024)).toFixed(2);
const readingsPerChunk = Math.floor(chunkBytes / readingSizeBytes);
const numChunks = Math.ceil(totalReadings / readingsPerChunk);
// Memory: only 1 chunk buffered at a time
const streamMemMB = chunkSizeKB / 1024;
// Unbounded: entire payload in memory
const unboundedMemMB = totalBytes / (1024 * 1024);
const unboundedFits = unboundedMemMB < gwAvailableRam;
// Concurrent streams possible
const maxConcurrentStreams = Math.floor(gwAvailableRam / streamMemMB);
const maxConcurrentUnbounded = Math.floor(gwAvailableRam / unboundedMemMB);
// Network efficiency: more chunks = more HTTP overhead
const httpOverheadPerChunk = 50; // bytes for chunk headers
const totalOverhead = numChunks * httpOverheadPerChunk;
const overheadPercent = (totalOverhead / totalBytes * 100).toFixed(3);
// Checkpoint interval recommendation
const checkpointEvery = Math.max(1, Math.floor(numChunks / 10)); // ~10 checkpoints
return html`
<div style="font-family: Arial, sans-serif; padding: 15px; background: #f8f9fa; border-radius: 8px; border-left: 4px solid #3498DB;">
<p style="margin-top: 0; margin-bottom: 0.8em; color: #2C3E50; font-weight: 700; font-size: 1.1em;">Chunk Streaming Analysis</p>
<div style="background: white; padding: 12px 15px; border-radius: 6px; margin-bottom: 15px; font-size: 0.9em; color: #7F8C8D;">
Upload: <strong style="color: #2C3E50;">${totalReadings.toLocaleString()} readings</strong> (${totalMB} MB total) in <strong style="color: #2C3E50;">${numChunks}</strong> chunks of <strong style="color: #2C3E50;">${readingsPerChunk}</strong> readings each
</div>
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px; margin-bottom: 15px;">
<div style="background: white; padding: 15px; border-radius: 6px; border-top: 3px solid #16A085;">
<div style="font-size: 0.85em; color: #7F8C8D; margin-bottom: 5px;">Bounded Chunking (${chunkSizeKB} KB)</div>
<div style="font-size: 1.6em; font-weight: bold; color: #16A085;">${streamMemMB.toFixed(2)} MB</div>
<div style="font-size: 0.85em; color: #7F8C8D;">memory per stream</div>
<div style="margin-top: 10px; padding: 6px 10px; background: #e8f5e9; border-radius: 4px; font-size: 0.85em; color: #16A085;">
Supports <strong>${maxConcurrentStreams.toLocaleString()}</strong> concurrent uploads
</div>
</div>
<div style="background: white; padding: 15px; border-radius: 6px; border-top: 3px solid #E74C3C;">
<div style="font-size: 0.85em; color: #7F8C8D; margin-bottom: 5px;">Unbounded (full buffer)</div>
<div style="font-size: 1.6em; font-weight: bold; color: #E74C3C;">${unboundedMemMB.toFixed(2)} MB</div>
<div style="font-size: 0.85em; color: #7F8C8D;">memory per stream</div>
<div style="margin-top: 10px; padding: 6px 10px; background: ${unboundedFits ? '#fff3e0' : '#ffebee'}; border-radius: 4px; font-size: 0.85em; color: ${unboundedFits ? '#E67E22' : '#E74C3C'};">
${unboundedFits ? `Supports only <strong>${Math.max(maxConcurrentUnbounded, 0)}</strong> concurrent uploads` : `<strong>EXCEEDS</strong> available RAM (${gwAvailableRam} MB)!`}
</div>
</div>
</div>
<div style="background: white; padding: 12px 15px; border-radius: 6px;">
<div style="font-size: 0.9em; color: #2C3E50; margin-bottom: 8px;"><strong>Recommendations</strong></div>
<div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; font-size: 0.8em;">
<div style="padding: 6px 8px; background: #f0f0f0; border-radius: 4px;">
<div style="color: #7F8C8D;">Chunk overhead</div>
<div style="font-weight: bold; color: #2C3E50;">${overheadPercent}%</div>
</div>
<div style="padding: 6px 8px; background: #f0f0f0; border-radius: 4px;">
<div style="color: #7F8C8D;">Checkpoint every</div>
<div style="font-weight: bold; color: #2C3E50;">${checkpointEvery} chunks</div>
</div>
<div style="padding: 6px 8px; background: #f0f0f0; border-radius: 4px;">
<div style="color: #7F8C8D;">Memory savings</div>
<div style="font-weight: bold; color: #16A085;">${(unboundedMemMB / streamMemMB).toFixed(0)}x less</div>
</div>
</div>
</div>
</div>
`;
}