Skip to content

Commit

Permalink
Merge pull request #76 from IQEngine/bug_fixes
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
lmiguelgato authored Sep 15, 2023
2 parents f7ce142 + df3fd2a commit d513c45
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 37 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ A file webfftWrapper.js will exist in all sub-library dirs, so that we can avoid
### How to build and run our benchmarks

1. `npm install`
2. `npm run build`
3. `npm install --global serve`
2. `npm install --global serve`
4. `npm run serve`
5. Open your browser to http://localhost:8080

Expand Down
2 changes: 2 additions & 0 deletions examples/basicUsage.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ for (let i = 0; i < fftsize; i++) {
}
const outputArrReal = fft.fftr(inputArrReal);
console.log(outputArrReal);

fft.dispose();
1 change: 1 addition & 0 deletions examples/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import webfft from "../lib/main.js";
const fftsize = 1024;
const fft = new webfft(fftsize);
const profileObj = fft.profile(2); // duration to run profile, in seconds
fft.dispose();
console.log(profileObj);
console.log("Fastest sub-library:", profileObj.fastestSubLibrary);

Expand Down
1 change: 1 addition & 0 deletions lib/@types/webfft/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default class webfft {
availableSubLibraries(): string[];
profile(duration?: number): ProfileResult;
checkBrowserCapabilities(): Promise<BrowserCapabilities>;
dispose(): void;
}

export interface ProfileResult {
Expand Down
25 changes: 13 additions & 12 deletions lib/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ window.onload = function () {
const fft = new webfft(fftsize);

const profileObj = fft.profile(duration);
fft.dispose();
console.log(profileObj);

let results = profileObj.ffsPerSecond;
let results = profileObj.fftsPerSecond;

let test_names = [];
let barColors = [];
Expand All @@ -43,8 +44,8 @@ window.onload = function () {
x: xArray,
y: yArray,
type: "bar",
marker: { color: barColors }
}
marker: { color: barColors },
},
];

const layout = {
Expand All @@ -54,9 +55,9 @@ window.onload = function () {
text: "FFTs per Second",
font: {
family: "sans serif",
size: 18
}
}
size: 18,
},
},
},
annotations: [
{
Expand All @@ -67,8 +68,8 @@ window.onload = function () {
font: {
family: "sans serif",
size: 18,
color: "rgba(255,0,0,1)"
}
color: "rgba(255,0,0,1)",
},
},
{
x: 1,
Expand All @@ -78,10 +79,10 @@ window.onload = function () {
font: {
family: "sans serif",
size: 18,
color: "rgba(0,0,255,1)"
}
}
]
color: "rgba(0,0,255,1)",
},
},
],
};

Plotly.newPlot("myPlot", sortDescending(data), layout);
Expand Down
2 changes: 1 addition & 1 deletion lib/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<html>
<head>
<script type="module" src="./dist/bundle.js"></script>
<script type="module" src="./benchmark.js"></script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
Expand Down
6 changes: 6 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ class webfft {
async checkBrowserCapabilities() {
return await checkBrowserCapabilities();
}

dispose() {
if (this.fftLibrary && this.fftLibrary.dispose !== undefined) {
this.fftLibrary.dispose();
}
}
}

export default webfft;
17 changes: 0 additions & 17 deletions lib/utils/replaceStrings.js

This file was deleted.

14 changes: 11 additions & 3 deletions lib/utils/sortPerformance.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ function sortDescending(data) {
let yArray = data[0].y;
let barColors = data[0].marker.color;

if (!yArray || yArray.length == 0) {
return data;
}

// Create an array of objects from xArray, yArray, and barColors
let combined = xArray.map((x, i) => ({ x: x, y: yArray[i], color: barColors[i] }));
let combined = xArray.map((x, i) => ({
x: x,
y: yArray[i],
color: barColors[i],
}));

// Sort combined array by 'y' property in descending order
combined.sort((a, b) => b.y - a.y);
Expand All @@ -21,8 +29,8 @@ function sortDescending(data) {
x: sortedXArray,
y: sortedYArray,
type: "bar",
marker: { color: sortedBarColors }
}
marker: { color: sortedBarColors },
},
];

return sortedData;
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"main": "lib/main.js",
"types": "lib/@types/webfft/index.d.ts",
"scripts": {
"serve": "node ./lib/utils/replaceStrings.js lib/index.html \"benchmark\" \"dist/bundle\" && serve lib -l 8080",
"serve_dev": "node ./lib/utils/replaceStrings.js lib/index.html \"dist/bundle\" \"benchmark\" && serve lib -l 8080",
"serve": "serve lib -l 8080",
"build": "webpack --config ./webpack.config.cjs --output-path ./lib/dist",
"test": "vitest --config ./tests/vitest.config.ts"
},
Expand Down
1 change: 1 addition & 0 deletions site/src/components/BenchmarkSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function BenchmarkSection({
.catch((error) => {
console.error("Failed to check browser capabilities:", error);
});
webfftInstance.dispose();
}, []);

useEffect(() => {
Expand Down
3 changes: 3 additions & 0 deletions site/src/docs/GettingStarted.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const out = fft.fft(input); // out will be a Float32Array of size 2048
const out = fft.fft(input, 'indutny');
//or
const out = fft.fft(input, profileResults[0]['SubLibraryName']); // profileResults obj will likely be changed later

// Dispose (release Wasm memory, etc.)
fft.dispose();
```

### Sub-Library API
Expand Down
1 change: 1 addition & 0 deletions site/src/utils/webworker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ onmessage = (e: MessageEvent<[number, number]>) => {
const [fftSize, duration] = e.data;
const fft = new webfft(fftSize);
const profileObj: ProfileResult = fft.profile(duration);
fft.dispose();

self.postMessage(profileObj);
};
8 changes: 8 additions & 0 deletions tests/fft.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ test("basic usage", () => {
const outputArr = fft.fft(inputArr);
expect(outputArr[0]).not.toBe(0);
expect(outputArr.length).toBe(2 * fftsize);
fft.dispose();
});

test("available sublibraries", () => {
const fft = new webfft(1024);
const availableSubLibraries = fft.availableSubLibraries();
expect(availableSubLibraries.length).toBeGreaterThan(1);
fft.dispose();
});

test("outputs for all sublibs approx match using different fftsizes", () => {
Expand Down Expand Up @@ -57,6 +59,8 @@ test("outputs for all sublibs approx match using different fftsizes", () => {
}
expect(Math.abs(total - goldenTotal)).toBeLessThan(goldenTotal * 1e-7);
}

fft.dispose();
}
});

Expand Down Expand Up @@ -90,6 +94,8 @@ test("fftr", () => {
}
expect(Math.abs(total - goldenTotal)).toBeLessThan(goldenTotal * 1e-7);
}

fft.dispose();
});

test("int16 inputs", () => {
Expand Down Expand Up @@ -122,4 +128,6 @@ test("int16 inputs", () => {
}
expect(Math.abs(total - goldenTotal)).toBeLessThan(goldenTotal * 1e-7);
}

fft.dispose();
});
1 change: 1 addition & 0 deletions tests/profile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ test("run profile", () => {
const elapsed = (performance.now() - start) / 1e3;
expect(elapsed).toBeGreaterThan(duration);
expect(elapsed).toBeLessThan(duration * 1.5); // possibility for this to error in the future if run on a super slow machine
fft.dispose();
});

0 comments on commit d513c45

Please sign in to comment.