summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJędrzej Nowacki <jedrzej.nowacki@qt.io>2019-02-26 09:31:18 +0100
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2019-08-29 14:04:27 +0200
commit949482f8e43533d3370201c0f216253c7b5872bd (patch)
tree2af0a7b53785f39c8e6bb05fbdff875d42423ebf
parentce8ab39a0897600fabde9001961bc2baa1ada726 (diff)
Add QObject allocation benchmarks
The benchmark measures the performance of QObject allocation, including costs of memory allocations. Change-Id: I5d8ecfb97fe0be3375340b5ce84eb423e8a4ddaf Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
-rw-r--r--tests/benchmarks/corelib/kernel/qobject/main.cpp47
1 files changed, 47 insertions, 0 deletions
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<class T>
+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<T> 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<QObjectUsingStandardAllocator>();
+}
+
struct Functor {
void operator()(){}
};