Commit: 20d0dcbe76b464cdf78fb1fe85ba5e56d6195540 Author: Jeroen Bakker Date: Wed May 26 14:33:10 2021 +0200 Branches: temp-lib-google-benchmark https://developer.blender.org/rB20d0dcbe76b464cdf78fb1fe85ba5e56d6195540
Migrated lattice deform benchmark. =================================================================== M build_files/cmake/macros.cmake M source/blender/blenkernel/CMakeLists.txt A source/blender/blenkernel/intern/lattice_deform_benchmark.cc M tests/gbenchmark/CMakeLists.txt M tests/gbenchmark/gbenchmark_test.cc =================================================================== diff --git a/build_files/cmake/macros.cmake b/build_files/cmake/macros.cmake index 813ac013cdf..f0f92c74134 100644 --- a/build_files/cmake/macros.cmake +++ b/build_files/cmake/macros.cmake @@ -504,6 +504,30 @@ function(blender_add_test_executable ) endfunction() +# Add tests for a Blender library, to be called in tandem with blender_add_lib(). +# The tests will be part of the blender_test executable (see tests/gtests/runner). +function(blender_add_benchmark_lib + name + sources + includes + includes_sys + library_deps + ) + + add_cc_flags_custom_test(${name} PARENT_SCOPE) + + # Otherwise external projects will produce warnings that we cannot fix. + remove_strict_flags() + + LIST(APPEND includes_sys + ${CMAKE_SOURCE_DIR}/extern/gbenchmark/include + ) + + blender_add_lib__impl(${name} "${sources}" "${includes}" "${includes_sys}" "${library_deps}") + + set_property(GLOBAL APPEND PROPERTY BLENDER_BENCHMARK_LIBS ${name}) +endfunction() + # Ninja only: assign 'heavy pool' to some targets that are especially RAM-consuming to build. function(setup_heavy_lib_pool) if(WITH_NINJA_POOL_JOBS AND NINJA_MAX_NUM_PARALLEL_COMPILE_HEAVY_JOBS) @@ -525,6 +549,36 @@ function(setup_heavy_lib_pool) endif() endfunction() +# Add benchmarks for a Blender library, to be called in tandem with blender_add_lib(). +# benchmark will be compiled into a ${name}_benchmark executable. +# +# To be used for smaller isolated libraries, that do not have many dependencies. +# For libraries that do drag in many other Blender libraries and would create a +# very large executable, blender_add_benchmark_lib() should be used instead. +function(blender_add_benchmark_executable + name + sources + includes + includes_sys + library_deps + ) + + add_cc_flags_custom_test(${name} PARENT_SCOPE) + + ## Otherwise external projects will produce warnings that we cannot fix. + remove_strict_flags() + + include_directories(${includes}) + include_directories(${includes_sys}) + setup_libdirs() + + blender_add_benchmark_suite( + TARGET ${name}_benchmark + SUITE_NAME ${name} + SOURCES "${sources}" + ) +endfunction() + function(SETUP_LIBDIRS) # NOTE: For all new libraries, use absolute library paths. diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt index 021d7e15814..3796cb068bd 100644 --- a/source/blender/blenkernel/CMakeLists.txt +++ b/source/blender/blenkernel/CMakeLists.txt @@ -778,3 +778,13 @@ if(WITH_GTESTS) include(GTestTesting) blender_add_test_lib(bf_blenkernel_tests "${TEST_SRC}" "${INC};${TEST_INC}" "${INC_SYS}" "${LIB}") endif() + +if(WITH_BENCHMARK_TESTS) + set(TEST_SRC + intern/lattice_deform_benchmark.cc + ) + set(TEST_INC + ) + # include(GBenchmarkTesting) + blender_add_benchmark_lib(bf_blenkernel_benchmarks "${TEST_SRC}" "${INC};${TEST_INC}" "${INC_SYS}" "${LIB}") +endif() diff --git a/source/blender/blenkernel/intern/lattice_deform_benchmark.cc b/source/blender/blenkernel/intern/lattice_deform_benchmark.cc new file mode 100644 index 00000000000..774ec65f137 --- /dev/null +++ b/source/blender/blenkernel/intern/lattice_deform_benchmark.cc @@ -0,0 +1,98 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2021 by Blender Foundation. + */ + +#include <benchmark/benchmark.h> + +#include "BKE_idtype.h" +#include "BKE_lattice.h" + +#include "MEM_guardedalloc.h" + +#include "DNA_lattice_types.h" +#include "DNA_mesh_types.h" +#include "DNA_object_types.h" + +#include "BLI_rand.hh" + +namespace blender::bke::benchmarks { + +struct LatticeDeformTestContext { + Lattice lattice; + Object ob_lattice; + Mesh mesh; + Object ob_mesh; + float (*coords)[3]; + LatticeDeformData *ldd; +}; + +static void test_lattice_deform_init(LatticeDeformTestContext *ctx, + RandomNumberGenerator *rng, + int32_t num_items) +{ + /* Generate random input data between -5 and 5. */ + ctx->coords = (float(*)[3])MEM_malloc_arrayN(sizeof(float[3]), num_items, __func__); + for (uint32_t index = 0; index < num_items; index++) { + ctx->coords[index][0] = (rng->get_float() - 0.5f) * 10; + ctx->coords[index][1] = (rng->get_float() - 0.5f) * 10; + ctx->coords[index][2] = (rng->get_float() - 0.5f) * 10; + } + IDType_ID_LT.init_data(&ctx->lattice.id); + strcpy(ctx->lattice.id.name, "LTLattice"); + IDType_ID_OB.init_data(&ctx->ob_lattice.id); + ctx->ob_lattice.type = OB_LATTICE; + ctx->ob_lattice.data = &ctx->lattice; + IDType_ID_OB.init_data(&ctx->ob_mesh.id); + IDType_ID_ME.init_data(&ctx->mesh.id); + ctx->ob_mesh.type = OB_MESH; + ctx->ob_mesh.data = &ctx->mesh; + + ctx->ldd = BKE_lattice_deform_data_create(&ctx->ob_lattice, &ctx->ob_mesh); +} + +static void test_lattice_deform(LatticeDeformTestContext *ctx, int32_t num_items) +{ + for (int i = 0; i < num_items; i++) { + float *co = &ctx->coords[i][0]; + BKE_lattice_deform_data_eval_co(ctx->ldd, co, 1.0f); + } +} + +static void test_lattice_deform_free(LatticeDeformTestContext *ctx) +{ + BKE_lattice_deform_data_destroy(ctx->ldd); + MEM_freeN(ctx->coords); + IDType_ID_LT.free_data(&ctx->lattice.id); + IDType_ID_OB.free_data(&ctx->ob_lattice.id); + IDType_ID_OB.free_data(&ctx->ob_mesh.id); + IDType_ID_ME.free_data(&ctx->mesh.id); +} + +static void lattice_deform_benchmark(benchmark::State &state) { + for (auto _ : state) { + const int32_t num_items = state.range(0); + LatticeDeformTestContext ctx = {{{nullptr}}}; + RandomNumberGenerator rng; + test_lattice_deform_init(&ctx, &rng, num_items); + test_lattice_deform(&ctx, num_items); + test_lattice_deform_free(&ctx); + } +} + +BENCHMARK(lattice_deform_benchmark)->RangeMultiplier(10)->Range(1000, 10000000); + +} diff --git a/tests/gbenchmark/CMakeLists.txt b/tests/gbenchmark/CMakeLists.txt index 1670e9af7e6..78a73cc701f 100644 --- a/tests/gbenchmark/CMakeLists.txt +++ b/tests/gbenchmark/CMakeLists.txt @@ -1,4 +1,7 @@ -if(WITH_BENCHMARK_TESTS) +# Otherwise we get warnings here that we cant fix in external projects +remove_strict_flags() + +if(WITH_BENCHMARK_TESTS AND NOT (WITH_BUILDINFO)) set(INC . ../../extern/gbenchmark/include/ @@ -16,5 +19,9 @@ if(WITH_BENCHMARK_TESTS) extern_gbenchmark ) - blender_add_test_executable(benchmark "${SRC}" "${INC}" "${INC_SYS}" "${LIB}") + # TODO: check on none linux platforms. + get_property(_test_libs GLOBAL PROPERTY BLENDER_BENCHMARK_LIBS) + list(APPEND BENCHMARK_LIBS "-Wl,--whole-archive" ${_test_libs} "-Wl,--no-whole-archive") + + blender_add_test_executable(blender_benchmark "${SRC}" "${INC}" "${INC_SYS}" "${LIB};${BENCHMARK_LIBS}") endif() diff --git a/tests/gbenchmark/gbenchmark_test.cc b/tests/gbenchmark/gbenchmark_test.cc index 9884d9611f7..15a9b094c0a 100644 --- a/tests/gbenchmark/gbenchmark_test.cc +++ b/tests/gbenchmark/gbenchmark_test.cc @@ -1,18 +1,3 @@ -#include <benchmark/benchmark.h> - -static void BM_StringCreation(benchmark::State &state) -{ - for (auto _ : state) - std::string empty_string; -} -// Register the function as a benchmark -BENCHMARK(BM_StringCreation); - -// Define another benchmark -static void BM_StringCopy(benchmark::State &state) -{ - std::string x = "hello"; - for (auto _ : state) - std::string copy(x); -} -BENCHMARK(BM_StringCopy); +/* + * File is left blank intentionally as a single source file is needed to compile a library. + */ \ No newline at end of file _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org https://lists.blender.org/mailman/listinfo/bf-blender-cvs