Skip to content

Commit cc5f694

Browse files
committed
#3366 note: add checksum benchmark
Signed-off-by: Patrizio Bekerle <[email protected]>
1 parent 1845f29 commit cc5f694

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/entities/note.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <QCryptographicHash>
1212
#include <QDebug>
1313
#include <QDir>
14+
#include <QElapsedTimer>
1415
#include <QMessageBox>
1516
#include <QMimeDatabase>
1617
#include <QRegularExpression>
@@ -2240,6 +2241,10 @@ void Note::createFromFile(QFile &file, int noteSubFolderId, bool withNoteNameHoo
22402241
#endif
22412242

22422243
this->_fileLastModified = fileInfo.lastModified();
2244+
2245+
// Calculate the checksum before storing
2246+
this->_fileChecksum = calculateChecksum(this->_noteText);
2247+
22432248
this->store();
22442249

22452250
if (withNoteNameHook) {
@@ -2972,12 +2977,50 @@ qint64 Note::qint64Hash(const QString &str) {
29722977
return a ^ b;
29732978
}
29742979

2980+
// Namespace-scoped variables for checksum timing statistics
2981+
namespace {
2982+
qint64 g_totalChecksumTime = 0;
2983+
int g_checksumCallCount = 0;
2984+
} // namespace
2985+
29752986
/**
29762987
* Calculates SHA256 checksum of text
29772988
*/
29782989
QString Note::calculateChecksum(const QString &text) {
2990+
QElapsedTimer timer;
2991+
timer.start();
2992+
29792993
const QByteArray hash = QCryptographicHash::hash(text.toUtf8(), QCryptographicHash::Sha256);
2980-
return QString::fromLatin1(hash.toHex());
2994+
const QString result = QString::fromLatin1(hash.toHex());
2995+
2996+
const qint64 elapsed = timer.nsecsElapsed();
2997+
g_totalChecksumTime += elapsed;
2998+
g_checksumCallCount++;
2999+
3000+
// qDebug() << "Note::calculateChecksum - Call #" << g_checksumCallCount
3001+
// << "took" << (elapsed / 1000.0) << "microseconds"
3002+
// << "| Total time:" << (g_totalChecksumTime / 1000000.0) << "ms"
3003+
// << "| Average:" << (g_totalChecksumTime / g_checksumCallCount / 1000.0) <<
3004+
// "microseconds";
3005+
3006+
return result;
3007+
}
3008+
3009+
/**
3010+
* Gets checksum calculation statistics
3011+
*/
3012+
void Note::getChecksumStats(qint64 &totalTime, int &callCount) {
3013+
totalTime = g_totalChecksumTime;
3014+
callCount = g_checksumCallCount;
3015+
}
3016+
3017+
/**
3018+
* Resets checksum calculation statistics
3019+
*/
3020+
void Note::resetChecksumStats() {
3021+
g_totalChecksumTime = 0;
3022+
g_checksumCallCount = 0;
3023+
qDebug() << "Note::resetChecksumStats - Checksum statistics have been reset";
29813024
}
29823025

29833026
/**

src/entities/note.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ class Note {
174174

175175
static QString calculateChecksum(const QString &text);
176176

177+
static void getChecksumStats(qint64 &totalTime, int &callCount);
178+
179+
static void resetChecksumStats();
180+
177181
QString encryptNoteText();
178182

179183
QString fetchDecryptedNoteText() const;

src/mainwindow.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3522,6 +3522,20 @@ bool MainWindow::buildNotesIndex(int noteSubFolderId, bool forceRebuild) {
35223522

35233523
if (noteSubFolderId == 0) {
35243524
removeConflictedNotesDatabaseCopies();
3525+
3526+
// Log checksum calculation statistics for the entire note folder
3527+
qint64 totalTime = 0;
3528+
int callCount = 0;
3529+
Note::getChecksumStats(totalTime, callCount);
3530+
qDebug() << "====================================================";
3531+
qDebug() << "Note folder loading complete - Checksum statistics:";
3532+
qDebug() << " Total calculateChecksum() calls:" << callCount;
3533+
qDebug() << " Total time spent:" << (totalTime / 1000000.0) << "ms";
3534+
if (callCount > 0) {
3535+
qDebug() << " Average time per call:" << (totalTime / callCount / 1000.0)
3536+
<< "microseconds";
3537+
}
3538+
qDebug() << "====================================================";
35253539
}
35263540

35273541
return wasModified;

0 commit comments

Comments
 (0)