Skip to content

Commit

Permalink
Added a simple benchmarking suite. Run with 'make benchmark'
Browse files Browse the repository at this point in the history
  • Loading branch information
corbanbrook committed Jan 24, 2011
1 parent 864bad1 commit e361d92
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Make sure $JSSHELL points to your js shell binary in .profile or .bashrc
# Most targets use commands that need a js shell path specified
JSSHELL ?= $(error Specify a valid path to a js shell binary in ~/.profile: export JSSHELL=C:\path\js.exe or /path/js)

# Version number used in naming release files.
VERSION ?= $(error Specify a version for your release (e.g., VERSION=0.5))
FREQUENCY ?= 440

benchmark:
${JSSHELL} -j -e 'var FREQUENCY=${FREQUENCY};' -f dsp.js -f ./bench/bench.js -f ./bench/dft.js
${JSSHELL} -j -e 'var FREQUENCY=${FREQUENCY};' -f dsp.js -f ./bench/bench.js -f ./bench/fft.js
${JSSHELL} -j -e 'var FREQUENCY=${FREQUENCY};' -f dsp.js -f ./bench/bench.js -f ./bench/rfft.js

clean:
rm -fr ./release
12 changes: 12 additions & 0 deletions bench/bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function benchmark(func, loopCount) {
loopCount = loopCount || 10000;

var start = Date.now();

for (var i = 0; i < loopCount; i++) {
func();
}

var end = Date.now();
return end - start;
}
20 changes: 20 additions & 0 deletions bench/dft.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var bufferSize = 2048;
var sampleRate = 44100;
var frequency = FREQUENCY || 440;

var dft = new DFT(bufferSize, sampleRate);
var osc = new Oscillator(DSP.SAW, frequency, 1.0, bufferSize, sampleRate);
var signal = osc.generate();

var duration = benchmark(function() { dft.forward(signal); }, 20);

var peakBand = 0;

for (var i = 0; i < dft.spectrum.length; i++) {
peakBand = (dft.spectrum[i] > dft.spectrum[peakBand]) ? i : peakBand;
}

var peakFreq = dft.getBandFrequency(dft.peakBand);

print("Detected peak: " + peakFreq + " Hz (error " + Math.abs(peakFreq - frequency) + " Hz)");
print("20 DFTs: " + (duration) + " ms (" + ((duration) / 20) + "ms per DFT)\n");
20 changes: 20 additions & 0 deletions bench/fft.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var bufferSize = 2048;
var sampleRate = 44100;
var frequency = FREQUENCY || 440;

var fft = new FFT(bufferSize, sampleRate);
var osc = new Oscillator(DSP.SAW, frequency, 1.0, bufferSize, sampleRate);
var signal = osc.generate();

var duration = benchmark(function() { fft.forward(signal); });

var peakBand = 0;

for (var i = 0; i < fft.spectrum.length; i++) {
peakBand = (fft.spectrum[i] > fft.spectrum[peakBand]) ? i : peakBand;
}

var peakFreq = fft.getBandFrequency(fft.peakBand);

print("Detected peak: " + peakFreq + " Hz (error " + Math.abs(peakFreq - frequency) + " Hz)");
print("10000 FFTs: " + (duration) + " ms (" + ((duration) / 10000) + "ms per FFT)\n");
20 changes: 20 additions & 0 deletions bench/rfft.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var bufferSize = 2048;
var sampleRate = 44100;
var frequency = FREQUENCY || 440;

var fft = new RFFT(bufferSize, sampleRate);
var osc = new Oscillator(DSP.SAW, frequency, 1.0, bufferSize, sampleRate);
var signal = osc.generate();

var duration = benchmark(function() { fft.forward(signal); });

var peakBand = 0;

for (var i = 0; i < fft.spectrum.length; i++) {
peakBand = (fft.spectrum[i] > fft.spectrum[peakBand]) ? i : peakBand;
}

var peakFreq = fft.getBandFrequency(fft.peakBand);

print("Detected peak: " + peakFreq + " Hz (error " + Math.abs(peakFreq - frequency) + " Hz)");
print("10000 FFTs: " + (duration) + " ms (" + ((duration) / 10000) + "ms per FFT)\n");

0 comments on commit e361d92

Please sign in to comment.