Skip to content

Commit

Permalink
Added the local_testing_datadir/ directory to gitignore, which is mad…
Browse files Browse the repository at this point in the history
…e during jenkins build script setup. Modified a tap Makefile to include curl path to resolve compilation error when compiling tap tests. Added tap test file 'test_admin_stats-t.cpp' for testing the ProxySQL_Statistics code changes, table definitions, and expected results.
  • Loading branch information
jaredev committed Oct 29, 2021
1 parent 0ef478e commit e041be5
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,5 @@ heaptrack.*
test/tap/tests/galera_1_timeout_count
test/tap/tests/galera_2_timeout_no_count
test/tap/tests/setparser_test

local_testing_datadir/
5 changes: 4 additions & 1 deletion test/tap/tap/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ JSON_IDIR=$(DEPS_PATH)/json
MARIADB_PATH=$(DEPS_PATH)/mariadb-client-library/mariadb_client
MARIADB_IDIR=$(MARIADB_PATH)/include
MARIADB_LDIR=$(MARIADB_PATH)/libmariadb
CURL_DIR=$(DEPS_PATH)/curl/curl
CURL_IDIR=$(CURL_DIR)/include
CURL_LDIR=$(CURL_DIR)/lib/.libs

.PHONY: all
all: libtap.a
Expand All @@ -18,6 +21,6 @@ debug: OPT = -O0 -DDEBUG -ggdb
debug: libtap.a

libtap.a: tap.cpp tap.h command_line.cpp command_line.h utils.cpp utils.h
g++ -c tap.cpp command_line.cpp utils.cpp -std=c++11 -I$(JSON_IDIR) -I$(MARIADB_IDIR) $(OPT)
g++ -c tap.cpp command_line.cpp utils.cpp -std=c++11 -I$(JSON_IDIR) -I$(MARIADB_IDIR) -I${CURL_IDIR} $(OPT)
ar rcs libtap.a tap.o command_line.o utils.o

180 changes: 180 additions & 0 deletions test/tap/tests/test_admin_stats-t.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/**
* @file test_admin_stats-t.cpp
* @brief This tests the Statistics module and its lookup code and tables.
* @date 2021-10-28
*/

#include <algorithm>
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <vector>

#include <mysql.h>
#include <mysql/mysqld_error.h>

#include "tap.h"
#include "command_line.h"
#include "utils.h"

using std::string;

int main(int argc, char** argv) {
CommandLine cl;

if (cl.getEnv()) {
diag("Failed to get the required environmental variables.");
return -1;
}

/** @brief Minimum number of distinct variable_name strings in the history_mysql_status_variables_lookup table */
const int min_distinct_variable_names = 50;

plan(5);

MYSQL* proxysql_admin = mysql_init(NULL);

// Initialize connections
if (!proxysql_admin) {
fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql_admin));
return -1;
}

// Connnect to local proxysql
if (!mysql_real_connect(proxysql_admin, cl.host, cl.admin_username, cl.admin_password, NULL, cl.admin_port, NULL, 0)) {
fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql_admin));
return -1;
}

// Setup the interval of how often new status entries are created
uint16_t new_stats_interval_sec = 2;

// @TODO: run command to set the interval

// If on a fresh install, wait long enough for the first run of stats to be created
// The lookup table will be empty until the first run!
sleep(new_stats_interval_sec * 2);

// Test 1: Lookup table has at least 50 rows
int64_t lookup_row_count = 0;
MYSQL_QUERY(proxysql_admin, "SELECT COUNT(*) FROM history_mysql_status_variables_lookup");
MYSQL_RES* result = mysql_store_result(proxysql_admin);
MYSQL_ROW row = mysql_fetch_row(result);

if (row[0])
lookup_row_count = strtoll(row[0], nullptr, 10);

mysql_free_result(result);

ok(
lookup_row_count >= 50,
"Lookup table 'history_mysql_status_variables_lookup' has at least 50 rows. %lu rows found.",
lookup_row_count
);

// Test 2: Distinct timestamps in
int64_t distinct_timestamp_count = 0;
MYSQL_QUERY(proxysql_admin, "SELECT COUNT(DISTINCT(timestamp)) FROM history_mysql_status_variables");
result = mysql_store_result(proxysql_admin);
row = mysql_fetch_row(result);

if (row[0])
distinct_timestamp_count = strtoll(row[0], nullptr, 10);

mysql_free_result(result);

ok(
distinct_timestamp_count >= 2,
"History table 'history_mysql_status_variables' has at least 2 distinct timestamps. %lu distinct timestamps found.",
distinct_timestamp_count
);

// Test 3: Matching distinct variable_id counts in lookup and history table
int64_t distinct_var_ids_in_history = 0;
int64_t distinct_var_ids_in_lookup = 0;

MYSQL_QUERY(proxysql_admin, "SELECT COUNT(DISTINCT(variable_id)) from history_mysql_status_variables_lookup");
result = mysql_store_result(proxysql_admin);
row = mysql_fetch_row(result);

if (row[0])
distinct_var_ids_in_lookup = strtoll(row[0], nullptr, 10);

mysql_free_result(result);

MYSQL_QUERY(proxysql_admin, "SELECT COUNT(DISTINCT(variable_id)) from history_mysql_status_variables");
result = mysql_store_result(proxysql_admin);
row = mysql_fetch_row(result);

if (row[0])
distinct_var_ids_in_history = strtoll(row[0], nullptr, 10);

mysql_free_result(result);

ok(
distinct_var_ids_in_history == distinct_var_ids_in_lookup,
"Distinct variable_id count matches in history and lookup tables. History:%lu, Lookup:%lu",
distinct_var_ids_in_history,
distinct_var_ids_in_lookup
);

// Test 4: Check that for each variable_id has same number of rows in history table

// @note: As the CI tests are done on a fresh install, these should match in this instance
// In practice, they could differ if new metrics variables are added.

std::vector<int64_t> rows_per_var_id;
time_t two_mins_ago = time(nullptr) - 60*2;
string query = "SELECT variable_id, COUNT(*) FROM history_mysql_status_variables WHERE timestamp < " + std::to_string(two_mins_ago) + " GROUP BY variable_id";
MYSQL_QUERY(proxysql_admin, query.c_str());
result = mysql_store_result(proxysql_admin);

for (int i = 0; i < result->row_count; i++) {
row = mysql_fetch_row(result);
rows_per_var_id.push_back(strtoll(row[0], nullptr, 10));
}

mysql_free_result(result);

bool each_var_row_count_equal = std::adjacent_find(rows_per_var_id.begin(), rows_per_var_id.end(), std::not_equal_to<int64_t>()) == rows_per_var_id.end(); // no adjacent unequal values found

ok(
each_var_row_count_equal,
"Each variable_id in the history table has the same number of rows."
);

// Test 5: Number of rows in history table increases after connections stat changes
int64_t history_rows_before = 0;
int64_t history_rows_after = 0;

MYSQL_QUERY(proxysql_admin, "SELECT COUNT(*) FROM history_mysql_status_variables");
result = mysql_store_result(proxysql_admin);
row = mysql_fetch_row(result);

if (row[0])
history_rows_before = strtoll(row[0], nullptr, 10);

mysql_free_result(result);

MYSQL_QUERY(proxysql_admin, "SET admin-stats_mysql_connections=10");

sleep(new_stats_interval_sec + 1); // give it time to insert next round of stats

MYSQL_QUERY(proxysql_admin, "SELECT COUNT(*) FROM history_mysql_status_variables");
result = mysql_store_result(proxysql_admin);
row = mysql_fetch_row(result);

if (row[0])
history_rows_after = strtoll(row[0], nullptr, 10);

mysql_free_result(result);

ok(
history_rows_before < history_rows_after,
"Number of rows in history table increases after connections stat changes. Before: %lu After: %lu",
history_rows_before,
history_rows_after
);

return exit_status();
}

0 comments on commit e041be5

Please sign in to comment.