mirror of https://github.com/status-im/qzxing.git
Added blocking map-reduce concurrency when executing EncoderStressTest. (still has issues.)
This commit is contained in:
parent
83243790f8
commit
9dfa221563
|
@ -1,11 +1,15 @@
|
|||
#include "EncoderStressTest.h"
|
||||
#include <iostream>
|
||||
#include <QtConcurrent>
|
||||
|
||||
namespace zxing {
|
||||
namespace tests{
|
||||
|
||||
EncoderStressTest::EncoderStressTest(): TestCase(), decoder(QZXing::DecoderFormat_QR_CODE),
|
||||
sumOfSuccessfullTests(0), totalExecutedTests(0)
|
||||
QMutex EncoderStressTest::mutex;
|
||||
QMutex EncoderStressTest::printLockMutex;
|
||||
|
||||
EncoderStressTest::EncoderStressTest(): TestCase(),
|
||||
sumOfSuccessfullTests_(0), totalExecutedTests_(0)
|
||||
{
|
||||
/**
|
||||
N A B
|
||||
|
@ -15,20 +19,23 @@ EncoderStressTest::EncoderStressTest(): TestCase(), decoder(QZXing::DecoderForma
|
|||
H 3057 1852 1273
|
||||
*/
|
||||
|
||||
maxCharacterNumberMap_[qrcode::ErrorCorrectionLevel::L.name()][qrcode::Mode::NUMERIC.getName()] = 7089;
|
||||
maxCharacterNumberMap_[qrcode::ErrorCorrectionLevel::M.name()][qrcode::Mode::NUMERIC.getName()] = 5596;
|
||||
maxCharacterNumberMap_[qrcode::ErrorCorrectionLevel::Q.name()][qrcode::Mode::NUMERIC.getName()] = 3993;
|
||||
maxCharacterNumberMap_[qrcode::ErrorCorrectionLevel::H.name()][qrcode::Mode::NUMERIC.getName()] = 3057;
|
||||
// 4 different error correction levels
|
||||
maxCharacterNumberMap_.resize(4);
|
||||
|
||||
maxCharacterNumberMap_[qrcode::ErrorCorrectionLevel::L.name()][qrcode::Mode::ALPHANUMERIC.getName()] = 4296;
|
||||
maxCharacterNumberMap_[qrcode::ErrorCorrectionLevel::M.name()][qrcode::Mode::ALPHANUMERIC.getName()] = 3391;
|
||||
maxCharacterNumberMap_[qrcode::ErrorCorrectionLevel::Q.name()][qrcode::Mode::ALPHANUMERIC.getName()] = 2420;
|
||||
maxCharacterNumberMap_[qrcode::ErrorCorrectionLevel::H.name()][qrcode::Mode::ALPHANUMERIC.getName()] = 1852;
|
||||
maxCharacterNumberMap_[QZXing::EncodeErrorCorrectionLevel_L][qrcode::Mode::NUMERIC.getName()] = 7089;
|
||||
maxCharacterNumberMap_[QZXing::EncodeErrorCorrectionLevel_M][qrcode::Mode::NUMERIC.getName()] = 5596;
|
||||
maxCharacterNumberMap_[QZXing::EncodeErrorCorrectionLevel_Q][qrcode::Mode::NUMERIC.getName()] = 3993;
|
||||
maxCharacterNumberMap_[QZXing::EncodeErrorCorrectionLevel_H][qrcode::Mode::NUMERIC.getName()] = 3057;
|
||||
|
||||
maxCharacterNumberMap_[qrcode::ErrorCorrectionLevel::L.name()][qrcode::Mode::BYTE.getName()] = 2953;
|
||||
maxCharacterNumberMap_[qrcode::ErrorCorrectionLevel::M.name()][qrcode::Mode::BYTE.getName()] = 2331;
|
||||
maxCharacterNumberMap_[qrcode::ErrorCorrectionLevel::Q.name()][qrcode::Mode::BYTE.getName()] = 1663;
|
||||
maxCharacterNumberMap_[qrcode::ErrorCorrectionLevel::H.name()][qrcode::Mode::BYTE.getName()] = 1273;
|
||||
maxCharacterNumberMap_[QZXing::EncodeErrorCorrectionLevel_L][qrcode::Mode::ALPHANUMERIC.getName()] = 4296;
|
||||
maxCharacterNumberMap_[QZXing::EncodeErrorCorrectionLevel_M][qrcode::Mode::ALPHANUMERIC.getName()] = 3391;
|
||||
maxCharacterNumberMap_[QZXing::EncodeErrorCorrectionLevel_Q][qrcode::Mode::ALPHANUMERIC.getName()] = 2420;
|
||||
maxCharacterNumberMap_[QZXing::EncodeErrorCorrectionLevel_H][qrcode::Mode::ALPHANUMERIC.getName()] = 1852;
|
||||
|
||||
maxCharacterNumberMap_[QZXing::EncodeErrorCorrectionLevel_L][qrcode::Mode::BYTE.getName()] = 2953;
|
||||
maxCharacterNumberMap_[QZXing::EncodeErrorCorrectionLevel_M][qrcode::Mode::BYTE.getName()] = 2331;
|
||||
maxCharacterNumberMap_[QZXing::EncodeErrorCorrectionLevel_Q][qrcode::Mode::BYTE.getName()] = 1663;
|
||||
maxCharacterNumberMap_[QZXing::EncodeErrorCorrectionLevel_H][qrcode::Mode::BYTE.getName()] = 1273;
|
||||
}
|
||||
|
||||
void zxing::tests::EncoderStressTest::execute()
|
||||
|
@ -37,14 +44,14 @@ void zxing::tests::EncoderStressTest::execute()
|
|||
runTests(zxing::qrcode::Mode::ALPHANUMERIC);
|
||||
runTests(zxing::qrcode::Mode::BYTE);
|
||||
|
||||
std::cout << "total: successful: " << sumOfSuccessfullTests << ", total: " << totalExecutedTests << std::endl;
|
||||
std::cout << "total: successful: " << sumOfSuccessfullTests_ << ", total: " << totalExecutedTests_ << std::endl;
|
||||
}
|
||||
|
||||
int EncoderStressTest::getMaxCharacterNumber(qrcode::ErrorCorrectionLevel &errorCorrectionLevel, qrcode::Mode &mode)
|
||||
int EncoderStressTest::getMaxCharacterNumber(QZXing::EncodeErrorCorrectionLevel errorCorrectionLevel, qrcode::Mode &mode)
|
||||
{
|
||||
if(maxCharacterNumberMap_.contains(errorCorrectionLevel.name()) &&
|
||||
maxCharacterNumberMap_[errorCorrectionLevel.name()].contains(mode.getName()))
|
||||
return maxCharacterNumberMap_[errorCorrectionLevel.name()][mode.getName()];
|
||||
if(maxCharacterNumberMap_.size() > errorCorrectionLevel &&
|
||||
maxCharacterNumberMap_[errorCorrectionLevel].contains(mode.getName()))
|
||||
return maxCharacterNumberMap_[errorCorrectionLevel][mode.getName()];
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
@ -76,44 +83,53 @@ char EncoderStressTest::getCharacterForQrMode(qrcode::Mode &mode)
|
|||
|
||||
void EncoderStressTest::runTests(qrcode::Mode &mode)
|
||||
{
|
||||
runTests(zxing::qrcode::ErrorCorrectionLevel::L, mode);
|
||||
runTests(zxing::qrcode::ErrorCorrectionLevel::M, mode);
|
||||
runTests(zxing::qrcode::ErrorCorrectionLevel::Q, mode);
|
||||
runTests(zxing::qrcode::ErrorCorrectionLevel::H, mode);
|
||||
runTests(QZXing::EncodeErrorCorrectionLevel_L, mode);
|
||||
runTests(QZXing::EncodeErrorCorrectionLevel_M, mode);
|
||||
runTests(QZXing::EncodeErrorCorrectionLevel_Q, mode);
|
||||
runTests(QZXing::EncodeErrorCorrectionLevel_H, mode);
|
||||
}
|
||||
|
||||
void EncoderStressTest::runTests(zxing::qrcode::ErrorCorrectionLevel &errorCorrectionLevel, zxing::qrcode::Mode &mode)
|
||||
void EncoderStressTest::runTests(QZXing::EncodeErrorCorrectionLevel errorCorrectionLevel, zxing::qrcode::Mode &mode)
|
||||
{
|
||||
currentSetupExecutedTests = 0;
|
||||
currentSetupSuccessfullTests = 0;
|
||||
|
||||
QString currentCharacters;
|
||||
int maxCharLength = getMaxCharacterNumber(errorCorrectionLevel, mode);
|
||||
|
||||
QList<TestRunData> testRunDataSet;
|
||||
for(int i=1; i<=maxCharLength; ++i)
|
||||
{
|
||||
currentCharacters += getCharacterForQrMode(mode);
|
||||
QImage generatedImage = QZXing::encodeData(currentCharacters);
|
||||
|
||||
QString decodedData = decoder.decodeImage(generatedImage);
|
||||
|
||||
std::cout << "M[" << mode.getName() << "], E[" << errorCorrectionLevel.name() << "], L[" << i << "]: " <<
|
||||
"decoded=" << decoder.getLastDecodeOperationSucceded() <<
|
||||
", match=" << (currentCharacters == decodedData) << std::endl;
|
||||
|
||||
if(decoder.getLastDecodeOperationSucceded() && (currentCharacters == decodedData))
|
||||
{
|
||||
++sumOfSuccessfullTests;
|
||||
++currentSetupSuccessfullTests;
|
||||
}
|
||||
++totalExecutedTests;
|
||||
++currentSetupExecutedTests;
|
||||
TestRunData testData;
|
||||
testData.data = currentCharacters;
|
||||
testData.errorCorrectionLevel = errorCorrectionLevel;
|
||||
testData.mode = mode;
|
||||
testRunDataSet.append(testData);
|
||||
}
|
||||
|
||||
std::cout << "subtotal: M[" << mode.getName() << "], E[" << errorCorrectionLevel.name() << "]: " <<
|
||||
"successfull: " << currentSetupSuccessfullTests << ", total: " << currentSetupExecutedTests << std::endl;
|
||||
QtConcurrent::blockingMap(testRunDataSet, EncoderStressTest::runTest);
|
||||
}
|
||||
|
||||
void EncoderStressTest::runTest(TestRunData &testData)
|
||||
{
|
||||
QImage generatedImage = QZXing::encodeData(testData.data,
|
||||
QZXing::EncoderFormat_QR_CODE,
|
||||
QSize(500, 500),
|
||||
testData.errorCorrectionLevel);
|
||||
|
||||
// mutex.lock();
|
||||
QZXing decoder(QZXing::DecoderFormat_QR_CODE);
|
||||
QString decodedData = decoder.decodeImage(generatedImage);
|
||||
bool wasDecoded = decoder.getLastDecodeOperationSucceded();
|
||||
// mutex.unlock();
|
||||
|
||||
testData.successful = (testData.data == decodedData);
|
||||
|
||||
printLockMutex.lock();
|
||||
std::cout << "M[" << testData.mode.getName() << "], E[" << testData.errorCorrectionLevel << "], L[" << testData.data.size() << "]: " <<
|
||||
"decoded=" << wasDecoded <<
|
||||
", match=" << testData.successful << std::endl;
|
||||
printLockMutex.unlock();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,10 +10,28 @@
|
|||
#include <QtGlobal>
|
||||
#include <TestCase.h>
|
||||
#include <zxing/qrcode/decoder/Mode.h>
|
||||
#include <QWaitCondition>
|
||||
#include <QMutex>
|
||||
#include <QVector>
|
||||
#include <QThreadPool>
|
||||
#include <QFuture>
|
||||
#include <QMutex>
|
||||
|
||||
namespace zxing {
|
||||
namespace tests{
|
||||
|
||||
struct TestRunData {
|
||||
QString data;
|
||||
QZXing::EncodeErrorCorrectionLevel errorCorrectionLevel;
|
||||
qrcode::Mode mode;
|
||||
|
||||
bool successful;
|
||||
|
||||
public:
|
||||
TestRunData(): data(""), errorCorrectionLevel(QZXing::EncodeErrorCorrectionLevel_L),
|
||||
mode(qrcode::Mode::BYTE), successful(false) {}
|
||||
};
|
||||
|
||||
class EncoderStressTest : public TestCase
|
||||
{
|
||||
public:
|
||||
|
@ -21,21 +39,21 @@ public:
|
|||
void execute() override;
|
||||
|
||||
protected:
|
||||
int getMaxCharacterNumber(zxing::qrcode::ErrorCorrectionLevel &errorCorrectionLevel, zxing::qrcode::Mode &mode);
|
||||
int getMaxCharacterNumber(QZXing::EncodeErrorCorrectionLevel errorCorrectionLevel, zxing::qrcode::Mode &mode);
|
||||
char getCharacterForQrMode(zxing::qrcode::Mode &mode);
|
||||
|
||||
void runTests(zxing::qrcode::Mode &mode);
|
||||
void runTests(zxing::qrcode::ErrorCorrectionLevel &errorCorrectionLevel, zxing::qrcode::Mode &mode);
|
||||
void runTests(QZXing::EncodeErrorCorrectionLevel errorCorrectionLevel, zxing::qrcode::Mode &mode);
|
||||
|
||||
static void runTest(TestRunData &testData);
|
||||
|
||||
private:
|
||||
QZXing decoder;
|
||||
QMap<std::string, QMap<std::string, int>> maxCharacterNumberMap_;
|
||||
QVector<QMap<std::string, int>> maxCharacterNumberMap_;
|
||||
size_t sumOfSuccessfullTests_;
|
||||
size_t totalExecutedTests_;
|
||||
|
||||
int sumOfSuccessfullTests;
|
||||
int totalExecutedTests;
|
||||
|
||||
int currentSetupSuccessfullTests;
|
||||
int currentSetupExecutedTests;
|
||||
static QMutex mutex;
|
||||
static QMutex printLockMutex;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@ TARGET = QZXingTests
|
|||
CONFIG += console
|
||||
CONFIG -= app_bundle
|
||||
|
||||
QT += concurrent
|
||||
|
||||
TEMPLATE = app
|
||||
|
||||
HEADERS += \
|
||||
|
|
Loading…
Reference in New Issue