File indexing completed on 2024-06-16 07:42:27

0001 /*
0002     SPDX-FileCopyrightText: 1998-2007 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 
0007 #include "k3bthroughputestimator.h"
0008 
0009 #include <QDateTime>
0010 #include <QDebug>
0011 #include <QElapsedTimer>
0012 
0013 class K3b::ThroughputEstimator::Private
0014 {
0015 public:
0016     Private()
0017         : started(false) {
0018     }
0019 
0020     QElapsedTimer firstDataTime;
0021     unsigned long firstData;
0022     QElapsedTimer lastDataTime;
0023     unsigned long lastData;
0024 
0025     int lastThroughput;
0026 
0027     bool started;
0028 };
0029 
0030 
0031 K3b::ThroughputEstimator::ThroughputEstimator( QObject* parent )
0032     : QObject( parent )
0033 {
0034     d = new Private();
0035 }
0036 
0037 
0038 K3b::ThroughputEstimator::~ThroughputEstimator()
0039 {
0040     delete d;
0041 }
0042 
0043 
0044 int K3b::ThroughputEstimator::average() const
0045 {
0046     int msecs = d->firstDataTime.msecsTo( d->lastDataTime );
0047     if( msecs > 0 )
0048         return (int)( 1000.0*(double)(d->lastData - d->firstData)/(double)msecs);
0049     else
0050         return 0;
0051 }
0052 
0053 
0054 void K3b::ThroughputEstimator::reset()
0055 {
0056     d->started = false;
0057 }
0058 
0059 
0060 void K3b::ThroughputEstimator::dataWritten( unsigned long data )
0061 {
0062     if( !d->started ) {
0063         d->started = true;
0064         d->firstData = d->lastData = data;
0065         d->firstDataTime.start();
0066         d->lastDataTime.start();
0067         d->lastThroughput = 0;
0068     }
0069     else if( data > d->lastData ) {
0070         unsigned long diff = data - d->lastData;
0071         int msecs = d->lastDataTime.elapsed();
0072 
0073         //if( msecs > 0 ) {
0074         // down the update sequence a little bit
0075         if( msecs > 500 ) {
0076             d->lastData = data;
0077             d->lastDataTime.start();
0078             int t = (int)(1000.0*(double)diff/(double)msecs);
0079             if( t != d->lastThroughput ) {
0080                 d->lastThroughput = t;
0081                 emit throughput( t );
0082             }
0083         }
0084     }
0085 }
0086 
0087 #include "moc_k3bthroughputestimator.cpp"