Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 46 additions & 9 deletions src/common/performancemonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ PerformanceMonitor::PerformanceMonitor()
qCDebug(appLog) << "PerformanceMonitor instance created";
}

// 注意:各打点方法只读一次时钟(赋值与日志格式化复用同一时刻),
// 保证行为与断言不随日志开关(QT_LOGGING_RULES)变化
void PerformanceMonitor::initializeAppStart()
{
QDateTime current = QDateTime::currentDateTime();
qCDebug(appLog) << qPrintable(LOG_FLAG)
<< QDateTime::currentDateTime().toString(Qt::ISODateWithMs)
<< current.toString(Qt::ISODateWithMs)
<< "start to initialize app";
initializeAppStartMs = current.toMSecsSinceEpoch();
qCDebug(appLog) << "Application start timestamp recorded:" << initializeAppStartMs;
Expand All @@ -38,12 +40,12 @@ void PerformanceMonitor::initializeAppFinish()
{
QDateTime current = QDateTime::currentDateTime();
qCDebug(appLog) << qPrintable(LOG_FLAG)
<< QDateTime::currentDateTime().toString(Qt::ISODateWithMs)
<< current.toString(Qt::ISODateWithMs)
<< " finish to initialize app";

inittalizeApoFinishMs = current.toMSecsSinceEpoch();
qint64 time = inittalizeApoFinishMs - initializeAppStartMs;
qCInfo(appLog) << qPrintable(QString("%1 startduration=%2ms #(Init app time)").arg(GRAB_POINT_INIT_APP_TIME).arg(time));
qCInfo(appLog) << qPrintable(formatInitDuration(time));
}

void PerformanceMonitor::closeAppStart()
Expand All @@ -57,19 +59,19 @@ void PerformanceMonitor::closeAppFinish()
{
QDateTime current = QDateTime::currentDateTime();
qCDebug(appLog) << qPrintable(LOG_FLAG)
<< QDateTime::currentDateTime().toString(Qt::ISODateWithMs)
<< current.toString(Qt::ISODateWithMs)
<< " finish to close app";

closeAppFinishMs = current.toMSecsSinceEpoch();
qint64 time = closeAppFinishMs - closeAppStartMs;
qCInfo(appLog) << qPrintable(QString("%1 closeduration=%2ms #(Close app time)").arg(GRAB_POINT_CLOSE_APP_TIME).arg(time));
qCInfo(appLog) << qPrintable(formatCloseDuration(time));
}

void PerformanceMonitor::openFileStart()
{
QDateTime current = QDateTime::currentDateTime();
qCDebug(appLog) << qPrintable(LOG_FLAG)
<< QDateTime::currentDateTime().toString(Qt::ISODateWithMs)
<< current.toString(Qt::ISODateWithMs)
<< "start to open file";
openFileStartMs = current.toMSecsSinceEpoch();
}
Expand All @@ -78,11 +80,46 @@ void PerformanceMonitor::openFileFinish(const QString &strFileName, qint64 iFile
{
QDateTime current = QDateTime::currentDateTime();
qCDebug(appLog) << qPrintable(LOG_FLAG)
<< QDateTime::currentDateTime().toString(Qt::ISODateWithMs)
<< current.toString(Qt::ISODateWithMs)
<< " finish to open file";

openFileFinishMs = current.toMSecsSinceEpoch();
qint64 time = openFileFinishMs - openFileStartMs;
float fFilesize = iFileSize;
qCInfo(appLog) << qPrintable(QString("%1 filename=%2 filesize=%3M opentime=%4ms #(Open file time)").arg(GRAB_POINT_OPEN_FILE_TIME).arg(strFileName).arg(QString::number(fFilesize/(1024*1024), 'f', 6)).arg(time));
qCInfo(appLog) << qPrintable(formatOpenFileInfo(strFileName, iFileSize, time));
}

PerformanceMonitorSnapshot PerformanceMonitor::snapshot()
{
PerformanceMonitorSnapshot snap;
snap.initializeAppStartMs = initializeAppStartMs;
snap.initializeAppFinishMs = inittalizeApoFinishMs;
snap.closeAppStartMs = closeAppStartMs;
snap.closeAppFinishMs = closeAppFinishMs;
snap.openFileStartMs = openFileStartMs;
snap.openFileFinishMs = openFileFinishMs;
return snap;
}

QString PerformanceMonitor::formatInitDuration(qint64 durationMs)
{
return QStringLiteral("%1 startduration=%2ms #(Init app time)")
.arg(GRAB_POINT_INIT_APP_TIME)
.arg(durationMs);
}

QString PerformanceMonitor::formatCloseDuration(qint64 durationMs)
{
return QStringLiteral("%1 closeduration=%2ms #(Close app time)")
.arg(GRAB_POINT_CLOSE_APP_TIME)
.arg(durationMs);
}

QString PerformanceMonitor::formatOpenFileInfo(const QString &fileName, qint64 fileSizeBytes, qint64 openTimeMs)
{
const float fFilesize = fileSizeBytes;
return QStringLiteral("%1 filename=%2 filesize=%3M opentime=%4ms #(Open file time)")
.arg(GRAB_POINT_OPEN_FILE_TIME)
.arg(fileName)
.arg(QString::number(fFilesize / (1024 * 1024), 'f', 6))
.arg(openTimeMs);
}
20 changes: 20 additions & 0 deletions src/common/performancemonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@


#include <QTime>
#include <QDateTime>

Check warning on line 10 in src/common/performancemonitor.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDateTime> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDebug>

Check warning on line 11 in src/common/performancemonitor.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDebug> not found. Please note: Cppcheck does not need standard library headers to get proper results.

// 各打点最近一次时间戳的只读快照,仅供观测与 UT 断言使用(随每次打点覆盖)
struct PerformanceMonitorSnapshot
{
qint64 initializeAppStartMs = 0;
qint64 initializeAppFinishMs = 0;
qint64 closeAppStartMs = 0;
qint64 closeAppFinishMs = 0;
qint64 openFileStartMs = 0;
qint64 openFileFinishMs = 0;
};

class PerformanceMonitor
{

Expand All @@ -23,6 +34,15 @@
static void openFileStart();
static void openFileFinish(const QString &strFileName, qint64 iFileSize);

// 最近一次各打点的时间戳快照(不读时钟、不输出日志)
static PerformanceMonitorSnapshot snapshot();

// GRABPOINT 消息构造(纯函数,不输出日志):打点格式回归与 UT 断言共用,
// 避免用例依赖运行期 debug/info 日志开关
static QString formatInitDuration(qint64 durationMs);
static QString formatCloseDuration(qint64 durationMs);
static QString formatOpenFileInfo(const QString &fileName, qint64 fileSizeBytes, qint64 openTimeMs);

private:
Q_DISABLE_COPY(PerformanceMonitor)

Expand Down
119 changes: 49 additions & 70 deletions tests/common2/test_performancemonitor.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

/*
Expand All @@ -8,31 +9,31 @@
* B1 : initializeAppStart/initializeAppFinish → 记录时间差并输出 POINT-01 startduration
* B2 : closeAppStart/closeAppFinish → POINT-02 closeduration
* B3 : openFileStart/openFileFinish(file,size)→ POINT-04 filename/filesize/opentime
* B4 : 构造函数 → 仅 qDebug
* B4 : 构造函数 → 无时钟读取,仅调试日志
*
* 用例映射:
* - InitializeApp_StartThenFinish_LogsPoint01Duration → B1
* - CloseApp_StartThenFinish_LogsPoint02Duration → B2
* - OpenFile_StartThenFinish_LogsPoint04Info → B3
* - Constructor_NewInstance_LogsCreated → B4
* - InitializeApp_StartThenFinish_RecordsPoint01Timestamps → B1
* - CloseApp_StartThenFinish_RecordsPoint02Timestamps → B2
* - OpenFile_StartThenFinish_BuildsPoint04Info → B3
* - Constructor_NewInstance_NoClockAccess → B4
*
* 隔离:QDateTime::currentDateTime 全部 stub 为固定队列时间(确定性时间差),
* qInfo/qDebug 经 qInstallMessageHandler 捕获后断言(TearDown 恢复默认 handler);
* 夹具内用 setFilterRules 显式放开 appLog 的 debug/info,避免运行脚本的
* QT_LOGGING_RULES 全局关日志导致断言失败(TearDown 恢复环境原规则)。
* 隔离(不依赖 debug/info 日志输出,见 task-1b84a8cf / task-7d4e671d):
* - QDateTime::currentDateTime 全部 stub 为固定队列时间;被测方法每个只读一次时钟,
* timeCalls 与 snapshot() 时间戳断言在任何 QT_LOGGING_RULES 配置下均确定成立
* (运行脚本会全局关闭 debug/info,经 qInstallMessageHandler 捕获日志文本断言
* 会产生大面积假失败,已废弃该方式);
* - GRABPOINT 消息格式经纯函数 formatXxx() 直接断言,与日志是否输出解耦。
*/

#include <gtest/gtest.h>

Check warning on line 28 in tests/common2/test_performancemonitor.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <gtest/gtest.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include "stubext.h"

#include "performancemonitor.h"

#include <QCoreApplication>

Check warning on line 33 in tests/common2/test_performancemonitor.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QCoreApplication> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDateTime>

Check warning on line 34 in tests/common2/test_performancemonitor.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDateTime> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDebug>
#include <QLoggingCategory>
#include <QStringList>

Check warning on line 35 in tests/common2/test_performancemonitor.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QStringList> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QVector>

Check warning on line 36 in tests/common2/test_performancemonitor.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QVector> not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace {

Expand All @@ -50,36 +51,15 @@

} // namespace

// 消息捕获:静态指针指向当前用例的捕获缓冲(TearDown 置空,避免跨用例污染)
static QStringList *g_capturedLogs = nullptr;

static void utCaptureHandler(QtMsgType type, const QMessageLogContext &, const QString &msg)
{
if (g_capturedLogs)
g_capturedLogs->append(msg);
// 保持默认行为需要的最小输出抑制:不再回显
Q_UNUSED(type);
}

class PerformanceMonitorTest : public ::testing::Test
{
protected:
void SetUp() override
{
ensureApp();
// 运行脚本会导出 QT_LOGGING_RULES="*.debug=false;*.info=false" 压日志量,
// 该规则会让 qCDebug/qCInfo(appLog) 整体短路:消息到不了捕获 handler,
// 流表达式里的 currentDateTime() 副作用也随之消失(timeCalls 少计)。
// setFilterRules 优先级高于环境变量,夹具内显式放开 appLog,保证用例
// 不随运行环境的日志配置漂移;TearDown 恢复原规则。
QLoggingCategory::setFilterRules(QStringLiteral(
"org.deepin.editor.debug=true\norg.deepin.editor.info=true"));
stub.clear();
logs.clear();
timeQueue.clear();
timeCalls = 0;
g_capturedLogs = &logs;
qInstallMessageHandler(utCaptureHandler);
timeQueue.clear();
stub.set_lamda(static_cast<QDateTime (*)()>(&QDateTime::currentDateTime),
[this]() -> QDateTime {
++timeCalls;
Expand All @@ -90,67 +70,62 @@

void TearDown() override
{
// 先恢复运行环境自带的日志规则(无则清空回到进程默认),再恢复默认
// handler,避免中间窗口 debug/info 直接打到 stderr
QLoggingCategory::setFilterRules(QString::fromLatin1(qgetenv("QT_LOGGING_RULES")));
qInstallMessageHandler(nullptr); // 恢复默认 handler
g_capturedLogs = nullptr;
stub.clear();
}

// 便捷:压入两次相同时间(每个方法内部会取 currentDateTime 两次:赋值 + 日志格式化)
// 压入一次固定时间:被测方法每个打点只读一次时钟(赋值与日志格式化复用同一时刻)
void pushTime(qint64 ms)
{
timeQueue.append(ms);
timeQueue.append(ms);
}

bool logsContain(const QString &sub) const
{
for (const QString &l : logs)
if (l.contains(sub, Qt::CaseSensitive))
return true;
return false;
}

stub_ext::StubExt stub;
QStringList logs;
QVector<qint64> timeQueue;
int timeCalls = 0;
};

// B1
TEST_F(PerformanceMonitorTest, InitializeApp_StartThenFinish_LogsPoint01Duration)
TEST_F(PerformanceMonitorTest, InitializeApp_StartThenFinish_RecordsPoint01Timestamps)
{
// Arrange: start=1000ms, finish=2000ms → duration 1000ms
pushTime(1000);
pushTime(2000);
// Act
PerformanceMonitor::initializeAppStart();
PerformanceMonitor::initializeAppFinish();
// Assert
EXPECT_TRUE(logsContain(QStringLiteral("[GRABPOINT] POINT-01 startduration=1000ms")));
EXPECT_TRUE(logsContain(QStringLiteral("start to initialize app")));
EXPECT_EQ(timeCalls, 4);
// Assert: 时间戳落点与时间差(与日志配置无关)
const PerformanceMonitorSnapshot snap = PerformanceMonitor::snapshot();
EXPECT_EQ(timeCalls, 2);
EXPECT_EQ(snap.initializeAppStartMs, 1000);
EXPECT_EQ(snap.initializeAppFinishMs, 2000);
const qint64 duration = snap.initializeAppFinishMs - snap.initializeAppStartMs;
EXPECT_EQ(duration, 1000);
EXPECT_EQ(PerformanceMonitor::formatInitDuration(duration),
QStringLiteral("[GRABPOINT] POINT-01 startduration=1000ms #(Init app time)"));
}

// B2
TEST_F(PerformanceMonitorTest, CloseApp_StartThenFinish_LogsPoint02Duration)
TEST_F(PerformanceMonitorTest, CloseApp_StartThenFinish_RecordsPoint02Timestamps)
{
// Arrange: closeAppStart 仅取一次当前时间;closeAppFinish 取两次(赋值+日志格式化)
timeQueue.append(3000);
// Arrange: closeAppStart/closeAppFinish 各取一次当前时间 → duration 1500ms
pushTime(3000);
pushTime(4500);
// Act
PerformanceMonitor::closeAppStart();
PerformanceMonitor::closeAppFinish();
// Assert
EXPECT_TRUE(logsContain(QStringLiteral("[GRABPOINT] POINT-02 closeduration=1500ms")));
EXPECT_TRUE(logsContain(QStringLiteral("finish to close app")));
EXPECT_EQ(timeCalls, 3);
const PerformanceMonitorSnapshot snap = PerformanceMonitor::snapshot();
EXPECT_EQ(timeCalls, 2);
EXPECT_EQ(snap.closeAppStartMs, 3000);
EXPECT_EQ(snap.closeAppFinishMs, 4500);
const qint64 duration = snap.closeAppFinishMs - snap.closeAppStartMs;
EXPECT_EQ(duration, 1500);
EXPECT_EQ(PerformanceMonitor::formatCloseDuration(duration),
QStringLiteral("[GRABPOINT] POINT-02 closeduration=1500ms #(Close app time)"));
}

// B3
TEST_F(PerformanceMonitorTest, OpenFile_StartThenFinish_LogsPoint04Info)
TEST_F(PerformanceMonitorTest, OpenFile_StartThenFinish_BuildsPoint04Info)
{
// Arrange: 2MB 文件,耗时 500ms
pushTime(100000);
Expand All @@ -159,21 +134,25 @@
PerformanceMonitor::openFileStart();
PerformanceMonitor::openFileFinish(QStringLiteral("test-doc.txt"), 2LL * 1024 * 1024);
// Assert
EXPECT_TRUE(logsContain(QStringLiteral("[GRABPOINT] POINT-04")));
EXPECT_TRUE(logsContain(QStringLiteral("filename=test-doc.txt")));
EXPECT_TRUE(logsContain(QStringLiteral("filesize=2.000000M")));
EXPECT_TRUE(logsContain(QStringLiteral("opentime=500ms")));
EXPECT_EQ(timeCalls, 4);
const PerformanceMonitorSnapshot snap = PerformanceMonitor::snapshot();
EXPECT_EQ(timeCalls, 2);
EXPECT_EQ(snap.openFileStartMs, 100000);
EXPECT_EQ(snap.openFileFinishMs, 100500);
const qint64 duration = snap.openFileFinishMs - snap.openFileStartMs;
EXPECT_EQ(duration, 500);
EXPECT_EQ(PerformanceMonitor::formatOpenFileInfo(QStringLiteral("test-doc.txt"),
2LL * 1024 * 1024, duration),
QStringLiteral("[GRABPOINT] POINT-04 filename=test-doc.txt "
"filesize=2.000000M opentime=500ms #(Open file time)"));
}

// B4
TEST_F(PerformanceMonitorTest, Constructor_NewInstance_LogsCreated)
TEST_F(PerformanceMonitorTest, Constructor_NewInstance_NoClockAccess)
{
// Arrange/Act
// Arrange/Act: 构造与析构不读时钟、不依赖任何外部资源
{
PerformanceMonitor monitor;
}
// Assert: 构造与析构不依赖任何外部资源,仅日志
EXPECT_TRUE(logsContain(QStringLiteral("PerformanceMonitor instance created")));
// Assert
EXPECT_EQ(timeCalls, 0);
}
Loading