comparison perf-test-node.js @ 34:0eafc96a039c

Add quick perf test that can be run under node instead of in-browser
author Chris Cannam
date Sun, 25 Sep 2016 09:47:56 +0100
parents
children 3faa4e3eedac
comparison
equal deleted inserted replaced
33:e9a0f66ee81c 34:0eafc96a039c
1 "use strict";
2
3 var VampExamplePlugins = require("./VampExamplePlugins");
4 var exampleModule = VampExamplePlugins();
5
6 // It is possible to declare both parameters and return values as
7 // "string", in which case Emscripten will take care of
8 // conversions. But it's not clear how one would manage memory for
9 // newly-constructed returned C strings -- the returned pointer from
10 // vampipeRequestJson would appear (?) to be thrown away by the
11 // Emscripten string converter if we declare it as returning a string,
12 // so we have no opportunity to pass it to vampipeFreeJson, which
13 // suggests this would leak memory if the string isn't static. Not
14 // wholly sure though. Anyway, passing and returning pointers (as
15 // numbers) means we can manage the Emscripten heap memory however we
16 // want in our request wrapper function below.
17
18 var vampipeRequestJson = exampleModule.cwrap(
19 'vampipeRequestJson', 'number', ['number']
20 );
21
22 var vampipeProcessRaw = exampleModule.cwrap(
23 "vampipeProcessRaw", "number", ["number", "number", "number", "number"]
24 );
25
26 var vampipeFreeJson = exampleModule.cwrap(
27 'vampipeFreeJson', 'void', ['number']
28 );
29
30 function note(blah) {
31 console.log(blah);
32 }
33
34 function comment(blah) {
35 console.log(blah);
36 }
37
38 function processRaw(request) {
39
40 const nChannels = request.processInput.inputBuffers.length;
41 const nFrames = request.processInput.inputBuffers[0].values.length;
42
43 const buffersPtr = exampleModule._malloc(nChannels * 4);
44 const buffers = new Uint32Array(
45 exampleModule.HEAPU8.buffer, buffersPtr, nChannels);
46
47 for (let i = 0; i < nChannels; ++i) {
48 const framesPtr = exampleModule._malloc(nFrames * 4);
49 const frames = new Float32Array(
50 exampleModule.HEAPU8.buffer, framesPtr, nFrames);
51 frames.set(request.processInput.inputBuffers[i].values);
52 buffers[i] = framesPtr;
53 }
54
55 const responseJson = vampipeProcessRaw(
56 request.pluginHandle,
57 buffersPtr,
58 request.processInput.timestamp.s,
59 request.processInput.timestamp.n);
60
61 for (let i = 0; i < nChannels; ++i) {
62 exampleModule._free(buffers[i]);
63 }
64 exampleModule._free(buffersPtr);
65
66 const response = JSON.parse(
67 exampleModule.Pointer_stringify(responseJson));
68
69 vampipeFreeJson(responseJson);
70
71 return response;
72 }
73
74 function request(jsonStr) {
75 note("Request JSON = " + jsonStr);
76 var m = exampleModule;
77 // Inspection reveals that intArrayFromString converts the string
78 // from utf16 to utf8, which is what we want (though the docs
79 // don't mention this). Note the *Cstr values are Emscripten heap
80 // pointers
81 var inCstr = m.allocate(m.intArrayFromString(jsonStr), 'i8', m.ALLOC_NORMAL);
82 var outCstr = vampipeRequestJson(inCstr);
83 m._free(inCstr);
84 var result = m.Pointer_stringify(outCstr);
85 vampipeFreeJson(outCstr);
86 note("Returned JSON = " + result);
87 return result;
88 }
89
90 function test() {
91
92 comment("Loading zero crossings plugin...");
93 let result = request('{"type":"load","content": {"pluginKey":"vamp-example-plugins:zerocrossing","inputSampleRate":44100,"adapterFlags":["AdaptAllSafe"]}}');
94
95 const blockSize = 1024;
96
97 result = request('{"type":"configure","content":{"pluginHandle":1,"configuration":{"blockSize": ' + blockSize + ', "channelCount": 1, "stepSize": ' + blockSize + '}}}');
98
99 const nblocks = 1000;
100
101 let processInputBuffers = [new Float32Array(
102 Array.from(Array(blockSize).keys(), n => n / blockSize))
103 ];
104
105 comment("Now processing " + nblocks + " blocks of 1024 samples each...");
106
107 let start = (new Date()).getTime();
108 comment("Start at " + start);
109
110 for (let i = 0; i < nblocks; ++i) {
111 let ts = { "s": i, "n": 0 }; // wholly bogus, but ZC plugin doesn't use it
112 result = processRaw({
113 "pluginHandle": 1,
114 "processInput": {
115 "timestamp": ts,
116 "inputBuffers": processInputBuffers
117 }
118 });
119 }
120
121 let finish = (new Date()).getTime();
122 comment("Finish at " + finish + " for a time of " + (finish - start) + " ms");
123
124 comment("Again...");
125
126 start = (new Date()).getTime();
127 comment("Start at " + start);
128
129 for (let i = 0; i < nblocks; ++i) {
130 let ts = { "s": i, "n": 0 }; // wholly bogus, but ZC plugin doesn't use it
131 result = processRaw({
132 "pluginHandle": 1,
133 "processInput": {
134 "timestamp": ts,
135 "inputBuffers": processInputBuffers
136 }
137 });
138 }
139
140 finish = (new Date()).getTime();
141 comment("Finish at " + finish + " for a time of " + (finish - start) + " ms");
142
143 comment("Cleaning up the plugin and getting any remaining features...");
144 result = request('{"type":"finish","content":{"pluginHandle":1}}');
145 }
146
147 test();
148