From d4f10f9fe446da2aff65088e5236d6ffcdd034b5 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 24 Feb 2017 12:33:55 +0100 Subject: initial commit --- src/copythread.cpp | 72 +++++++++++++++++++++++++++ src/copythread.h | 26 ++++++++++ src/datasource/cpuload.cpp | 55 +++++++++++++++++++++ src/datasource/cpuload.h | 22 +++++++++ src/datasource/idatasource.h | 16 ++++++ src/datasource/networkspeed.cpp | 50 +++++++++++++++++++ src/datasource/networkspeed.h | 24 +++++++++ src/graphwidget.cpp | 95 +++++++++++++++++++++++++++++++++++ src/graphwidget.h | 33 +++++++++++++ src/main.cpp | 16 ++++++ src/speedcheck.cpp | 81 ++++++++++++++++++++++++++++++ src/speedcheck.h | 35 +++++++++++++ src/types.h | 6 +++ src/ui/speedcheck.ui | 107 ++++++++++++++++++++++++++++++++++++++++ 14 files changed, 638 insertions(+) create mode 100644 src/copythread.cpp create mode 100644 src/copythread.h create mode 100644 src/datasource/cpuload.cpp create mode 100644 src/datasource/cpuload.h create mode 100644 src/datasource/idatasource.h create mode 100644 src/datasource/networkspeed.cpp create mode 100644 src/datasource/networkspeed.h create mode 100644 src/graphwidget.cpp create mode 100644 src/graphwidget.h create mode 100644 src/main.cpp create mode 100644 src/speedcheck.cpp create mode 100644 src/speedcheck.h create mode 100644 src/types.h create mode 100644 src/ui/speedcheck.ui (limited to 'src') diff --git a/src/copythread.cpp b/src/copythread.cpp new file mode 100644 index 0000000..1f9ec2f --- /dev/null +++ b/src/copythread.cpp @@ -0,0 +1,72 @@ +#include "copythread.h" +#include +#include +#include +#include + +// 1M read size +#define BUFFER_SIZE (1000000) +// 20 seconds for each test +#define TEST_LENGTH (60000) + +CopyThread::CopyThread(QFile *file, QObject *parent) + : QThread(parent), + _doStop(false) +{ + _file = file; +} + +CopyThread::~CopyThread() +{ + delete _file; +} + +void CopyThread::stop() +{ + _doStop = true; +} + +void CopyThread::run() +{ + qDebug() << "Entering run method"; + char *buffer = new char[BUFFER_SIZE]; + QElapsedTimer timer; + qint64 ret; + qint64 seqSum = 0, rndSum = 0; + qint64 seqTime, rndTime = -1; + + // Sequential read + emit logMessage(trUtf8("Starting sequential read test")); + timer.start(); + do { + ret = _file->read(buffer, BUFFER_SIZE); + seqSum += ret; + if (ret == 0) { + _file->seek(0); + } + } while (!_doStop && ret >= 0 && timer.elapsed() < TEST_LENGTH); + seqTime = timer.elapsed(); + + // Random read + qsrand((uint)QCoreApplication::applicationPid()); + const qint64 size = _file->size() - BUFFER_SIZE; + if (size > 0) { + emit logMessage(trUtf8("Starting random read test")); + timer.restart(); + do { + _file->seek(((qint64)qrand() ^ ((qint64)qrand() << 15)) % size); + ret = _file->read(buffer, BUFFER_SIZE); + rndSum += ret; + } while (!_doStop && ret > 0 && timer.elapsed() < TEST_LENGTH); + rndTime = timer.elapsed(); + } + + // All done + const qint64 seqSpeed = seqSum / (seqTime * 1024 + 1); + const qint64 rndSpeed = rndSum / (rndTime * 1024 + 1); + emit logMessage(trUtf8("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; + _file->close(); +} diff --git a/src/copythread.h b/src/copythread.h new file mode 100644 index 0000000..428c7fa --- /dev/null +++ b/src/copythread.h @@ -0,0 +1,26 @@ +#ifndef COPYTHREAD_H_ +#define COPYTHREAD_H_ + +#include + +class QFile; + +class CopyThread : public QThread { + Q_OBJECT +public: + CopyThread(QFile *file, QObject *parent = NULL); + virtual ~CopyThread(); + void stop(); + +protected: + virtual void run(); + +private: + QFile *_file; + volatile bool _doStop; + +signals: + void logMessage(QString message); +}; + +#endif /* COPYTHREAD_H_ */ diff --git a/src/datasource/cpuload.cpp b/src/datasource/cpuload.cpp new file mode 100644 index 0000000..48831f3 --- /dev/null +++ b/src/datasource/cpuload.cpp @@ -0,0 +1,55 @@ +#include "cpuload.h" +#include + +CpuLoad::CpuLoad() : + _lastLoad(0), + _lastTotal(0), + _file("/proc/stat") +{ + // TODO Auto-generated constructor stub + +} + +CpuLoad::~CpuLoad() { + // TODO Auto-generated destructor stub +} + +qint64 CpuLoad::read() +{ + static QChar space(' '); + if (!_file.open(QIODevice::ReadOnly)) { + return -1; + } + char buffer[500]; + _file.readLine(buffer, sizeof(buffer)); + _file.close(); + QString line(buffer); + QStringList list = line.split(space, QString::SkipEmptyParts); + if (list.length() < 8) { + return -1; + } + // "cpu", user, nice, system, idle, iowait, irq, softirq + qint64 load = 0, total = 0; + for (int i = 1; i < 8; ++i) { + const qint64 value = list.at(i).toLongLong(); + if (i < 4 || i > 5) { + load += value; + } + total += value; + } + const qint64 ret = ((load - _lastLoad) * 1000) / (total - _lastTotal); + _lastTotal = total; + _lastLoad = load; + return ret; +} + +const QList& CpuLoad::getBars() const +{ + static QList list; + return list; +} + +qint64 CpuLoad::getMaximum() +{ + return 1000; +} diff --git a/src/datasource/cpuload.h b/src/datasource/cpuload.h new file mode 100644 index 0000000..2a5a2cb --- /dev/null +++ b/src/datasource/cpuload.h @@ -0,0 +1,22 @@ +#ifndef CPULOAD_H_ +#define CPULOAD_H_ + +#include "idatasource.h" +#include + +class CpuLoad : public IDataSource { +public: + CpuLoad(); + virtual ~CpuLoad(); + + virtual qint64 read(); + virtual const QList& getBars() const; + virtual qint64 getMaximum(); + +private: + qint64 _lastLoad; + qint64 _lastTotal; + QFile _file; +}; + +#endif /* CPULOAD_H_ */ diff --git a/src/datasource/idatasource.h b/src/datasource/idatasource.h new file mode 100644 index 0000000..4639d57 --- /dev/null +++ b/src/datasource/idatasource.h @@ -0,0 +1,16 @@ +#ifndef IDATASOURCE_H_ +#define IDATASOURCE_H_ + +#include + +class IDataSource { +public: + IDataSource() {}; + virtual ~IDataSource() {}; + + virtual qint64 read() = 0; + virtual const QList& getBars() const = 0; + virtual qint64 getMaximum() = 0; +}; + +#endif /* IDATASOURCE_H_ */ diff --git a/src/datasource/networkspeed.cpp b/src/datasource/networkspeed.cpp new file mode 100644 index 0000000..8888fd4 --- /dev/null +++ b/src/datasource/networkspeed.cpp @@ -0,0 +1,50 @@ +#include "networkspeed.h" + +NetworkSpeed::NetworkSpeed() : + _lastBytes(0), + _lastMs(0), + _file("/sys/class/net/eth0/statistics/rx_bytes") +{ + _timer.start(); +} + +NetworkSpeed::~NetworkSpeed() +{ + // TODO Auto-generated destructor stub +} + +qint64 NetworkSpeed::read() +{ + char buffer[500]; + qint64 ret, now; + if (!_file.open(QIODevice::ReadOnly)) { + return -1; + } + now = _timer.elapsed(); + _file.readLine(buffer, sizeof(buffer)); + _file.close(); + QString line(buffer); + const qint64 counter = (qint64)line.toLongLong(); + if (_lastBytes == 0) { + ret = 0; + } else { + ret = counter - _lastBytes; + } + if (_lastMs < now) { + ret = (ret * 1000) / (now - _lastMs); + } + _lastMs = now; + _lastBytes = counter; + return ret; +} + +const QList& NetworkSpeed::getBars() const +{ + static QList list = QList() << ((qint64)1024 * 1024 * 10); + return list; +} + +qint64 NetworkSpeed::getMaximum() +{ + return ((qint64)1024 * 1024 * 100); +} diff --git a/src/datasource/networkspeed.h b/src/datasource/networkspeed.h new file mode 100644 index 0000000..482f4cd --- /dev/null +++ b/src/datasource/networkspeed.h @@ -0,0 +1,24 @@ +#ifndef NETWORKSPEED_H_ +#define NETWORKSPEED_H_ + +#include "idatasource.h" +#include +#include + +class NetworkSpeed : public IDataSource { +public: + NetworkSpeed(); + virtual ~NetworkSpeed(); + + virtual qint64 read(); + virtual const QList& getBars() const; + virtual qint64 getMaximum(); + +private: + qint64 _lastBytes; + qint64 _lastMs; + QElapsedTimer _timer; + QFile _file; +}; + +#endif /* NETWORKSPEED_H_ */ diff --git a/src/graphwidget.cpp b/src/graphwidget.cpp new file mode 100644 index 0000000..ec15026 --- /dev/null +++ b/src/graphwidget.cpp @@ -0,0 +1,95 @@ +#include "graphwidget.h" +#include "datasource/idatasource.h" +#include +#include +#include +#include + +const QColor GraphWidget::LINE_COLOR = QColor::fromRgb(20, 255, 80); +const QColor GraphWidget::BACK_COLOR = QColor::fromRgb(10, 0, 20); +const QColor GraphWidget::BAR_COLOR = QColor::fromRgb(200, 200, 200); + +GraphWidget::GraphWidget(QWidget* parent) + : QWidget(parent), + _source(NULL), + _histPos(0), + _vScale(0), + _doubleSpace(false) +{ + memset(_history, 0, sizeof(*_history) * HISTSIZE); +} + +GraphWidget::~GraphWidget() +{ + delete _source; +} + +void GraphWidget::setDataSource(IDataSource* source) +{ + delete _source; + _source = source; + memset(_history, 0, sizeof(*_history) * HISTSIZE); + _vScale = (float)height() / (float)_source->getMaximum(); + qDebug() << "sDS " << _vScale; + update(); +} + +void GraphWidget::readNextValue() +{ + if (_source == NULL) + return; + // + _history[_histPos] = _source->read(); + _histPos = (_histPos + 1) % HISTSIZE; + if (_doubleSpace && _histPos % 2 == 1) { + _history[_histPos] = _history[(_histPos + HISTSIZE - 1) % HISTSIZE]; + update(_updateRectSmall); + } else { + update(); + } +} + +void GraphWidget::resizeEvent(QResizeEvent * event) +{ + const QSize &size = event->size(); + _updateRectSmall = QRect(size.width() - 1, 0, 1, size.height()); + if (_source != NULL) { + _vScale = (float)size.height() / (float)_source->getMaximum(); + } + _doubleSpace = (width() < 400); + update(); +} + +void GraphWidget::paintEvent(QPaintEvent * event) +{ + qint64 value; + int x, y; + const QRect &rect = event->rect(); + QPainter painter; + painter.begin(this); + painter.fillRect(rect, BACK_COLOR); + if (_source == NULL) + return; + painter.setPen(LINE_COLOR); + const int offset = HISTSIZE + HISTSIZE + _histPos - (this->width() - rect.x()) * (_doubleSpace ? 2 : 1); + // TODO: double space + for (int index = 0; index < rect.width(); ++index) { + x = rect.x() + index; + if (_doubleSpace) { + value = (_history[(offset + index * 2) % HISTSIZE] + _history[(offset + index * 2 + 1) % HISTSIZE]) / 2; + } else { + value = _history[(offset + index) % HISTSIZE]; + } + y = height() - int(value * _vScale); + painter.drawLine(x, y, x, height()); + } + const QList &bars = _source->getBars(); + if (!bars.empty()) { + painter.setPen(BAR_COLOR); + for (QList::const_iterator it = bars.begin(); it != bars.end(); ++it) { + y = height() - int(*it * _vScale); + painter.drawLine(rect.left(), y, rect.right() + 1, y); + } + } + painter.end(); +} diff --git a/src/graphwidget.h b/src/graphwidget.h new file mode 100644 index 0000000..f43e0db --- /dev/null +++ b/src/graphwidget.h @@ -0,0 +1,33 @@ +#ifndef GRAPHWIDGET_H_ +#define GRAPHWIDGET_H_ + +#include + +class IDataSource; + +class GraphWidget : public QWidget +{ + Q_OBJECT +public: + GraphWidget(QWidget* parent = NULL); + virtual ~GraphWidget(); + + void setDataSource(IDataSource* source); + void readNextValue(); + +protected: + virtual void resizeEvent(QResizeEvent * event); + virtual void paintEvent(QPaintEvent * event); + +private: + static const int HISTSIZE = 2000; + static const QColor LINE_COLOR, BACK_COLOR, BAR_COLOR; + IDataSource* _source; + qint64 _history[HISTSIZE]; + int _histPos; + float _vScale; + bool _doubleSpace; + QRect _updateRectSmall; +}; + +#endif /* GRAPHWIDGET_H_ */ diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..d5b71fb --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,16 @@ +#include "speedcheck.h" +#include +#include + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + if (argc != 2) { + QMessageBox::critical(NULL, "Error", "Need one argument: file name"); + return 1; + } + SpeedCheck main(QString::fromLocal8Bit(argv[1])); + main.show(); + app.exec(); + return 0; +} diff --git a/src/speedcheck.cpp b/src/speedcheck.cpp new file mode 100644 index 0000000..8ff8c93 --- /dev/null +++ b/src/speedcheck.cpp @@ -0,0 +1,81 @@ +#include "speedcheck.h" +#include "ui_speedcheck.h" +#include "copythread.h" +#include "datasource/cpuload.h" +#include "datasource/networkspeed.h" + +#include +#include +#include +#include +#include + +SpeedCheck::SpeedCheck(QString fileName) + : _ui(new Ui::MainWindow), + _thread(NULL), + _doQuit(false), + _fileName(fileName) +{ + _ui->setupUi(this); + connect(_ui->btnStart, SIGNAL(clicked(bool)), this, SLOT(startClicked(bool))); + connect(_ui->btnQuit, SIGNAL(clicked(bool)), this, SLOT(quitClicked(bool))); + _timer.setInterval(200); + connect(&_timer, SIGNAL(timeout()), this, SLOT(updateTimer())); +} + +SpeedCheck::~SpeedCheck() +{ + +} + +void SpeedCheck::logMessage(QString message) +{ + qDebug() << message; + _ui->statusBar->showMessage(message); +} + +void SpeedCheck::startClicked(bool) +{ + QFile *file = new QFile(_fileName); + if (!file->open(QIODevice::ReadOnly)) { + QMessageBox::critical(this, trUtf8("Error"), trUtf8("Could not open %1 for reading.").arg(_fileName)); + return; + } + _ui->picCpu->setDataSource(new CpuLoad()); + _ui->picSpeed->setDataSource(new NetworkSpeed()); + _ui->btnStart->setDisabled(true); + delete _thread; + _thread = new CopyThread(file, this); + connect(_thread, SIGNAL(logMessage(QString)), this, SLOT(logMessage(QString)), Qt::QueuedConnection); + connect(_thread, SIGNAL(finished()), this, SLOT(testFinished()), Qt::QueuedConnection); + _timer.start(); + _thread->start(); +} + +void SpeedCheck::quitClicked(bool) +{ + if (_thread != NULL && _thread->isRunning()) { + _doQuit = true; + _thread->stop(); + } + if (_thread == NULL || !_thread->isRunning()) { + QCoreApplication::instance()->quit(); + } +} + +void SpeedCheck::testFinished() +{ + _timer.stop(); + delete _thread; + _thread = NULL; + if (_doQuit) { + QCoreApplication::instance()->quit(); + } + _ui->btnStart->setEnabled(true); +} + +void SpeedCheck::updateTimer() +{ + _ui->picCpu->readNextValue(); + _ui->picSpeed->readNextValue(); +} diff --git a/src/speedcheck.h b/src/speedcheck.h new file mode 100644 index 0000000..fd54605 --- /dev/null +++ b/src/speedcheck.h @@ -0,0 +1,35 @@ +#ifndef SPEEDCHECK_H_ +#define SPEEDCHECK_H_ + +#include +#include + +namespace Ui +{ + class MainWindow; +} +class CopyThread; + +class SpeedCheck : public QMainWindow +{ + Q_OBJECT +public: + SpeedCheck(QString fileName); + virtual ~SpeedCheck(); + +private slots: + void logMessage(QString message); + void startClicked(bool); + void quitClicked(bool); + void testFinished(); + void updateTimer(); + +private: + Ui::MainWindow *_ui; + CopyThread *_thread; + volatile bool _doQuit; + QString _fileName; + QTimer _timer; +}; + +#endif /* SPEEDCHECK_H_ */ diff --git a/src/types.h b/src/types.h new file mode 100644 index 0000000..bc24687 --- /dev/null +++ b/src/types.h @@ -0,0 +1,6 @@ +#ifndef _TYPES_H_ +#define _TYPES_H_ + +#define UNUSED __attribute__ ((unused)) + +#endif diff --git a/src/ui/speedcheck.ui b/src/ui/speedcheck.ui new file mode 100644 index 0000000..2f98f3b --- /dev/null +++ b/src/ui/speedcheck.ui @@ -0,0 +1,107 @@ + + + MainWindow + + + + 0 + 0 + 800 + 600 + + + + SpeedCheck + + + + + + + + + + + + + + 0 + 0 + + + + + 0 + 32 + + + + + 16777215 + 32 + + + + + 0 + 32 + + + + + 0 + 32 + + + + + 3 + + + 0 + + + + + Start + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Quit + + + + + + + + + + + + + GraphWidget + QWidget +
graphwidget.h
+ 1 +
+
+ + +
-- cgit v1.2.3-55-g7522