From 9a3672537a208deeb80875f33e98b890b955947b Mon Sep 17 00:00:00 2001 From: Joel Anderson Date: Sat, 21 Oct 2023 08:07:17 -0400 Subject: [PATCH] add performance test for sqlite3 --- include/test/helper/memory_counter.hpp | 12 ++++- test/performance/target/function.cpp | 6 +-- test/performance/target/sqlite3.cpp | 60 +++++++++++++++++++++++ tools/check_headers/stumpless_private.yml | 1 + tools/cmake/sqlite3.cmake | 6 +++ 5 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 test/performance/target/sqlite3.cpp diff --git a/include/test/helper/memory_counter.hpp b/include/test/helper/memory_counter.hpp index 6270cb388..5a0732238 100644 --- a/include/test/helper/memory_counter.hpp +++ b/include/test/helper/memory_counter.hpp @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: Apache-2.0 */ /* - * Copyright 2018-2021 Joel E. Anderson + * Copyright 2018-2023 Joel E. Anderson * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,6 +30,9 @@ struct memory_counter { size_t realloc_count; size_t free_count; size_t free_total; + void * ( *previous_malloc )( size_t ); + void * ( *previous_realloc )( void *, size_t ); + void ( *previous_free )( void * ); }; #define INIT_MEMORY_COUNTER(PREFIX) \ @@ -38,10 +41,17 @@ PREFIX##_memory_counter.alloc_total = 0; \ PREFIX##_memory_counter.realloc_count = 0; \ PREFIX##_memory_counter.free_count = 0; \ PREFIX##_memory_counter.free_total = 0; \ +PREFIX##_memory_counter.previous_malloc = malloc; \ +PREFIX##_memory_counter.previous_realloc = realloc; \ +PREFIX##_memory_counter.previous_free = free; \ stumpless_set_malloc( PREFIX##_memory_counter_malloc ); \ stumpless_set_realloc( PREFIX##_memory_counter_realloc ); \ stumpless_set_free( PREFIX##_memory_counter_free ); +#define FINALIZE_MEMORY_COUNTER(PREFIX) \ +stumpless_set_malloc( PREFIX##_memory_counter.previous_malloc ); \ +stumpless_set_realloc( PREFIX##_memory_counter.previous_realloc ); \ +stumpless_set_free( PREFIX##_memory_counter.previous_free ); #define NEW_MEMORY_COUNTER(PREFIX) \ static struct memory_counter PREFIX##_memory_counter; \ diff --git a/test/performance/target/function.cpp b/test/performance/target/function.cpp index 4e5ec9c7c..061ffe687 100644 --- a/test/performance/target/function.cpp +++ b/test/performance/target/function.cpp @@ -1,7 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 /* - * Copyright 2021 Joel E. Anderson + * Copyright 2021-2023 Joel E. Anderson * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,9 +46,7 @@ class FunctionFixture : public::benchmark::Fixture { } void TearDown( const ::benchmark::State &state ) { - stumpless_set_malloc( malloc ); - stumpless_set_realloc( realloc ); - stumpless_set_free( free ); + FINALIZE_MEMORY_COUNTER( function ); stumpless_destroy_entry_and_contents( entry ); stumpless_close_function_target( target ); stumpless_free_all( ); diff --git a/test/performance/target/sqlite3.cpp b/test/performance/target/sqlite3.cpp new file mode 100644 index 000000000..412bd6b57 --- /dev/null +++ b/test/performance/target/sqlite3.cpp @@ -0,0 +1,60 @@ +// SPDX-License-Identifier: Apache-2.0 + +/* + * Copyright 2023 Joel E. Anderson + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include "test/helper/fixture.hpp" +#include "test/helper/memory_counter.hpp" + +NEW_MEMORY_COUNTER( sqlite3 ); + +class Sqlite3Fixture : public::benchmark::Fixture { +protected: + struct stumpless_target *target; + struct stumpless_entry *entry; + +public: + const char *db_filename = "test_performance.sqlite3"; + + void SetUp( const ::benchmark::State &state ) { + remove( db_filename ); + target = stumpless_open_sqlite3_target( db_filename ); + stumpless_create_default_sqlite3_table( target ); + entry = create_entry(); + INIT_MEMORY_COUNTER( sqlite3 ); + } + + void TearDown( const ::benchmark::State &state ) { + FINALIZE_MEMORY_COUNTER( sqlite3 ); + stumpless_destroy_entry_and_contents( entry ); + stumpless_close_sqlite3_target( target ); + remove( db_filename ); + stumpless_free_all(); + } +}; + +BENCHMARK_F( Sqlite3Fixture, AddEntry )( benchmark::State &state ) { + for( auto _ : state ) { + if( stumpless_add_entry( target, entry ) < 0 ) { + state.SkipWithError( "could not send an entry" ); + } + } + + SET_STATE_COUNTERS( state, sqlite3 ); +} diff --git a/tools/check_headers/stumpless_private.yml b/tools/check_headers/stumpless_private.yml index 5d34d9554..31a4150fb 100644 --- a/tools/check_headers/stumpless_private.yml +++ b/tools/check_headers/stumpless_private.yml @@ -56,6 +56,7 @@ "create_empty_entry": "test/helper/fixture.hpp" "destroy_sqlite3_target": "private/target/sqlite3.h" "fallback_copy_wstring_to_cstring": "private/config/fallback.h" +"FINALIZE_MEMORY_COUNTER": "test/helper/memory_counter.hpp" "FOR_EACH_PARAM_WITH_NAME": "private/element.h" "FUZZ_CORPORA_DIR": "test/config.hpp" "GENERATE_STRING": "private/strhelper.h" diff --git a/tools/cmake/sqlite3.cmake b/tools/cmake/sqlite3.cmake index 576e7b5a1..882ce377f 100644 --- a/tools/cmake/sqlite3.cmake +++ b/tools/cmake/sqlite3.cmake @@ -25,6 +25,12 @@ add_function_test(sqlite3 $ ) +add_performance_test(sqlite3 + SOURCES + test/performance/target/sqlite3.cpp + $ +) + add_thread_safety_test(sqlite3 SOURCES test/thread_safety/target/sqlite3.cpp