104 lines
4.9 KiB
TypeScript
104 lines
4.9 KiB
TypeScript
// v1.9.0: 게임 중 GPU 아끼기 (발로란트 + 브라우저 + 겜녹이를 같이 켜면 GPU 가 100% 가까이 차던 것)
|
|
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { EventEmitter } from 'node:events';
|
|
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { Store } from '../src/core/db/db.ts';
|
|
import { Timeline } from '../src/core/timeline.ts';
|
|
import { Bus } from '../src/core/util/events.ts';
|
|
import { SttPipeline } from '../src/core/stt/pipeline.ts';
|
|
|
|
function setup(gameSaver = true) {
|
|
const dir = fs.mkdtempSync(path.join(process.env.TMPDIR || os.tmpdir(), 'gvr-saver-'));
|
|
const store = new Store(path.join(dir, 'db.sqlite'));
|
|
store.upsertUser({ id: 'u1', name: '하늘', avatar: null });
|
|
const t0 = Date.now() - 600000;
|
|
store.upsertMatch({ id: 'm1', game: 'valorant', started_at: t0, status: 'live' });
|
|
const mk = (id: string, at = Date.now()) => {
|
|
const f = path.join(dir, 'audio', `${id}.ogg`);
|
|
fs.mkdirSync(path.dirname(f), { recursive: true });
|
|
fs.writeFileSync(f, Buffer.concat([Buffer.from('OggS'), Buffer.alloc(50)]));
|
|
store.insertUtterance({ id, session_id: null, user_id: 'u1', started_at: at, ended_at: at + 1000, audio_path: f, match_id: 'm1' });
|
|
};
|
|
const reqs: any[] = [];
|
|
const ops: string[] = [];
|
|
const worker: any = Object.assign(new EventEmitter(), {
|
|
ready: true,
|
|
request: async (op: string, req: any) => {
|
|
if (op === 'gpu_info') return { gpus: [{ vram_total_mb: 16303 }] }; // 두 모델을 같이 올릴 수 있는 카드
|
|
if (op === 'models') return { models: [{ name: 'rt', installed: true }, { name: 'pr', installed: true }] };
|
|
if (op === 'load' || op === 'unload') {
|
|
ops.push(`${op}:${req.slot}`);
|
|
return {};
|
|
}
|
|
reqs.push(req);
|
|
await new Promise((r) => setTimeout(r, 40));
|
|
return { text: '말', raw_text: '말', words: [], noise: false, noise_reason: null, confidence: 0.5, lufs: -30, gain_db: 0, speech_ratio: 1, duration_s: 1, model: req.slot, elapsed_ms: 40 };
|
|
},
|
|
});
|
|
const settings: any = () => ({
|
|
stt: { realtimeModel: 'rt', preciseModel: 'pr', device: 'cuda', computeType: 'float16', gameVramReserveMb: 3072, reprocessAfterMatch: true, gameSaver },
|
|
audio: { targetLufs: -18, noiseSensitivity: 0.5, denoise: false },
|
|
});
|
|
const dict: any = { hotwords: () => '', corrections: () => [], candidates: () => [] };
|
|
const stt = new SttPipeline(worker, store, dict, new Timeline(store), new Bus(), settings);
|
|
stt.status.worker = 'ready';
|
|
const wait = async (fn: () => boolean, ms = 5000) => {
|
|
const t = Date.now();
|
|
while (!fn()) {
|
|
if (Date.now() - t > ms) throw new Error('timeout');
|
|
await new Promise((r) => setTimeout(r, 10));
|
|
}
|
|
};
|
|
return { store, stt, mk, reqs, ops, wait, t0 };
|
|
}
|
|
|
|
test('게임 중: 정밀 모델을 내리고, 새 발화는 실시간 모델 · 빔 1 로만, 발화 사이에 쉰다', async () => {
|
|
const s = setup();
|
|
await s.stt.prepareModels();
|
|
assert.equal(s.stt.status.slots.precise.state, 'loaded', '게임 밖에서는 두 모델 모두');
|
|
await s.stt.setGameActive(true);
|
|
assert.equal(s.stt.status.slots.precise.state, 'empty', '게임이 켜지면 정밀 모델을 내린다');
|
|
s.mk('a1');
|
|
s.mk('a2');
|
|
s.stt.enqueue('a1', 'realtime');
|
|
s.stt.enqueue('a2', 'realtime');
|
|
const t = Date.now();
|
|
await s.wait(() => s.reqs.length === 2 && !s.stt.status.processing);
|
|
assert.ok(Date.now() - t >= 75, '두 번째 발화 전에 앞 변환만큼 쉰다');
|
|
assert.deepEqual(s.reqs.map((r) => [r.slot, r.beam_size]), [['realtime', 1], ['realtime', 1]]);
|
|
});
|
|
|
|
test('경기가 끝나도 게임(메뉴)이 켜져 있으면 정밀 변환은 기다리고, 게임을 끄면 정밀 모델을 올려 한다', async () => {
|
|
const s = setup();
|
|
await s.stt.prepareModels();
|
|
await s.stt.setGameActive(true);
|
|
await s.stt.onMatchStart();
|
|
s.mk('b1', s.t0 + 1000);
|
|
s.stt.enqueue('b1', 'realtime');
|
|
await s.wait(() => s.store.getUtterance('b1')!.status === 'realtime');
|
|
await s.stt.onMatchEnd(s.t0, Date.now());
|
|
await new Promise((r) => setTimeout(r, 100));
|
|
assert.equal(s.reqs.length, 1, '게임이 켜져 있는 동안 정밀 변환 안 함');
|
|
assert.equal(s.stt.status.queue, 1);
|
|
assert.equal(s.stt.status.slots.precise.state, 'empty');
|
|
await s.stt.setGameActive(false);
|
|
await s.wait(() => s.store.getUtterance('b1')!.status === 'precise');
|
|
assert.equal(s.reqs[1].slot, 'precise');
|
|
assert.equal(s.reqs[1].beam_size, undefined, '게임 밖에서는 기본 빔');
|
|
});
|
|
|
|
test('게임 중 GPU 아끼기를 끄면 예전처럼 (정밀 모델 유지 · 기본 빔)', async () => {
|
|
const s = setup(false);
|
|
await s.stt.prepareModels();
|
|
await s.stt.setGameActive(true);
|
|
assert.equal(s.stt.status.slots.precise.state, 'loaded');
|
|
await s.stt.onMatchStart();
|
|
s.mk('c1');
|
|
s.stt.enqueue('c1', 'realtime');
|
|
await s.wait(() => s.reqs.length === 1);
|
|
assert.equal(s.reqs[0].beam_size, undefined);
|
|
});
|