Skip to content

Commit

Permalink
add performance test for sqlite3
Browse files Browse the repository at this point in the history
  • Loading branch information
goatshriek committed Oct 21, 2023
1 parent 8dc3991 commit 9a36725
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
12 changes: 11 additions & 1 deletion include/test/helper/memory_counter.hpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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) \
Expand All @@ -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; \
Expand Down
6 changes: 2 additions & 4 deletions test/performance/target/function.cpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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( );
Expand Down
60 changes: 60 additions & 0 deletions test/performance/target/sqlite3.cpp
Original file line number Diff line number Diff line change
@@ -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 <benchmark/benchmark.h>
#include <cstdlib>
#include <stumpless.h>
#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 );
}
1 change: 1 addition & 0 deletions tools/check_headers/stumpless_private.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions tools/cmake/sqlite3.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ add_function_test(sqlite3
$<TARGET_OBJECTS:test_helper_fixture>
)

add_performance_test(sqlite3
SOURCES
test/performance/target/sqlite3.cpp
$<TARGET_OBJECTS:test_helper_fixture>
)

add_thread_safety_test(sqlite3
SOURCES
test/thread_safety/target/sqlite3.cpp
Expand Down

0 comments on commit 9a36725

Please sign in to comment.