From 880fbb84acf853be8ac0c5a23ff0475e8ce6dff0 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 22 Jun 2022 16:44:30 +0200 Subject: Add --console mode, to be used remotely --- src/consoleworker.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/consoleworker.h | 34 ++++++++++++++++++++++++++ src/copythread.cpp | 10 ++++---- src/copythread.h | 12 +++++++++- src/main.cpp | 38 +++++++++++++++++++---------- src/speedcheck.cpp | 4 ++-- src/speedcheck.h | 3 ++- src/ui/speedcheck.ui | 13 ++++++++-- 8 files changed, 156 insertions(+), 24 deletions(-) create mode 100644 src/consoleworker.cpp create mode 100644 src/consoleworker.h diff --git a/src/consoleworker.cpp b/src/consoleworker.cpp new file mode 100644 index 0000000..a869725 --- /dev/null +++ b/src/consoleworker.cpp @@ -0,0 +1,66 @@ +#include "consoleworker.h" +#include "copythread.h" +#include "datasource/cpuload.h" +#include "datasource/networkspeed.h" + +#include +#include +#include + +ConsoleWorker::ConsoleWorker(QString fileName) : + _thread(nullptr), + _fileName(fileName), + _networkSource(nullptr) +{ + _timer.setInterval(200); + connect(&_timer, &QTimer::timeout, this, &ConsoleWorker::updateTimer); + QTimer::singleShot(1, [this]() { + QFile *file = new QFile(_fileName); + if (!file->open(QIODevice::ReadOnly)) { + puts(tr("Could not open %1 for reading.").arg(_fileName).toLocal8Bit().constData()); + return; + } + _networkSource = new NetworkSpeed(); + _thread = new CopyThread(file, this); + connect(_thread, &CopyThread::logMessage, this, &ConsoleWorker::logMessage, Qt::QueuedConnection); + connect(_thread, &CopyThread::finished, this, &ConsoleWorker::testFinished, Qt::QueuedConnection); + _timer.start(); + _thread->start(); + }); +} + +ConsoleWorker::~ConsoleWorker() +{ + delete _networkSource; +} + +void ConsoleWorker::logMessage(CopyThread::LogMessageId msgId, QString message) +{ + if (msgId == CopyThread::TestRandomStart) { + printf("+SEQ"); + } else if (msgId == CopyThread::TestFinished) { + printf("+RND"); + } + if (msgId == CopyThread::TestRandomStart || msgId == CopyThread::TestFinished) { + for (int i : _series) { + printf(",%i", i); + } + putchar('\n'); + } + _series.clear(); + puts(message.toLocal8Bit().constData()); +} + +void ConsoleWorker::testFinished() +{ + _timer.stop(); + delete _thread; + _thread = NULL; + QCoreApplication::instance()->quit(); +} + +void ConsoleWorker::updateTimer() +{ + int i = (int)_networkSource->read(); + _series.append(i); +} diff --git a/src/consoleworker.h b/src/consoleworker.h new file mode 100644 index 0000000..276f987 --- /dev/null +++ b/src/consoleworker.h @@ -0,0 +1,34 @@ +#ifndef _CONSOLEWORKER_H_ +#define _CONSOLEWORKER_H_ + +#include "datasource/idatasource.h" +#include "copythread.h" + +#include +#include +#include + +class CopyThread; + +class ConsoleWorker : public QObject +{ + Q_OBJECT + +public: + ConsoleWorker(QString fileName); + virtual ~ConsoleWorker(); + +private slots: + void logMessage(CopyThread::LogMessageId msgId, QString message); + void testFinished(); + void updateTimer(); + +private: + CopyThread *_thread; + QString _fileName; + QTimer _timer; + IDataSource* _networkSource; + QVector _series; +}; + +#endif diff --git a/src/copythread.cpp b/src/copythread.cpp index 21aadbb..7fda016 100644 --- a/src/copythread.cpp +++ b/src/copythread.cpp @@ -38,22 +38,22 @@ void CopyThread::run() qint64 seqSum = 0, rndSum = 0; qint64 seqTime, rndTime = -1; - const qint64 size = _file->size() - BUFFER_SIZE; + const qint64 size = _file->size(); // Sequential read - emit logMessage(tr("Starting sequential read test")); + emit logMessage(CopyThread::TestSequentialStart, tr("Starting sequential read test")); timer.start(); do { ret = _file->read(buffer, BUFFER_SIZE); seqSum += ret; if (ret == 0) break; // EOF - } while (!_doStop && ret >= 0 && timer.elapsed() < TEST_LENGTH); + } while (!_doStop && ret > 0 && timer.elapsed() < TEST_LENGTH); seqTime = timer.elapsed(); // Random read if (size > 0) { - emit logMessage(tr("Starting random read test")); + emit logMessage(CopyThread::TestRandomStart, tr("Starting random read test")); timer.restart(); do { _file->seek(BIGRAND % size); @@ -66,7 +66,7 @@ void CopyThread::run() // All done const qint64 seqSpeed = seqSum / (seqTime * 1024 + 1); const qint64 rndSpeed = rndSum / (rndTime * 1024 + 1); - emit logMessage(tr("Seq: %1MiB/s, Random: %2MiB/s - [%3s / %4s]") + emit logMessage(CopyThread::TestFinished, tr("Seq: %1MiB/s, Random: %2MiB/s - [%3s / %4s]") .arg(QString::number(seqSpeed), QString::number(rndSpeed), QString::number(seqTime / 1000), QString::number(rndTime / 1000))); delete[] buffer; diff --git a/src/copythread.h b/src/copythread.h index 428c7fa..ce6be83 100644 --- a/src/copythread.h +++ b/src/copythread.h @@ -2,6 +2,7 @@ #define COPYTHREAD_H_ #include +#include class QFile; @@ -12,6 +13,13 @@ public: virtual ~CopyThread(); void stop(); + enum LogMessageId { + None, + TestSequentialStart, + TestRandomStart, + TestFinished, + }; + protected: virtual void run(); @@ -20,7 +28,9 @@ private: volatile bool _doStop; signals: - void logMessage(QString message); + void logMessage(LogMessageId msgId, QString message); }; +Q_DECLARE_METATYPE(CopyThread::LogMessageId); + #endif /* COPYTHREAD_H_ */ diff --git a/src/main.cpp b/src/main.cpp index 5d9a396..8d60fe2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,22 +1,34 @@ #include "speedcheck.h" +#include "consoleworker.h" +#include "copythread.h" + #include #include +#include int main(int argc, char **argv) { - QApplication app(argc, argv); - bool autoStart = false; - if (argc < 2) { - QMessageBox::critical(NULL, "Error", "Need one argument: file name"); - return 1; - } - if (argc >= 3 && strcmp(argv[1], "--auto") == 0) { - autoStart = true; - argc--; - argv++; + if (argc >= 2 && strcmp(argv[1], "--console") == 0) { + QCoreApplication app(argc, argv); + qRegisterMetaType(); + ConsoleWorker main(QString::fromLocal8Bit(argv[2])); + app.exec(); + } else { + QApplication app(argc, argv); + qRegisterMetaType(); + bool autoStart = false; + if (argc < 2) { + QMessageBox::critical(NULL, "Error", "Need one argument: file name"); + return 1; + } + if (argc >= 3 && strcmp(argv[1], "--auto") == 0) { + autoStart = true; + argc--; + argv++; + } + SpeedCheck main(QString::fromLocal8Bit(argv[1]), autoStart); + main.show(); + app.exec(); } - SpeedCheck main(QString::fromLocal8Bit(argv[1]), autoStart); - main.show(); - app.exec(); return 0; } diff --git a/src/speedcheck.cpp b/src/speedcheck.cpp index 428be75..1248323 100644 --- a/src/speedcheck.cpp +++ b/src/speedcheck.cpp @@ -3,6 +3,7 @@ #include "copythread.h" #include "datasource/cpuload.h" #include "datasource/networkspeed.h" +#include "copythread.h" #include #include @@ -22,7 +23,6 @@ SpeedCheck::SpeedCheck(QString fileName, bool autoStart) _timer.setInterval(200); connect(&_timer, SIGNAL(timeout()), this, SLOT(updateTimer())); if (autoStart) { - _doQuit = true; QTimer::singleShot(1, [this]() { this->startClicked(true); }); @@ -34,7 +34,7 @@ SpeedCheck::~SpeedCheck() } -void SpeedCheck::logMessage(QString message) +void SpeedCheck::logMessage(CopyThread::LogMessageId, QString message) { qDebug() << message; _ui->statusBar->showMessage(message); diff --git a/src/speedcheck.h b/src/speedcheck.h index c89e3f1..c665ff0 100644 --- a/src/speedcheck.h +++ b/src/speedcheck.h @@ -3,6 +3,7 @@ #include #include +#include "copythread.h" namespace Ui { @@ -18,7 +19,7 @@ public: virtual ~SpeedCheck(); private slots: - void logMessage(QString message); + void logMessage(CopyThread::LogMessageId msgId, QString message); void startClicked(bool); void quitClicked(bool); void testFinished(); diff --git a/src/ui/speedcheck.ui b/src/ui/speedcheck.ui index 4e59fc1..d9dae26 100644 --- a/src/ui/speedcheck.ui +++ b/src/ui/speedcheck.ui @@ -24,7 +24,7 @@ - Network (100MBit / 1000GBit) + Network (100MBit / 1000MBit) @@ -83,7 +83,16 @@ 3 - + + 0 + + + 0 + + + 0 + + 0 -- cgit v1.2.3-55-g7522