feat: 统计按键触发到正在录音的延迟并在日志中打印

每次长按进入 Recording 状态时打印本次延迟,并维护累计统计:
  ⏱ 按键→录音延迟: 123ms (平均: 120ms, 最小: 98ms, 最大: 155ms, 累计: 5次)

使用 QElapsedTimer 精确计时。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
impressionyang 2026-06-11 16:39:59 +08:00
parent ff7318efd6
commit 2152dcb296
2 changed files with 32 additions and 0 deletions

View File

@ -19,6 +19,8 @@
#include <QThread>
#include <QTimer>
#include <QtConcurrent>
#include <QElapsedTimer>
#include <algorithm>
static const char* const kTag = "VoiceInputService";
@ -48,6 +50,22 @@ VoiceInputService::VoiceInputService(ConfigManager* configManager,
state_ = Recording;
audioBuffer_.clear(); // 清除预录音期间的静音
emit statusChanged("正在录音...");
// 统计按键到录音延迟
if (latencyTracking_ && hotkeyLatencyTimer_.isValid()) {
qint64 latencyMs = hotkeyLatencyTimer_.elapsed();
totalKeyCount_++;
totalLatencyMs_ += latencyMs;
maxLatencyMs_ = std::max(maxLatencyMs_, (double)latencyMs);
minLatencyMs_ = std::min(minLatencyMs_, (double)latencyMs);
double avgMs = totalLatencyMs_ / totalKeyCount_;
LOG_INFO(kTag, QString("⏱ 按键→录音延迟: %1ms (平均: %2ms, 最小: %3ms, 最大: %4ms, 累计: %5次)")
.arg(latencyMs).arg(avgMs, 0, 'f', 0)
.arg(minLatencyMs_, 0, 'f', 0).arg(maxLatencyMs_, 0, 'f', 0)
.arg(totalKeyCount_));
latencyTracking_ = false;
}
LOG_DEBUG(kTag, "PreRecording → Recording (灯保持 ON开始录音)");
}
});
@ -156,6 +174,10 @@ void VoiceInputService::onHotkeyActivated() {
recording_ = true;
audioBuffer_.clear();
// 启动延迟统计
hotkeyLatencyTimer_.start();
latencyTracking_ = true;
int deviceIndex = configManager_->get("audio.input_device").toInt();
int sampleRate = configManager_->get("stt.sample_rate").toInt();
int bufferSizeMs = configManager_->get("audio.buffer_size_ms").toInt();

View File

@ -3,6 +3,7 @@
#include <QObject>
#include <QString>
#include <QTimer>
#include <QElapsedTimer>
#include <vector>
#include <memory>
@ -80,6 +81,15 @@ private:
QTimer* longPressTimer_ = nullptr;
QTimer* cooldownTimer_ = nullptr;
// 按键到录音延迟统计
QElapsedTimer hotkeyLatencyTimer_;
bool latencyTracking_ = false;
int totalKeyCount_ = 0;
double totalLatencyMs_ = 0;
double maxLatencyMs_ = 0;
double minLatencyMs_ = 9999;
void startRecording();
void stopRecordingAndTranscribe();
void simulateCapsLock();