Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

common: Added ScopyBenchmark class. #1770

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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: 55 additions & 0 deletions common/include/common/scopybenchmark.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2025 Analog Devices Inc.
*
* This file is part of Scopy
* (see https://www.github.com/analogdevicesinc/scopy).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

#ifndef SCOPYBENCHMARK_H
#define SCOPYBENCHMARK_H

#include <QElapsedTimer>
#include <QString>
#include "scopy-common_export.h"

#define CONSOLE_LOG(logger, msg) logger.log(msg, __PRETTY_FUNCTION__, __FILE__, __LINE__)
#define CONSOLE_LOG_RESET(logger, msg) logger.logAndReset(msg, __PRETTY_FUNCTION__, __FILE__, __LINE__)
#define FILE_LOG(logger, msg, path) logger.log(path, msg, __PRETTY_FUNCTION__, __FILE__, __LINE__)
#define FILE_LOG_RESET(logger, msg, path) logger.logAndReset(path, msg, __PRETTY_FUNCTION__, __FILE__, __LINE__)

namespace scopy {
class SCOPY_COMMON_EXPORT ScopyBenchmark
{
public:
ScopyBenchmark();
~ScopyBenchmark();

void startTimer();
void restartTimer();

void log(const QString &msg, const char *function, const char *file, int line);
void log(const QString &filePath, const QString &msg, const char *function, const char *file, int line);
void logAndReset(const QString &msg, const char *function, const char *file, int line);
void logAndReset(const QString &filePath, const QString &msg, const char *function, const char *file, int line);

private:
QElapsedTimer m_timer;
};

} // namespace scopy

#endif // SCOPYBENCHMARK_H
66 changes: 66 additions & 0 deletions common/src/scopybenchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2025 Analog Devices Inc.
*
* This file is part of Scopy
* (see https://www.github.com/analogdevicesinc/scopy).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

#include "scopybenchmark.h"
#include <QDate>
#include <QFile>
#include <QLoggingCategory>

Q_LOGGING_CATEGORY(CAT_BENCHMARK, "Benchmark")
using namespace scopy;

ScopyBenchmark::ScopyBenchmark() {}

ScopyBenchmark::~ScopyBenchmark() {}

void ScopyBenchmark::startTimer() { m_timer.start(); }

void ScopyBenchmark::restartTimer() { m_timer.restart(); }

void ScopyBenchmark::log(const QString &msg, const char *function, const char *file, int line)
{
QMessageLogger(file, line, function).info(CAT_BENCHMARK) << function << msg << m_timer.elapsed() << "ms";
}

void ScopyBenchmark::log(const QString &filePath, const QString &msg, const char *function, const char *file, int line)
{
QFile f(filePath);
if(f.open(QIODevice::WriteOnly | QIODevice::Append)) {
QTextStream stream(&f);
stream << QDateTime::currentDateTime().toString("dd:MM:yyyy hh:mm:ss.zzz") << "\t" << file << ":"
<< line << "\t" << function << "\t" << msg << "\t" << m_timer.elapsed() << "\t"
<< "ms"
<< "\n";
}
}

void ScopyBenchmark::logAndReset(const QString &msg, const char *function, const char *file, int line)
{
log(msg, function, file, line);
m_timer.restart();
}

void ScopyBenchmark::logAndReset(const QString &filePath, const QString &msg, const char *function, const char *file,
int line)
{
log(filePath, msg, function, file, line);
m_timer.restart();
}
45 changes: 23 additions & 22 deletions core/src/deviceimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

#include "deviceimpl.h"

#include "logging_categories.h"
#include "pluginbase/preferences.h"
#include "qboxlayout.h"
#include "qpushbutton.h"
Expand All @@ -37,6 +36,7 @@
#include <QtConcurrent/QtConcurrent>
#include <style.h>

#include <common/scopybenchmark.h>
#include <common/scopyconfig.h>
#include <gui/widgets/hoverwidget.h>
#include <gui/widgets/connectionloadingbar.h>
Expand All @@ -58,8 +58,8 @@ DeviceImpl::DeviceImpl(QString param, PluginManager *p, QString category, QObjec

void DeviceImpl::init()
{
QElapsedTimer timer;
timer.start();
ScopyBenchmark benchmark;
benchmark.startTimer();
m_plugins = p->getCompatiblePlugins(m_param, m_category);
for(Plugin *p : qAsConst(m_plugins)) {
QObject *obj = dynamic_cast<QObject *>(p);
Expand All @@ -69,7 +69,7 @@ void DeviceImpl::init()
qWarning(CAT_DEVICEIMPL, "Plugin not a QObject");
}
}
qInfo(CAT_BENCHMARK) << this->displayName() << " init took: " << timer.elapsed() << "ms";
CONSOLE_LOG(benchmark, "Dev init took:");
}

void DeviceImpl::preload()
Expand All @@ -81,8 +81,8 @@ void DeviceImpl::preload()

void DeviceImpl::loadPlugins()
{
QElapsedTimer timer;
timer.start();
ScopyBenchmark benchmark;
benchmark.startTimer();
removeDisabledPlugins();
preload();
loadName();
Expand All @@ -103,13 +103,13 @@ void DeviceImpl::loadPlugins()
p->postload();
}
m_state = DEV_IDLE;
qInfo(CAT_BENCHMARK) << this->displayName() << " plugins load took: " << timer.elapsed() << "ms";
CONSOLE_LOG(benchmark, this->displayName() + " plugins load took:");
}

void DeviceImpl::unloadPlugins()
{
QElapsedTimer timer;
timer.start();
ScopyBenchmark benchmark;
benchmark.startTimer();
QList<Plugin *>::const_iterator pI = m_plugins.constEnd();
while(pI != m_plugins.constBegin()) {
--pI;
Expand All @@ -123,7 +123,7 @@ void DeviceImpl::unloadPlugins()
delete(*pI);
}
m_plugins.clear();
qInfo(CAT_BENCHMARK) << this->displayName() << " plugins unload took: " << timer.elapsed() << "ms";
CONSOLE_LOG(benchmark, this->displayName() + " plugins unload took:");
}

bool DeviceImpl::verify() { return true; }
Expand Down Expand Up @@ -330,12 +330,12 @@ void DeviceImpl::load(QSettings &s)
void DeviceImpl::connectDev()
{
m_state = DEV_CONNECTING;
QElapsedTimer pluginTimer;
QElapsedTimer timer;
ScopyBenchmark pluginConnBm;
ScopyBenchmark connectDevBm;
ConnectionLoadingBar *connectionLoadingBar = new ConnectionLoadingBar();
connectionLoadingBar->setProgressBarMaximum(m_plugins.size());
StatusBarManager::pushUrgentWidget(connectionLoadingBar, "Connection Loading Bar");
timer.start();
connectDevBm.startTimer();
Preferences *pref = Preferences::GetInstance();
bool disconnectDevice = false;
connbtn->hide();
Expand All @@ -348,11 +348,11 @@ void DeviceImpl::connectDev()
Q_EMIT connecting();
QCoreApplication::processEvents();
for(int i = 0; i < m_plugins.size(); ++i) {
pluginTimer.start();
pluginConnBm.startTimer();
connectionLoadingBar->setCurrentPlugin(m_plugins[i]->name());
QCoreApplication::processEvents();
bool pluginConnectionSucceeded = m_plugins[i]->onConnect();
qInfo(CAT_BENCHMARK) << m_plugins[i]->name() << " connection took: " << pluginTimer.elapsed() << "ms";
CONSOLE_LOG(pluginConnBm, m_plugins[i]->name() + " connection took:");
connectionLoadingBar->addProgress(1); // TODO: might change to better reflect the time
QCoreApplication::processEvents();
if(pluginConnectionSucceeded) {
Expand Down Expand Up @@ -390,16 +390,17 @@ void DeviceImpl::connectDev()
m_state = DEV_CONNECTED;
Q_EMIT connected();
delete connectionLoadingBar;
qInfo(CAT_BENCHMARK) << this->displayName() << " device connection took: " << timer.elapsed() << "ms";
CONSOLE_LOG(connectDevBm, this->displayName() + " device connection took:");
}

void DeviceImpl::disconnectDev()
{
QElapsedTimer pluginTimer;
QElapsedTimer timer;
timer.start();
ScopyBenchmark pluginDisconnBm;
ScopyBenchmark disconnectDevBm;
disconnectDevBm.startTimer();
m_state = DEV_DISCONNECTING;
Q_EMIT disconnecting();

unbindPing();
connbtn->show();
discbtn->hide();
Expand All @@ -410,15 +411,15 @@ void DeviceImpl::disconnectDev()
QSettings::IniFormat);
p->saveSettings(s);
}
pluginTimer.start();
pluginDisconnBm.startTimer();
p->onDisconnect();
qInfo(CAT_BENCHMARK) << p->name() << " disconnection took: " << pluginTimer.elapsed() << "ms";
CONSOLE_LOG(pluginDisconnBm, p->name() + " disconnection took:");
}
m_connectedPlugins.clear();
connbtn->setFocus();
m_state = DEV_IDLE;
CONSOLE_LOG(disconnectDevBm, this->displayName() + " device disconnection took:");
Q_EMIT disconnected();
qInfo(CAT_BENCHMARK) << this->displayName() << " device disconnection took: " << timer.elapsed() << "ms";
}

DeviceImpl::~DeviceImpl() { qDebug(CAT_DEVICEIMPL) << m_id << "dtor"; }
Expand Down
31 changes: 16 additions & 15 deletions core/src/scopymainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <deviceautoconnect.h>
#include <style.h>

#include <common/scopybenchmark.h>
#include "logging_categories.h"
#include "qmessagebox.h"
#include "scopymainwindow.h"
Expand Down Expand Up @@ -70,8 +71,8 @@ ScopyMainWindow::ScopyMainWindow(QWidget *parent)
, ui(new Ui::ScopyMainWindow)
, m_glLoader(nullptr)
{
QElapsedTimer timer;
timer.start();
ScopyBenchmark benchmark;
benchmark.startTimer();
initPreferences();
ui->setupUi(this);

Expand Down Expand Up @@ -207,7 +208,7 @@ ScopyMainWindow::ScopyMainWindow(QWidget *parent)
deviceAutoconnect();
prefPage->initSessionDevices();

qInfo(CAT_BENCHMARK) << "ScopyMainWindow constructor took: " << timer.elapsed() << "ms";
CONSOLE_LOG(benchmark, "ScopyMainWindow constructor took:");
}

void ScopyMainWindow::initStatusBar()
Expand Down Expand Up @@ -319,8 +320,8 @@ ScopyMainWindow::~ScopyMainWindow()

void ScopyMainWindow::initAboutPage(PluginManager *pm)
{
QElapsedTimer timer;
timer.start();
ScopyBenchmark benchmark;
benchmark.startTimer();
about = new ScopyAboutPage(this);
if(!pm)
return;
Expand All @@ -331,7 +332,7 @@ void ScopyMainWindow::initAboutPage(PluginManager *pm)
about->addHorizontalTab(about->buildPage(content), p->name());
}
}
qInfo(CAT_BENCHMARK) << " Init about page took: " << timer.elapsed() << "ms";
CONSOLE_LOG(benchmark, "Init about page took:");
}

void ScopyMainWindow::initPreferencesPage(PluginManager *pm)
Expand Down Expand Up @@ -379,8 +380,8 @@ void ScopyMainWindow::setupPreferences()

void ScopyMainWindow::initPreferences()
{
QElapsedTimer timer;
timer.start();
ScopyBenchmark benchmark;
benchmark.startTimer();
QString preferencesPath = scopy::config::preferencesFolderPath() + "/preferences.ini";
Preferences *p = Preferences::GetInstance();
p->setPreferencesFilename(preferencesPath);
Expand Down Expand Up @@ -421,7 +422,7 @@ void ScopyMainWindow::initPreferences()
QString themeName = "scopy-" + theme;
QIcon::setThemeName(themeName);
QIcon::setThemeSearchPaths({":/gui/icons/" + themeName});
qInfo(CAT_BENCHMARK) << "Init preferences took: " << timer.elapsed() << "ms";
CONSOLE_LOG(benchmark, "Init preferences took:");
}

void ScopyMainWindow::loadOpenGL()
Expand Down Expand Up @@ -475,8 +476,8 @@ void ScopyMainWindow::loadOpenGL()
void ScopyMainWindow::loadPluginsFromRepository(PluginRepository *pr)
{

QElapsedTimer timer;
timer.start();
ScopyBenchmark benchmark;
benchmark.startTimer();
// Check the local build plugins folder first
// Check if directory exists and it's not empty
QDir pathDir(scopy::config::localPluginFolderPath());
Expand All @@ -494,7 +495,7 @@ void ScopyMainWindow::loadPluginsFromRepository(PluginRepository *pr)
}
#endif

qInfo(CAT_BENCHMARK) << "Loading the plugins from the repository took: " << timer.elapsed() << "ms";
CONSOLE_LOG(benchmark, "Loading the plugins from the repository took:");
}

void ScopyMainWindow::showEvent(QShowEvent *event)
Expand Down Expand Up @@ -565,8 +566,8 @@ void ScopyMainWindow::initPythonWIN32()

void ScopyMainWindow::loadDecoders()
{
QElapsedTimer timer;
timer.start();
ScopyBenchmark benchmark;
benchmark.startTimer();
#if defined(WITH_SIGROK) && defined(WITH_PYTHON)
#if defined __APPLE__
QString path = QCoreApplication::applicationDirPath() + "/decoders";
Expand Down Expand Up @@ -606,7 +607,7 @@ void ScopyMainWindow::loadDecoders()
#else
qInfo(CAT_SCOPY) << "Python or libsigrokdecode are disabled, can't load decoders";
#endif
qInfo(CAT_BENCHMARK) << "Loading the decoders took: " << timer.elapsed() << "ms";
CONSOLE_LOG(benchmark, "Loading the decoders took:");
}

void ScopyMainWindow::initApi()
Expand Down
Loading