Skip to content

Commit

Permalink
Tests: Test for expected SQLite3 cache size
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Nov 17, 2022
1 parent 4ed7397 commit d10887d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
# SQLITE_DQS=0: This setting disables the double-quoted string literal misfeature.
# SQLITE_ENABLE_DBPAGE_VTAB: Enables the SQLITE_DBPAGE virtual table. Warning: writing to the SQLITE_DBPAGE virtual table can very easily cause unrecoverably database corruption.
# SQLITE_OMIT_DESERIALIZE: This option causes the the sqlite3_serialize() and sqlite3_deserialize() interfaces to be omitted from the build (was the default before 3.36.0)
# HAVE_READLINE: Enable readline support to allow easy editing, history and auto-completion7
# HAVE_READLINE: Enable readline support to allow easy editing, history and auto-completion
# SQLITE_DEFAULT_CACHE_SIZE=-16384: Allow up to 16 MiB of cache to be used by SQLite3 (default is 2000 kiB)
set(SQLITE_DEFINES "-DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_OMIT_DEPRECATED -DSQLITE_OMIT_PROGRESS_CALLBACK -DSQLITE_DEFAULT_FOREIGN_KEYS=1 -DSQLITE_DQS=0 -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_OMIT_DESERIALIZE -DHAVE_READLINE -DSQLITE_DEFAULT_CACHE_SIZE=-16384")

Expand Down
10 changes: 5 additions & 5 deletions src/database/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ sqlite3* _dbopen(bool create, const char *func, const int line, const char *file

// Print database cache size when DEBUG_DATABASE is set
if(config.debug & DEBUG_DATABASE)
print_cache_size(db);
print_cache_size("FTL", db);

return db;
}
Expand Down Expand Up @@ -665,11 +665,11 @@ static int get_cache_size(sqlite3 *db)
return result;
}

void print_cache_size(sqlite3 *db)
void print_cache_size(const char *name, sqlite3 *db)
{
const int cs = get_cache_size(db);
if(cs < 0)
logg("Gravity database cache size is %.1f MiB", -1.0*cs/1024);
logg("%s database cache size is %.1f MiB", name, -1.0*cs/1024);
else
logg("Gravity database cache size is %d", cs);
}
logg("%s database cache size is %d", name, cs);
}
2 changes: 1 addition & 1 deletion src/database/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void SQLite3LogCallback(void *pArg, int iErrCode, const char *zMsg);
long int get_max_query_ID(sqlite3 *db);
bool db_update_counters(sqlite3 *db, const int total, const int blocked);
const char *get_sqlite3_version(void);
void print_cache_size(sqlite3 *db);
void print_cache_size(const char *name, sqlite3 *db);

extern long int lastdbindex;
extern bool DBdeleteoldqueries;
Expand Down
2 changes: 1 addition & 1 deletion src/database/gravity-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ bool gravityDB_open(void)

// Print database cache size when DEBUG_DATABASE is set
if(config.debug & DEBUG_DATABASE)
print_cache_size(gravity_db);
print_cache_size("Gravity", gravity_db);

// Prepare audit statement
if(config.debug & DEBUG_DATABASE)
Expand Down
7 changes: 7 additions & 0 deletions test/test_suite.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1407,3 +1407,10 @@
[[ "$firstnum" == 7 ]]
[[ "$lastnum" == 7 ]]
}

@test "SQLite3 cache size as expected" {
# Get number of lines in the log before the test
cache_size_total_mentionings="$(grep -c "database cache size is" /var/log/pihole/FTL.log)"
cache_size_corect_mentionings="$(grep -c "database cache size is 16.0 MiB" /var/log/pihole/FTL.log)"
[[ "$cache_size_total_mentionings" == "$cache_size_corect_mentionings" ]]
}

0 comments on commit d10887d

Please sign in to comment.