From 568671266e831633ada0af77e97ee365126b51ff Mon Sep 17 00:00:00 2001 From: Joe Vilches Date: Wed, 24 Jan 2024 12:30:22 -0800 Subject: [PATCH] Ability to capture trees as JSON files (#1565) Summary: This adds a `captureTree` method intended to be injected in somewhere where you would like a JSON representation of a yoga tree (like in calculateLayout). Right now it is very simple and just calls on `nodeToString` to serialize a node into JSON, and then saves that into a file in the `benchmark` dir. Some buck file changes needed to be done as well as this is the first `.cpp` file in `benchmark` Differential Revision: D52972995 --- benchmark/Benchmark.cpp | 32 ++++++++++++++++++++++++++++++++ benchmark/Benchmark.h | 19 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 benchmark/Benchmark.cpp create mode 100644 benchmark/Benchmark.h diff --git a/benchmark/Benchmark.cpp b/benchmark/Benchmark.cpp new file mode 100644 index 00000000..c63b9e78 --- /dev/null +++ b/benchmark/Benchmark.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#ifdef DEBUG + +#include +#include + +#include +#include +#include + +namespace facebook::yoga { + +void captureTree(const Node* node, std::string captureName) { + std::string str; + nodeToString(str, node, PrintOptions::Style | PrintOptions::Children); + std::filesystem::path currentPath = __FILE__; + std::ofstream file; + file.open( + currentPath.parent_path().string() + "/captures/" + captureName + + ".json"); + file << str; + file.close(); +} + +} // namespace facebook::yoga +#endif diff --git a/benchmark/Benchmark.h b/benchmark/Benchmark.h new file mode 100644 index 00000000..84d1af49 --- /dev/null +++ b/benchmark/Benchmark.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#ifdef DEBUG + +#pragma once + +#include + +namespace facebook::yoga { + +void captureTree(const Node* node, std::string captureName); + +} // namespace facebook::yoga +#endif