From 949482f8e43533d3370201c0f216253c7b5872bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Nowacki?= Date: Tue, 26 Feb 2019 09:31:18 +0100 Subject: Add QObject allocation benchmarks The benchmark measures the performance of QObject allocation, including costs of memory allocations. Change-Id: I5d8ecfb97fe0be3375340b5ce84eb423e8a4ddaf Reviewed-by: Volker Hilsheimer --- tests/benchmarks/corelib/kernel/qobject/main.cpp | 47 ++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'tests/benchmarks/corelib/kernel/qobject') diff --git a/tests/benchmarks/corelib/kernel/qobject/main.cpp b/tests/benchmarks/corelib/kernel/qobject/main.cpp index 04ca69ad3b..918227f74e 100644 --- a/tests/benchmarks/corelib/kernel/qobject/main.cpp +++ b/tests/benchmarks/corelib/kernel/qobject/main.cpp @@ -51,8 +51,55 @@ private slots: void connect_disconnect_benchmark_data(); void connect_disconnect_benchmark(); void receiver_destroyed_benchmark(); + + void stdAllocator(); }; +class QObjectUsingStandardAllocator : public QObject +{ + Q_OBJECT +public: + QObjectUsingStandardAllocator() + { + } +}; + +template +inline void allocator() +{ + // We need to allocate certain amount of objects otherwise the new implementation + // may re-use the previous allocation, hiding the somehow high cost of allocation. It + // also helps us to reduce the noise ratio, which is high for memory allocation. + // + // The check depends on memory allocation performance, which is quite non-deterministic. + // When a new memory is requested, the new operator, depending on implementation, is trying + // to re-use existing, already allocated for the process memory. If there is not enough, it + // asks OS to give more. Of course the first case is faster then the second. In the same + // time, from an application perspective the first is also more likely. + // + // As a result, depending on which use-case one wants to test, it may be recommended to run this + // test in separation from others, to "force" expensive code path in the memory allocation. + // + // The time based results are heavily affected by background noise. One really needs to + // prepare OS (no other tasks, CPU and RAM reservations) to run this test, or use + // instruction counting which seems to be less fragile. + + const int count = 256 * 1024; + + QScopedPointer objects[count]; + QBENCHMARK_ONCE { + for (int i = 0; i < count; ++i) + objects[i].reset(new T); + for (int i = 0; i < count; ++i) + objects[i].reset(); + } +} + +void QObjectBenchmark::stdAllocator() +{ + allocator(); +} + struct Functor { void operator()(){} }; -- cgit v1.2.3