QBenchlib: use QBenchmarkMeasurerBase::Measurement in QBenchmarkResult

Change-Id: I3c79b7e08fa346988dfefffd17202a818cde1d84
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Thiago Macieira 2022-10-21 11:56:37 -07:00
parent 985b942152
commit b5b00e7790
6 changed files with 22 additions and 28 deletions

View File

@ -124,8 +124,8 @@ void QBenchmarkTestMethodData::setResult(QBenchmarkMeasurerBase::Measurement m,
else
iterationCount *= 2;
this->result = QBenchmarkResult(
QBenchmarkGlobalData::current->context, m.value, iterationCount, m.metric, setByMacro);
this->result = QBenchmarkResult(QBenchmarkGlobalData::current->context, m,
iterationCount, setByMacro);
}
/*!

View File

@ -61,28 +61,26 @@ class QBenchmarkResult
{
public:
QBenchmarkContext context;
qreal value = -1;
QBenchmarkMeasurerBase::Measurement measurement = { -1, QTest::FramesPerSecond };
int iterations = -1;
QTest::QBenchmarkMetric metric = QTest::FramesPerSecond;
bool setByMacro = true;
bool valid = false;
QBenchmarkResult() = default;
QBenchmarkResult(
const QBenchmarkContext &context, const qreal value, const int iterations,
QTest::QBenchmarkMetric metric, bool setByMacro)
const QBenchmarkContext &context, QBenchmarkMeasurerBase::Measurement m,
const int iterations, bool setByMacro)
: context(context)
, value(value)
, measurement(m)
, iterations(iterations)
, metric(metric)
, setByMacro(setByMacro)
, valid(true)
{ }
bool operator<(const QBenchmarkResult &other) const
{
return (value / iterations) < (other.value / other.iterations);
return (measurement.value / iterations) < (other.measurement.value / other.iterations);
}
};
Q_DECLARE_TYPEINFO(QBenchmarkResult, Q_RELOCATABLE_TYPE);

View File

@ -57,13 +57,14 @@ void QCsvBenchmarkLogger::addBenchmarkResult(const QBenchmarkResult &result)
: "";
const char *filler = (tag[0] && gtag[0]) ? ":" : "";
const char *metric = QTest::benchmarkMetricName(result.metric);
const char *metric = QTest::benchmarkMetricName(result.measurement.metric);
char buf[1024];
// "function","[globaltag:]tag","metric",value_per_iteration,total,iterations
qsnprintf(buf, sizeof(buf), "\"%s\",\"%s%s%s\",\"%s\",%.13g,%.13g,%u\n",
fn, gtag, filler, tag, metric,
result.value / result.iterations, result.value, result.iterations);
result.measurement.value / result.iterations,
result.measurement.value, result.iterations);
outputString(buf);
}

View File

@ -265,11 +265,11 @@ void QPlainTestLogger::printBenchmarkResult(const QBenchmarkResult &result)
char fill[1024];
qsnprintf(fill, sizeof(fill), fillFormat, "");
const char * unitText = QTest::benchmarkMetricUnit(result.metric);
const char * unitText = QTest::benchmarkMetricUnit(result.measurement.metric);
qreal valuePerIteration = qreal(result.value) / qreal(result.iterations);
qreal valuePerIteration = qreal(result.measurement.value) / qreal(result.iterations);
char resultBuffer[100] = "";
QTest::formatResult(resultBuffer, 100, valuePerIteration, QTest::countSignificantDigits(result.value));
QTest::formatResult(resultBuffer, 100, valuePerIteration, QTest::countSignificantDigits(result.measurement.value));
char buf2[1024];
qsnprintf(buf2, sizeof(buf2), "%s %s", resultBuffer, unitText);
@ -281,7 +281,8 @@ void QPlainTestLogger::printBenchmarkResult(const QBenchmarkResult &result)
char buf3[1024];
Q_ASSERT(result.iterations > 0);
QTest::formatResult(resultBuffer, 100, result.value, QTest::countSignificantDigits(result.value));
QTest::formatResult(resultBuffer, 100, result.measurement.value,
QTest::countSignificantDigits(result.measurement.value));
qsnprintf(buf3, sizeof(buf3), " (total: %s, iterations: %d)", resultBuffer, result.iterations);
char buf[1024];

View File

@ -1097,15 +1097,6 @@ struct QTestDataSetter
}
};
namespace {
qreal addResult(qreal current, const QBenchmarkResult& r)
{
return current + r.value;
}
}
void TestMethods::invokeTestOnData(int index) const
{
/* Benchmarking: for each median iteration*/
@ -1179,11 +1170,11 @@ void TestMethods::invokeTestOnData(int index) const
if (i == -1) {
QTestLog::info(qPrintable(
QString::fromLatin1("warmup stage result : %1")
.arg(QBenchmarkTestMethodData::current->result.value)), nullptr, 0);
.arg(QBenchmarkTestMethodData::current->result.measurement.value)), nullptr, 0);
} else {
QTestLog::info(qPrintable(
QString::fromLatin1("accumulation stage result: %1")
.arg(QBenchmarkTestMethodData::current->result.value)), nullptr, 0);
.arg(QBenchmarkTestMethodData::current->result.measurement.value)), nullptr, 0);
}
}
}
@ -1192,6 +1183,9 @@ void TestMethods::invokeTestOnData(int index) const
if (QBenchmarkGlobalData::current->minimumTotal == -1) {
minimumTotalReached = true;
} else {
auto addResult = [](qreal current, const QBenchmarkResult& r) {
return current + r.measurement.value;
};
const qreal total = std::accumulate(results.begin(), results.end(), 0.0, addResult);
minimumTotalReached = (total >= QBenchmarkGlobalData::current->minimumTotal);
}

View File

@ -255,14 +255,14 @@ void QXmlTestLogger::addBenchmarkResult(const QBenchmarkResult &result)
QTestCharBuffer quotedMetric;
QTestCharBuffer quotedTag;
if (xmlQuote(&quotedMetric, benchmarkMetricName(result.metric))
if (xmlQuote(&quotedMetric, benchmarkMetricName(result.measurement.metric))
&& xmlQuote(&quotedTag, result.context.tag.toUtf8().constData())) {
QTestCharBuffer buf;
QTest::qt_asprintf(&buf,
QTest::benchmarkResultFormatString(),
quotedMetric.constData(),
quotedTag.constData(),
result.value / double(result.iterations),
result.measurement.value / double(result.iterations),
result.iterations);
outputString(buf.constData());
}