chore: update benchmark with real sample

This commit is contained in:
skdhg committed 2023-09-17 12:41:01 +05:45
1 parent e34aedde25
commit aadd415d6a
8 files changed
+73 -82

No files matched your search

+13 -44
View File
@@ -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
});
Binary file not shown.
Binary file not shown.
+12 -7
View File
@@ -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();
+12 -7
View File
@@ -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();