From 118f64f2060172a0baf9c24b9d7dd9f6752f931b Mon Sep 17 00:00:00 2001 From: Emil Sjolander Date: Thu, 11 Aug 2016 02:23:52 -0700 Subject: [PATCH] Add benchmarks and change repetition count Summary: The previous repetition count did not scale to many benchmarks. Also add more benchmarks Reviewed By: adamjernst Differential Revision: D3697280 fbshipit-source-id: 56fe424f36936445f31d6e9fa080abbdd816d32d --- benchmarks/CSSBenchmark.c | 51 ++++++++++++++++++++++++++++++++++++--- benchmarks/CSSBenchmark.h | 2 +- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/benchmarks/CSSBenchmark.c b/benchmarks/CSSBenchmark.c index 839be338..d86e7fb6 100644 --- a/benchmarks/CSSBenchmark.c +++ b/benchmarks/CSSBenchmark.c @@ -9,8 +9,20 @@ #include "CSSBenchmark.h" +#include #include +// Measure functions can be quite slow, for example when measuring text. +// Simulate this by sleeping for 1 millisecond. +static CSSSize _measure(void *context, float width, CSSMeasureMode widthMode, float height, CSSMeasureMode heightMode) { + struct timespec sleeptime = {0, 1000000}; + nanosleep(&sleeptime, NULL); + return (CSSSize) { + .width = widthMode == CSSMeasureModeUndefined ? 10 : width, + .height = heightMode == CSSMeasureModeUndefined ? 10 : width, + }; +} + CSS_BENCHMARKS({ CSS_BENCHMARK("Stack with flex", { @@ -18,14 +30,47 @@ CSS_BENCHMARKS({ CSSNodeStyleSetWidth(root, 100); CSSNodeStyleSetHeight(root, 100); - for (uint32_t i = 0; i < 3; i++) { + for (uint32_t i = 0; i < 10; i++) { CSSNodeRef child = CSSNodeNew(); - CSSNodeStyleSetHeight(child, 20); + CSSNodeSetMeasureFunc(child, _measure); CSSNodeStyleSetFlex(child, 1); CSSNodeInsertChild(root, child, 0); } - CSSNodeCalculateLayout(root, 100, 100, CSSDirectionLTR); + CSSNodeCalculateLayout(root, CSSUndefined, CSSUndefined, CSSDirectionLTR); + }); + + CSS_BENCHMARK("Align stretch in undefined axis", { + CSSNodeRef root = CSSNodeNew(); + + for (uint32_t i = 0; i < 10; i++) { + CSSNodeRef child = CSSNodeNew(); + CSSNodeStyleSetHeight(child, 20); + CSSNodeSetMeasureFunc(child, _measure); + CSSNodeInsertChild(root, child, 0); + } + + CSSNodeCalculateLayout(root, CSSUndefined, CSSUndefined, CSSDirectionLTR); + }); + + CSS_BENCHMARK("Nested flex", { + CSSNodeRef root = CSSNodeNew(); + + for (uint32_t i = 0; i < 10; i++) { + CSSNodeRef child = CSSNodeNew(); + CSSNodeSetMeasureFunc(child, _measure); + CSSNodeStyleSetFlex(child, 1); + CSSNodeInsertChild(root, child, 0); + + for (uint32_t ii = 0; ii < 10; ii++) { + CSSNodeRef grandChild = CSSNodeNew(); + CSSNodeSetMeasureFunc(grandChild, _measure); + CSSNodeStyleSetFlex(grandChild, 1); + CSSNodeInsertChild(child, grandChild, 0); + } + } + + CSSNodeCalculateLayout(root, CSSUndefined, CSSUndefined, CSSDirectionLTR); }); }); diff --git a/benchmarks/CSSBenchmark.h b/benchmarks/CSSBenchmark.h index 7b5bc291..fdb0f5eb 100644 --- a/benchmarks/CSSBenchmark.h +++ b/benchmarks/CSSBenchmark.h @@ -14,7 +14,7 @@ #include #include -#define NUM_REPETITIONS 100000 +#define NUM_REPETITIONS 100 #define CSS_BENCHMARKS(BLOCK) \ int main(int argc, char const *argv[]) { \