From c72321f8a9c0bff6c25c17ce6dc585d86a6b7535 Mon Sep 17 00:00:00 2001 From: Emil Sjolander Date: Mon, 1 Aug 2016 07:29:34 -0700 Subject: [PATCH] Add first benchmark Summary: Implement some very basic benchmarking infra. We need benchmarks in css-layout and I want to add something now so that others have the option to follow an example when implementing a benchmark. Reviewed By: lucasr Differential Revision: D3648889 fbshipit-source-id: 60b93c6e5ed027a37195a9a5d86e681e3e79a5b9 --- BUCK | 12 +++++++++++ benchmarks/CSSBenchmark.c | 31 ++++++++++++++++++++++++++++ benchmarks/CSSBenchmark.h | 43 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 benchmarks/CSSBenchmark.c create mode 100644 benchmarks/CSSBenchmark.h diff --git a/BUCK b/BUCK index b098c8e0..956fb103 100644 --- a/BUCK +++ b/BUCK @@ -33,6 +33,18 @@ cxx_library( visibility = ['PUBLIC'], ) +cxx_binary( + name = 'benchmark', + srcs = glob(['benchmarks/*.c']), + headers = subdir_glob([('', 'benchmarks/*.h')]), + header_namespace = '', + compiler_flags = COMPILER_FLAGS, + deps = [ + ':CSSLayout', + ], + visibility = ['PUBLIC'], +) + cxx_library( name = 'CSSLayoutTestUtils', srcs = glob(['tests/CSSLayoutTestUtils/*.c']), diff --git a/benchmarks/CSSBenchmark.c b/benchmarks/CSSBenchmark.c new file mode 100644 index 00000000..61f851c0 --- /dev/null +++ b/benchmarks/CSSBenchmark.c @@ -0,0 +1,31 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +#include "CSSBenchmark.h" + +#include + +CSS_BENCHMARKS({ + + CSS_BENCHMARK("Stack with flex", { + CSSNodeRef root = CSSNodeNew(); + CSSNodeStyleSetWidth(root, 100); + CSSNodeStyleSetHeight(root, 100); + + for (int i = 0; i < 3; i++) { + CSSNodeRef child = CSSNodeNew(); + CSSNodeStyleSetHeight(child, 20); + CSSNodeStyleSetFlex(child, 1); + CSSNodeInsertChild(root, child, 0); + } + + CSSNodeCalculateLayout(root, 100, 100, CSSDirectionLTR); + }); + +}); diff --git a/benchmarks/CSSBenchmark.h b/benchmarks/CSSBenchmark.h new file mode 100644 index 00000000..fb801b7b --- /dev/null +++ b/benchmarks/CSSBenchmark.h @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +#ifndef __CSS_BENCHMARK_H +#define __CSS_BENCHMARK_H + +#include +#include +#include + +#define NUM_REPETITIONS 100000 + +#define CSS_BENCHMARKS(BLOCK) \ +int main(int argc, char const *argv[]) { \ + clock_t __start; \ + clock_t __end; \ + { \ + BLOCK \ + } \ + return 0; \ +} + +#define CSS_BENCHMARK(NAME, BLOCK) \ +__start = clock(); \ +for (int __i = 0; __i < NUM_REPETITIONS; __i++) { BLOCK } \ +__end = clock(); \ +__printBenchmarkResult(NAME, __start, __end); + +void __printBenchmarkResult(char *name, clock_t start, clock_t end) { + float total = (end - start) / (double) CLOCKS_PER_SEC * 1000; + float mean = total / NUM_REPETITIONS; + printf("%s: ", name); + printf("%lf ms", mean); + printf("\n"); +} + +#endif