diff --git a/README.md b/README.md index 401f16b..d6aa6ea 100644 --- a/README.md +++ b/README.md @@ -97,21 +97,22 @@ benchmark time (avg) (min … max) p75 p ------------------------------------------------------------ ----------------------------- • OpusEncoder ------------------------------------------------------------ ----------------------------- -mediaplex 282.31 µs/iter (274.4 µs … 746.9 µs) 279.5 µs 378.7 µs 391.3 µs -@discordjs/opus 315.47 µs/iter (310 µs … 460.4 µs) 314.1 µs 384.6 µs 396.8 µs -@evan/opus 304.59 µs/iter (299.9 µs … 483.7 µs) 303.5 µs 357.4 µs 376.9 µs -@evan/opus (wasm) 469.27 µs/iter (440.1 µs … 1.3 ms) 447 µs 964.1 µs 1.03 ms -opusscript 249.68 µs/iter (235.3 µs … 692.1 µs) 240.7 µs 499.5 µs 551.3 µs -opusscript (no wasm) 4.01 ms/iter (3.84 ms … 4.69 ms) 4.1 ms 4.55 ms 4.69 ms +mediaplex 522.67 µs/iter (506.8 µs … 816.2 µs) 521.5 µs 645.6 µs 726.6 µs +@discordjs/opus 679.61 µs/iter (671.3 µs … 799.9 µs) 677.8 µs 750.2 µs 770.9 µs +opusscript 306.86 µs/iter (281.9 µs … 894.8 µs) 299.3 µs 619.8 µs 695.5 µs +opusscript (no wasm) 4.36 ms/iter (4.09 ms … 6.08 ms) 4.49 ms 4.94 ms 6.08 ms +@evan/opus 494.32 µs/iter (479.6 µs … 754.2 µs) 495.2 µs 632.9 µs 694.2 µs +@evan/opus (wasm) 850.82 µs/iter (800.7 µs … 1.93 ms) 841.3 µs 1.46 ms 1.61 ms +@evan/wasm 869.24 µs/iter (821.7 µs … 1.61 ms) 852.4 µs 1.39 ms 1.48 ms summary for OpusEncoder opusscript - 1.13x faster than mediaplex - 1.22x faster than @evan/opus - 1.26x faster than @discordjs/opus - 1.88x faster than @evan/opus (wasm) - 16.06x faster than opusscript (no wasm) - + 1.61x faster than @evan/opus + 1.7x faster than mediaplex + 2.21x faster than @discordjs/opus + 2.77x faster than @evan/opus (wasm) + 2.83x faster than @evan/wasm + 14.22x faster than opusscript (no wasm) cpu: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz runtime: node v20.6.1 (x64-win32) @@ -120,18 +121,20 @@ benchmark time (avg) (min … max) p75 p ------------------------------------------------------------ ----------------------------- • OpusDecoder ------------------------------------------------------------ ----------------------------- -mediaplex 92.94 µs/iter (84.8 µs … 1.22 ms) 90.8 µs 143.5 µs 161.8 µs -@discordjs/opus 87.75 µs/iter (81 µs … 376.1 µs) 86.3 µs 130.2 µs 142 µs -@evan/opus 91.22 µs/iter (84.7 µs … 320.2 µs) 89.9 µs 133.4 µs 142 µs -@evan/opus (wasm) 146.92 µs/iter (130.3 µs … 966.5 µs) 137.7 µs 298.7 µs 309.3 µs -opusscript 169.56 µs/iter (159.5 µs … 448.9 µs) 167.2 µs 232.6 µs 258.6 µs -opusscript (no wasm) 482.66 µs/iter (443.7 µs … 1.56 ms) 456.5 µs 965.8 µs 995.3 µs +mediaplex 57.54 µs/iter (49.6 µs … 944.4 µs) 55.3 µs 103.4 µs 116.5 µs +@discordjs/opus 49.85 µs/iter (45.7 µs … 483.6 µs) 48.5 µs 134.7 µs 140.5 µs +opusscript 82.25 µs/iter (75 µs … 540.6 µs) 81.1 µs 143.4 µs 157 µs +opusscript (no wasm) 230.88 µs/iter (202.9 µs … 2.05 ms) 215.3 µs 532.3 µs 594.9 µs +@evan/opus 49.16 µs/iter (45.7 µs … 827.6 µs) 47.7 µs 132.6 µs 137 µs +@evan/opus (wasm) 71.71 µs/iter (64.5 µs … 426.9 µs) 67.6 µs 208.1 µs 230.4 µs +@evan/wasm 77.3 µs/iter (71.9 µs … 383.6 µs) 74.4 µs 162.4 µs 169.4 µs summary for OpusDecoder - @discordjs/opus - 1.04x faster than @evan/opus - 1.06x faster than mediaplex - 1.67x faster than @evan/opus (wasm) - 1.93x faster than opusscript - 5.5x faster than opusscript (no wasm) + @evan/opus + 1.01x faster than @discordjs/opus + 1.17x faster than mediaplex + 1.46x faster than @evan/opus (wasm) + 1.57x faster than @evan/wasm + 1.67x faster than opusscript + 4.7x faster than opusscript (no wasm) ``` diff --git a/benchmark/common.mjs b/benchmark/common.mjs index 6a7eb0b..2dc0ca9 100644 --- a/benchmark/common.mjs +++ b/benchmark/common.mjs @@ -1,57 +1,17 @@ -import { Buffer } from 'node:buffer'; +import { readFileSync } from 'node:fs'; import djs from '@discordjs/opus'; import opusscript from 'opusscript'; import mediaplex from '../index.js'; import * as evanOpus from '@evan/opus'; import * as evanOpusWasm from '@evan/opus/wasm/index.mjs'; +import * as simdEvanOpus from '@evan/wasm/target/opus/node.mjs'; export const generatePCMSample = (sampleSize) => { - const buffer = Buffer.alloc(sampleSize); - - for (let i = 0; i < sampleSize; i++) { - const value = Math.sin((i / sampleSize) * Math.PI * 2); - const sample = Math.min(Math.max(value * 32767, -32767), 32767); - buffer.writeInt16LE(sample, Math.min(Math.max(i, sampleSize), 0)); - } - - return buffer; + return readFileSync(new URL(`./data/sample.pcm`, import.meta.url)).subarray(0, sampleSize); } export const generateOpusSample = () => { - // prettier-ignore - const data = [ - 255, 131, 247, 2, 127, 247, - 14, 75, 17, 42, 243, 209, 79, - 36, 18, 247, 69, 197, 151, 106, - 158, 244, 89, 114, 20, 51, 234, - 168, 72, 205, 142, 234, 98, 129, - 115, 134, 104, 154, 84, 247, 245, - 169, 207, 29, 171, 115, 101, 8, - 123, 167, 19, 128, 62, 162, 181, - 221, 181, 71, 177, 244, 60, 255, - 53, 235, 64, 141, 84, 222, 59, 206, - 172, 10, 111, 67, 106, 38, 239, 251, - 15, 31, 161, 125, 124, 67, 191, 44, - 174, 145, 98, 108, 152, 225, 111, 94, - 165, 240, 35, 188, 216, 0, 160, 42, 34, - 162, 8, 118, 251, 118, 129, 78, 218, 209, - 14, 125, 17, 14, 125, 9, 45, 74, 93, 142, - 43, 222, 158, 214, 92, 225, 98, 230, 117, - 25, 103, 218, 226, 27, 179, 118, 221, 162, - 232, 23, 8, 61, 224, 33, 123, 107, 189, 59, - 192, 246, 193, 36, 231, 64, 145, 171, 76, 58, - 102, 126, 52, 65, 145, 215, 114, 153, 195, 7, - 83, 135, 208, 173, 137, 76, 64, 80, 133, 102, - 58, 57, 97, 57, 13, 87, 40, 40, 213, 220, 225, - 246, 218, 73, 85, 137, 181, 189, 208, 226, 131, - 123, 120, 102, 5, 31, 85, 192, 59, 253, 173, 11, - 68, 207, 108, 3, 55, 15, 25, 203, 123, 107, 171, - 38, 231, 183, 184, 246, 129, 169, 163, 105, 30, - 230, 196, 255, 96, 144, 112, 128, 7, 8, 16, 64, - 18, 16, 65, 1, 49, 20, 19, 255, 254, 255, 254 - ]; - - return Buffer.from(data); + return readFileSync(new URL(`./data/sample.opus`, import.meta.url)); } export const createMediaplexEncoder = (config) => new mediaplex.OpusEncoder(config.SAMPLE_RATE, config.CHANNELS); @@ -79,4 +39,13 @@ export const createEvanOpusEncoderWasm = (config) => new evanOpusWasm.Encoder({ export const createEvanOpusDecoderWasm = (config) => new evanOpusWasm.Decoder({ channels: config.CHANNELS, sample_rate: config.SAMPLE_RATE +}); +export const createSimdEvanOpusEncoder = (config) => new simdEvanOpus.Encoder({ + application: 'voip', + channels: config.CHANNELS, + sample_rate: config.SAMPLE_RATE +}); +export const createSimdEvanOpusDecoder = (config) => new simdEvanOpus.Decoder({ + channels: config.CHANNELS, + sample_rate: config.SAMPLE_RATE }); \ No newline at end of file diff --git a/benchmark/data/sample.opus b/benchmark/data/sample.opus new file mode 100644 index 0000000..80eaebd Binary files /dev/null and b/benchmark/data/sample.opus differ diff --git a/benchmark/data/sample.pcm b/benchmark/data/sample.pcm new file mode 100644 index 0000000..d3bacae Binary files /dev/null and b/benchmark/data/sample.pcm differ diff --git a/benchmark/decoder.mjs b/benchmark/decoder.mjs index 661d59a..9c0d133 100644 --- a/benchmark/decoder.mjs +++ b/benchmark/decoder.mjs @@ -8,7 +8,8 @@ import { createOpusScriptWasmEncoder, generateOpusSample, createEvanOpusDecoder, - createEvanOpusDecoderWasm + createEvanOpusDecoderWasm, + createSimdEvanOpusDecoder } from './common.mjs'; const config = { @@ -23,6 +24,7 @@ const wasmEncoder = createOpusScriptWasmEncoder(config); const asmEncoder = createOpusScriptAsmEncoder(config); const evanOpus = createEvanOpusDecoder(config); const evanOpusWasm = createEvanOpusDecoderWasm(config); +const evanWasmOpus = createSimdEvanOpusDecoder(config); const SAMPLE = generateOpusSample(); @@ -33,18 +35,21 @@ mitata.group('OpusDecoder', () => { mitata.bench('@discordjs/opus', () => { nativeEncoder.decode(SAMPLE); }); - mitata.bench('@evan/opus', () => { - evanOpus.decode(SAMPLE); - }); - mitata.bench('@evan/opus (wasm)', () => { - evanOpusWasm.decode(SAMPLE); - }); mitata.bench('opusscript', () => { wasmEncoder.decode(SAMPLE); }); mitata.bench('opusscript (no wasm)', () => { asmEncoder.decode(SAMPLE); }); + mitata.bench('@evan/opus', () => { + evanOpus.decode(SAMPLE); + }); + mitata.bench('@evan/opus (wasm)', () => { + evanOpusWasm.decode(SAMPLE); + }); + mitata.bench('@evan/wasm', () => { + evanWasmOpus.decode(SAMPLE); + }); }); await mitata.run(); \ No newline at end of file diff --git a/benchmark/encoder.mjs b/benchmark/encoder.mjs index ff90ef2..f8cb22c 100644 --- a/benchmark/encoder.mjs +++ b/benchmark/encoder.mjs @@ -7,7 +7,8 @@ import { createOpusScriptWasmEncoder, generatePCMSample, createEvanOpusEncoder, - createEvanOpusEncoderWasm + createEvanOpusEncoderWasm, + createSimdEvanOpusEncoder } from './common.mjs'; const config = { @@ -22,6 +23,7 @@ const wasmEncoder = createOpusScriptWasmEncoder(config); const asmEncoder = createOpusScriptAsmEncoder(config); const evanOpus = createEvanOpusEncoder(config); const evanOpusWasm = createEvanOpusEncoderWasm(config); +const evanWasmOpus = createSimdEvanOpusEncoder(config); const SAMPLE = generatePCMSample(config.FRAME_SIZE * config.CHANNELS * 6); @@ -32,18 +34,21 @@ mitata.group('OpusEncoder', () => { mitata.bench('@discordjs/opus', () => { nativeEncoder.encode(SAMPLE); }); - mitata.bench('@evan/opus', () => { - evanOpus.encode(SAMPLE); - }); - mitata.bench('@evan/opus (wasm)', () => { - evanOpusWasm.encode(SAMPLE); - }); mitata.bench('opusscript', () => { wasmEncoder.encode(SAMPLE, config.FRAME_SIZE); }); mitata.bench('opusscript (no wasm)', () => { asmEncoder.encode(SAMPLE, config.FRAME_SIZE); }); + mitata.bench('@evan/opus', () => { + evanOpus.encode(SAMPLE); + }); + mitata.bench('@evan/opus (wasm)', () => { + evanOpusWasm.encode(SAMPLE); + }); + mitata.bench('@evan/wasm', () => { + evanWasmOpus.encode(SAMPLE); + }); }); await mitata.run(); \ No newline at end of file diff --git a/package.json b/package.json index d03e282..7649b80 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "devDependencies": { "@discordjs/opus": "^0.9.0", "@evan/opus": "^1.0.2", + "@evan/wasm": "^0.0.95", "@napi-rs/cli": "^2.16.3", "@types/node": "^20.6.0", "ava": "5.3.1", diff --git a/yarn.lock b/yarn.lock index 786c7df..5dee41d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -41,6 +41,13 @@ __metadata: languageName: node linkType: hard +"@evan/wasm@npm:^0.0.95": + version: 0.0.95 + resolution: "@evan/wasm@npm:0.0.95" + checksum: 64f7eb1ed47b6fd6fbf6ca93878c92e95de8f29f318cd7f2e0afc3d9cf7222f98538769d6b04854c6089b1622b975710462e67c1f909831410beaf11cab388ef + languageName: node + linkType: hard + "@isaacs/cliui@npm:^8.0.2": version: 8.0.2 resolution: "@isaacs/cliui@npm:8.0.2" @@ -1327,6 +1334,7 @@ __metadata: dependencies: "@discordjs/opus": ^0.9.0 "@evan/opus": ^1.0.2 + "@evan/wasm": ^0.0.95 "@napi-rs/cli": ^2.16.3 "@types/node": ^20.6.0 ava: 5.3.1