Skip to content

Commit

Permalink
optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
professorabhay committed Jul 25, 2024
1 parent 91f6571 commit c0106d0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/lib/free-queue/free-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class FreeQueue {
// match with this buffer obejct.

if (arraySequence.length !== this._channelCount) {
throw new Error('Channel count mismatch');
throw new Error(`Channel count mismatch: expected ${this._channelCount}, but got ${arraySequence.length}.`);
}

// Transfer data from the |arraySequence| storage to the internal buffer.
Expand Down Expand Up @@ -220,7 +220,7 @@ class FreeQueue {
// match with this buffer obejct.

if (arraySequence.length !== this._channelCount) {
throw new Error('Channel count mismatch');
throw new Error(`Channel count mismatch: expected ${this._channelCount}, but got ${arraySequence.length}.`);
}

// If the FIFO is completely empty, do nothing.
Expand Down
73 changes: 42 additions & 31 deletions src/lib/free-queue/test/free-queue.test.html
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FreeQueue Tests</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/10.6.0/mocha.css" integrity="sha512-xPbP9qFwQklQFQX6R6+36vNq6mZatPrpfEKUB/zWASwZvfDBy8Y2gEpkRuDlZ0WCiZZGMPyluZif5v2KKxylqg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
.head {
margin-top: 5vh;
text-align: center;
}
button {
margin-left: 45vw;
margin-top: 30vh;
padding: 10px 20px;
font-size: 16px;
}
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FreeQueue Tests</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/10.6.0/mocha.css"
integrity="sha512-xPbP9qFwQklQFQX6R6+36vNq6mZatPrpfEKUB/zWASwZvfDBy8Y2gEpkRuDlZ0WCiZZGMPyluZif5v2KKxylqg=="
crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
.head {
margin-top: 5vh;
text-align: center;
}

button {
margin-left: 45vw;
margin-top: 30vh;
padding: 10px 20px;
font-size: 16px;
}
</style>
</head>

<body>
<h1 class="head">FreeQueue Test</h1>
<div id="mocha"></div>
<button id="run-tests">Run Tests</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/10.6.0/mocha.min.js" integrity="sha512-Wt8lK9Rbdq3uAOR9lm7Fy+XTPP0xie3DzFQ0JTzQLGPvdZ7tDFMqHOJq6hbB4v8uvj7xaBEsINRs7K7HTfi9wA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/5.1.1/chai.js" integrity="sha512-HLs4bXIGAstDHJcCm2HfGhBkTUW9oErgCOCiDEnoOhwCEJLcLZUXX7TKcarluZx1D385vuUziYjD1uNGXy9zzg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
mocha.setup('bdd');
</script>
<script src="../coi-serviceworker.js"></script>
<script type="module" src="./free-queue.test.js"></script>
<script>
document.getElementById('run-tests').addEventListener('click', function() {
mocha.run();
});
</script>
<h1 class="head">FreeQueue Test</h1>
<div id="mocha"></div>
<button id="run-tests">Run Tests</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/10.6.0/mocha.min.js"
integrity="sha512-Wt8lK9Rbdq3uAOR9lm7Fy+XTPP0xie3DzFQ0JTzQLGPvdZ7tDFMqHOJq6hbB4v8uvj7xaBEsINRs7K7HTfi9wA=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/5.1.1/chai.js"
integrity="sha512-HLs4bXIGAstDHJcCm2HfGhBkTUW9oErgCOCiDEnoOhwCEJLcLZUXX7TKcarluZx1D385vuUziYjD1uNGXy9zzg=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
mocha.setup('bdd');
</script>
<!-- We need this coi-serviceworker.js file to make SharedArrayBuffer works properly without any erros -->
<script src="../coi-serviceworker.js"></script>
<script type="module" src="./free-queue.test.js"></script>
<script>
document.getElementById('run-tests').addEventListener('click', function () {
mocha.run();
});
</script>
</body>
</html

</html>
39 changes: 27 additions & 12 deletions src/lib/free-queue/test/free-queue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ import { FreeQueue, MAX_CHANNEL_COUNT, RENDER_QUANTUM_FRAMES } from '../free-que

// Mock WASM module
const mockWasmModule = {
_malloc: (size) => new ArrayBuffer(size), // Simulate memory allocation
_free: () => {}, // Simulate memory deallocation
HEAPF32: new Float32Array(1024), // Simulate HEAPF32
// Simulate memory allocation
_malloc: (size) => new ArrayBuffer(size),
// Simulate memory deallocation
_free: () => {},
// Simulate HEAPF32
HEAPF32: new Float32Array(1024),
};

describe('FreeQueue Class', () => {
const bufferLength = 512;
const channelCount = 2;
const maxChannelCount = 4;
let freeQueue;
let freeQueue = null;

beforeEach(() => {
freeQueue = new FreeQueue(mockWasmModule, bufferLength, channelCount, maxChannelCount);
Expand Down Expand Up @@ -106,27 +109,39 @@ describe('FreeQueue Class', () => {

it('should throw an error if pushing with mismatched channel count', () => {
const invalidTestData = [new Float32Array(bufferLength).fill(1)];
expect(() => freeQueue.push(invalidTestData)).to.throw(Error, 'Channel count mismatch');

const expectedChannelCount = freeQueue._channelCount;
const actualChannelCount = invalidTestData.length;

expect(() => freeQueue.push(invalidTestData))
.to.throw(Error, `Channel count mismatch: expected ${expectedChannelCount}, but got ${actualChannelCount}.`);
});

it('should throw an error if pulling with mismatched channel count', () => {
const invalidOutputData = [new Float32Array(bufferLength)];
expect(() => freeQueue.pull(invalidOutputData)).to.throw(Error, 'Channel count mismatch');
const invalidOutputData = [new Float32Array(bufferLength)];

const expectedChannelCount = freeQueue._channelCount;
const actualChannelCount = invalidOutputData.length;

expect(() => freeQueue.pull(invalidOutputData))
.to.throw(Error, `Channel count mismatch: expected ${expectedChannelCount}, but got ${actualChannelCount}.`);
});
});

describe('Performance', () => {
it('should efficiently handle large data transfers', function() {
this.timeout(5000);
const largeBuffer = 1024 * 1024; // 1 MB buffer
// this.timeout(5000);
// 1 MB buffer
const largeBuffer = 1024 * 1024;
const testData = [new Float32Array(largeBuffer).fill(1), new Float32Array(largeBuffer).fill(2)];

const start = performance.now();
freeQueue.push(testData);
freeQueue.pull(testData);
const end = performance.now();

expect(end - start).to.be.below(1000); // Ensure operations complete within 1 second
// Ensure operations complete within 1 second
expect(end - start).to.be.below(1000);
});

it('should perform consistently over many operations', function() {
Expand All @@ -140,8 +155,8 @@ describe('FreeQueue Class', () => {
freeQueue.pull(testData);
}
const end = performance.now();

expect(end - start).to.be.below(5000); // Ensure it completes within 5 seconds
// Ensure it completes within 1 seconds
expect(end - start).to.be.below(1000);
});
});
});

0 comments on commit c0106d0

Please sign in to comment.