Skip to content

Benchmark Scripts and Comparison Results

Abhay Gupta edited this page Aug 29, 2024 · 1 revision

This document contains the result of the comparison of FreeQueueSAB with Ringbuf.js and all the script files used in benchmarking. This is done under the task of GSOC'24 by Abhay Gupta.

Date - 28/08/2024

Let's Start

  • For the ringbuf.js, I copied the file from the original project and pasted it into our project (inside lib\free-queue)

Files

benchmark.js (main file)

import { FreeQueueSAB } from './free-queue-sab.js'; 
import { RingBuffer } from './ringbuf.js'; 
import { Worker } from 'worker_threads';

// Configuration
const bufferSize = 1024; 
const channelCount = 1; 
const iterations = 100000; 

// Create instances
const freeQueue = new FreeQueueSAB(bufferSize, channelCount);
const sab = RingBuffer.getStorageForCapacity(bufferSize, Float32Array);
const ringBuffer = new RingBuffer(sab, Float32Array);

// Data to push
const inputData = [new Float32Array(bufferSize)];
for (let i = 0; i < bufferSize; i++) {
    inputData[0][i] = Math.random(); // Fill with random data
}
const outputDataFreeQueue = [new Float32Array(bufferSize)];
const outputDataRingBuffer = new Float32Array(bufferSize);

// Benchmark FreeQueueSAB
console.time('FreeQueueSAB');
for (let i = 0; i < iterations; i++) {
    freeQueue.push(inputData, bufferSize);
    freeQueue.pull(outputDataFreeQueue, bufferSize);
}
console.timeEnd('FreeQueueSAB');

// Benchmark RingBuffer
console.time('RingBuffer');
for (let i = 0; i < iterations; i++) {
    ringBuffer.push(inputData[0], bufferSize);
    ringBuffer.pop(outputDataRingBuffer, bufferSize);
}
console.timeEnd('RingBuffer');



// Number of worker threads
const workerCount = 4;

// Helper to create and run workers
const runWorkers = (workerFile) => {
    return new Promise((resolve) => {
        let completed = 0;

        for (let i = 0; i < workerCount; i++) {
            const worker = new Worker(workerFile);

            worker.on('message', (msg) => {
                if (msg === 'done') {
                    completed++;
                    if (completed === workerCount) {
                        resolve();
                    }
                }
            });

            worker.postMessage('start');
        }
    });
};

(async () => {
    console.time('FreeQueueSAB Multithreaded');
    await runWorkers('./worker-freequeue.js');
    console.timeEnd('FreeQueueSAB Multithreaded');

    console.time('RingBuffer Multithreaded');
    await runWorkers('./worker-ringbuffer.js');
    console.timeEnd('RingBuffer Multithreaded');
})();

worker-freequeueSAB.js (support file - 1)

import { parentPort } from 'worker_threads';
import { FreeQueueSAB } from './free-queue-sab.js';

const bufferSize = 1024;
const channelCount = 1;
const iterations = 100000;

const freeQueue = new FreeQueueSAB(bufferSize, channelCount);
const inputData = [new Float32Array(bufferSize)];
for (let i = 0; i < bufferSize; i++) {
    inputData[0][i] = Math.random();
}
const outputData = [new Float32Array(bufferSize)];

parentPort.on('message', () => {
    for (let i = 0; i < iterations; i++) {
        freeQueue.push(inputData, bufferSize);
        freeQueue.pull(outputData, bufferSize);
    }
    parentPort.postMessage('done');
});

worker-ringbuf.js (support file - 2)

import { parentPort } from 'worker_threads';
import { RingBuffer } from './ringbuf.js';

const bufferSize = 1024;
const iterations = 100000;

const sab = RingBuffer.getStorageForCapacity(bufferSize, Float32Array);
const ringBuffer = new RingBuffer(sab, Float32Array);

const inputData = new Float32Array(bufferSize);
for (let i = 0; i < bufferSize; i++) {
    inputData[i] = Math.random();
}
const outputData = new Float32Array(bufferSize);

parentPort.on('message', () => {
    for (let i = 0; i < iterations; i++) {
        ringBuffer.push(inputData, bufferSize);
        ringBuffer.pop(outputData, bufferSize);
    }
    parentPort.postMessage('done');
});

File Structure

image

Results

image

image

According to the results of the operation, We found that our implementation is more optimized than ringbuf.js