PG-525: Update PGSM TAP Test Cases to accommodate PG15 changes.

Following changes are included in this commit:

1. Updated pgsm.pm to enable runtime loading of PG server version dependent
perl modules that are needed for TAP testing. Similarly removed unneeded
code from this file that is not needed right now.

2. Added generic settings and helper functions to pgsm.pm that could be used
across different test cases.

3.Updated following TAP test case to use pgsm.pm based global settings and helper
functions while making sure that we reduce the clutter and duplicate code in
test cases, where possible.

t/001_settings_default.pl
t/002_settings_pgsm_track_planning.pl
t/003_settings_pgms_extract_comments.pl
t/004_settings_pgsm_track.pl
t/005_settings_pgsm_enable_query_plan.pl
t/006_settings_pgsm_overflow_target.pl
t/007_settings_pgsm_query_shared_buffer.pl
t/008_settings_pgsm_histogram_buckets.pl
t/009_settings_pgsm_histogram_max.pl
t/010_settings_pgsm_histogram_min.pl
t/011_settings_pgsm_bucket_time.pl
t/012_settings_pgsm_max_buckets.pl
t/013_settings_pgsm_normalized_query.pl
t/014_settings_pgsm_track_utility.pl
t/015_settings_pgsm_query_max_len.pl
t/016_settings_pgsm_max.pl
t/017_execution_stats.pl
t/019_insufficient_shared_space.pl
t/020_buffer_overflow.pl
t/021_misc_1.pl
t/022_misc_2.pl
t/023_missing_queries.pl
t/024_check_timings.pl
t/025_compare_pgss.pl
t/026_shared_blocks.pl
t/027_local_blocks.pl
t/028_temp_block.pl

4. Removed following TAP test cases as these are no longer needed and are
covered by existing other test cases.

0001_settings_pgsm_track_planning.pl
0002_settings_pgsm_enable_query_plan.pl

5. Added more out files for histogram sql test cases to cover the behavior for
bucket_start_time and server versions.

regression/expected/histogram_3.out
regression/expected/histogram_4.out
regression/expected/histogram_5.out
regression/expected/histogram_6.out

6. Added following out file that is PG server version 15 specific.

t/expected/007_settings_pgsm_query_shared_buffer.out.15
pull/311/head
Naeem Akhter 2022-09-22 17:43:08 +05:00 committed by Hamid Akhtar
parent 0fe9908d5f
commit af2da8885a
35 changed files with 1114 additions and 1812 deletions

View File

@ -0,0 +1,73 @@
CREATE OR REPLACE FUNCTION generate_histogram()
RETURNS TABLE (
range TEXT, freq INT, bar TEXT
) AS $$
Declare
bucket_id integer;
query_id text;
BEGIN
select bucket into bucket_id from pg_stat_monitor order by calls desc limit 1;
select queryid into query_id from pg_stat_monitor order by calls desc limit 1;
--RAISE INFO 'bucket_id %', bucket_id;
--RAISE INFO 'query_id %', query_id;
return query
SELECT * FROM histogram(bucket_id, query_id) AS a(range TEXT, freq INT, bar TEXT);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION run_pg_sleep(INTEGER) RETURNS VOID AS $$
DECLARE
loops ALIAS FOR $1;
BEGIN
FOR i IN 1..loops LOOP
--RAISE INFO 'Current timestamp: %', timeofday()::TIMESTAMP;
RAISE INFO 'Sleep % seconds', i;
PERFORM pg_sleep(i);
END LOOP;
END;
$$ LANGUAGE 'plpgsql' STRICT;
CREATE EXTENSION pg_stat_monitor;
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
Set pg_stat_monitor.pgsm_track='all';
select run_pg_sleep(5);
INFO: Sleep 1 seconds
INFO: Sleep 2 seconds
INFO: Sleep 3 seconds
INFO: Sleep 4 seconds
INFO: Sleep 5 seconds
run_pg_sleep
--------------
(1 row)
SELECT substr(query, 0,50) as query, calls, resp_calls FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | calls | resp_calls
---------------------------------------------------+-------+-----------------------
SELECT pg_sleep(i) | 3 | {0,0,0,0,0,0,3,0,0,0}
SELECT pg_sleep(i) | 2 | {0,0,0,0,0,0,0,2,0,0}
SELECT pg_stat_monitor_reset() | 1 | {1,0,0,0,0,0,0,0,0,0}
SELECT substr(query, 0,50) as query, calls, resp_ | 1 | {1,0,0,0,0,0,0,0,0,0}
Set pg_stat_monitor.pgsm_track='all' | 1 | {1,0,0,0,0,0,0,0,0,0}
select run_pg_sleep(5) | 1 | {0,0,0,0,0,0,0,0,1,0}
(6 rows)
select * from generate_histogram();
range | freq | bar
--------------------+------+--------------------------------------------------------------------------------------------
(0 - 3)} | 0 |
(3 - 10)} | 0 |
(10 - 31)} | 0 |
(31 - 100)} | 0 |
(100 - 316)} | 0 |
(316 - 1000)} | 0 |
(1000 - 3162)} | 3 | ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
(3162 - 10000)} | 0 |
(10000 - 31622)} | 0 |
(31622 - 100000)} | 0 |
(10 rows)
DROP EXTENSION pg_stat_monitor;

View File

@ -0,0 +1,73 @@
CREATE OR REPLACE FUNCTION generate_histogram()
RETURNS TABLE (
range TEXT, freq INT, bar TEXT
) AS $$
Declare
bucket_id integer;
query_id text;
BEGIN
select bucket into bucket_id from pg_stat_monitor order by calls desc limit 1;
select queryid into query_id from pg_stat_monitor order by calls desc limit 1;
--RAISE INFO 'bucket_id %', bucket_id;
--RAISE INFO 'query_id %', query_id;
return query
SELECT * FROM histogram(bucket_id, query_id) AS a(range TEXT, freq INT, bar TEXT);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION run_pg_sleep(INTEGER) RETURNS VOID AS $$
DECLARE
loops ALIAS FOR $1;
BEGIN
FOR i IN 1..loops LOOP
--RAISE INFO 'Current timestamp: %', timeofday()::TIMESTAMP;
RAISE INFO 'Sleep % seconds', i;
PERFORM pg_sleep(i);
END LOOP;
END;
$$ LANGUAGE 'plpgsql' STRICT;
CREATE EXTENSION pg_stat_monitor;
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
Set pg_stat_monitor.pgsm_track='all';
select run_pg_sleep(5);
INFO: Sleep 1 seconds
INFO: Sleep 2 seconds
INFO: Sleep 3 seconds
INFO: Sleep 4 seconds
INFO: Sleep 5 seconds
run_pg_sleep
--------------
(1 row)
SELECT substr(query, 0,50) as query, calls, resp_calls FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | calls | resp_calls
---------------------------------------------------+-------+-----------------------
SELECT pg_sleep(i) | 3 | {0,0,0,0,0,0,3,0,0,0}
SELECT pg_sleep(i) | 2 | {0,0,0,0,0,0,0,2,0,0}
SELECT pg_stat_monitor_reset() | 1 | {1,0,0,0,0,0,0,0,0,0}
SELECT substr(query, 0,50) as query, calls, resp_ | 1 | {1,0,0,0,0,0,0,0,0,0}
Set pg_stat_monitor.pgsm_track='all' | 1 | {1,0,0,0,0,0,0,0,0,0}
select run_pg_sleep(5) | 1 | {0,0,0,0,0,0,0,0,1,0}
(6 rows)
select * from generate_histogram();
range | freq | bar
--------------------+------+--------------------------------
(0 - 3)} | 0 |
(3 - 10)} | 0 |
(10 - 31)} | 0 |
(31 - 100)} | 0 |
(100 - 316)} | 0 |
(316 - 1000)} | 0 |
(1000 - 3162)} | 3 | ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
(3162 - 10000)} | 0 |
(10000 - 31622)} | 0 |
(31622 - 100000)} | 0 |
(10 rows)
DROP EXTENSION pg_stat_monitor;

View File

@ -0,0 +1,72 @@
CREATE OR REPLACE FUNCTION generate_histogram()
RETURNS TABLE (
range TEXT, freq INT, bar TEXT
) AS $$
Declare
bucket_id integer;
query_id text;
BEGIN
select bucket into bucket_id from pg_stat_monitor order by calls desc limit 1;
select queryid into query_id from pg_stat_monitor order by calls desc limit 1;
--RAISE INFO 'bucket_id %', bucket_id;
--RAISE INFO 'query_id %', query_id;
return query
SELECT * FROM histogram(bucket_id, query_id) AS a(range TEXT, freq INT, bar TEXT);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION run_pg_sleep(INTEGER) RETURNS VOID AS $$
DECLARE
loops ALIAS FOR $1;
BEGIN
FOR i IN 1..loops LOOP
--RAISE INFO 'Current timestamp: %', timeofday()::TIMESTAMP;
RAISE INFO 'Sleep % seconds', i;
PERFORM pg_sleep(i);
END LOOP;
END;
$$ LANGUAGE 'plpgsql' STRICT;
CREATE EXTENSION pg_stat_monitor;
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
Set pg_stat_monitor.pgsm_track='all';
select run_pg_sleep(5);
INFO: Sleep 1 seconds
INFO: Sleep 2 seconds
INFO: Sleep 3 seconds
INFO: Sleep 4 seconds
INFO: Sleep 5 seconds
run_pg_sleep
--------------
(1 row)
SELECT substr(query, 0,50) as query, calls, resp_calls FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | calls | resp_calls
---------------------------------------------------+-------+-----------------------
SELECT pg_sleep(i) | 4 | {0,0,0,0,0,0,2,2,0,0}
SELECT pg_stat_monitor_reset() | 1 | {1,0,0,0,0,0,0,0,0,0}
SELECT substr(query, 0,50) as query, calls, resp_ | 1 | {1,0,0,0,0,0,0,0,0,0}
Set pg_stat_monitor.pgsm_track='all' | 1 | {1,0,0,0,0,0,0,0,0,0}
select run_pg_sleep(5) | 1 | {0,0,0,0,0,0,0,0,1,0}
(5 rows)
select * from generate_histogram();
range | freq | bar
--------------------+------+--------------------------------
(0 - 3)} | 0 |
(3 - 10)} | 0 |
(10 - 31)} | 0 |
(31 - 100)} | 0 |
(100 - 316)} | 0 |
(316 - 1000)} | 0 |
(1000 - 3162)} | 2 | ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
(3162 - 10000)} | 2 | ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
(10000 - 31622)} | 0 |
(31622 - 100000)} | 0 |
(10 rows)
DROP EXTENSION pg_stat_monitor;

View File

@ -0,0 +1,73 @@
CREATE OR REPLACE FUNCTION generate_histogram()
RETURNS TABLE (
range TEXT, freq INT, bar TEXT
) AS $$
Declare
bucket_id integer;
query_id text;
BEGIN
select bucket into bucket_id from pg_stat_monitor order by calls desc limit 1;
select queryid into query_id from pg_stat_monitor order by calls desc limit 1;
--RAISE INFO 'bucket_id %', bucket_id;
--RAISE INFO 'query_id %', query_id;
return query
SELECT * FROM histogram(bucket_id, query_id) AS a(range TEXT, freq INT, bar TEXT);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION run_pg_sleep(INTEGER) RETURNS VOID AS $$
DECLARE
loops ALIAS FOR $1;
BEGIN
FOR i IN 1..loops LOOP
--RAISE INFO 'Current timestamp: %', timeofday()::TIMESTAMP;
RAISE INFO 'Sleep % seconds', i;
PERFORM pg_sleep(i);
END LOOP;
END;
$$ LANGUAGE 'plpgsql' STRICT;
CREATE EXTENSION pg_stat_monitor;
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
Set pg_stat_monitor.pgsm_track='all';
select run_pg_sleep(5);
INFO: Sleep 1 seconds
INFO: Sleep 2 seconds
INFO: Sleep 3 seconds
INFO: Sleep 4 seconds
INFO: Sleep 5 seconds
run_pg_sleep
--------------
(1 row)
SELECT substr(query, 0,50) as query, calls, resp_calls FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | calls | resp_calls
---------------------------------------------------+-------+-----------------------
SELECT pg_sleep(i) | 2 | {0,0,0,0,0,0,2,0,0,0}
SELECT pg_sleep(i) | 3 | {0,0,0,0,0,0,1,2,0,0}
SELECT pg_stat_monitor_reset() | 1 | {1,0,0,0,0,0,0,0,0,0}
SELECT substr(query, 0,50) as query, calls, resp_ | 1 | {1,0,0,0,0,0,0,0,0,0}
Set pg_stat_monitor.pgsm_track='all' | 1 | {1,0,0,0,0,0,0,0,0,0}
select run_pg_sleep(5) | 1 | {0,0,0,0,0,0,0,0,1,0}
(6 rows)
select * from generate_histogram();
range | freq | bar
--------------------+------+--------------------------------
(0 - 3)} | 0 |
(3 - 10)} | 0 |
(10 - 31)} | 0 |
(31 - 100)} | 0 |
(100 - 316)} | 0 |
(316 - 1000)} | 0 |
(1000 - 3162)} | 1 | ■■■■■■■■■■■■■■■
(3162 - 10000)} | 2 | ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
(10000 - 31622)} | 0 |
(31622 - 100000)} | 0 |
(10 rows)
DROP EXTENSION pg_stat_monitor;

View File

@ -1,78 +0,0 @@
#!/usr/bin/perl
use String::Util qw(trim);
use File::Basename;
use File::Compare;
use String::Util qw(trim);
use Test::More;
use lib 't';
use pgsm;
# PG's major server version
open my $FH_PG_VERSION, '<', "${pgdata}/PG_VERSION";
my $major_version = trim(<$FH_PG_VERSION>);
close $FH_PG_VERSION;
sub check_value
{
my ($var, $postive, $expected, $val) = @_;
if ($postive)
{
is($expected, $val, "Checking $var...");
}
else
{
isnt($expected, $val, "Checking $var...");
}
}
sub do_testing
{
my ($expected, $postive) = @_;
my ($cmdret, $stdout, $stderr) = pgsm_psql_cmd('postgres', 'SELECT max(total_plan_time) FROM pg_stat_monitor;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
my $total_plan_time = trim($stdout);
($cmdret, $stdout, $stderr) = pgsm_psql_cmd('postgres', 'SELECT max(min_plan_time) FROM pg_stat_monitor;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
my $min_plan_time = trim($stdout);
($cmdret, $stdout, $stderr) = pgsm_psql_cmd('postgres', 'SELECT max(max_plan_time) FROM pg_stat_monitor;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
my $max_plan_time = trim($stdout);
($cmdret, $stdout, $stderr) = pgsm_psql_cmd('postgres', 'SELECT max(mean_plan_time) FROM pg_stat_monitor;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
my $mean_plan_time = trim($stdout);
($cmdret, $stdout, $stderr) = pgsm_psql_cmd('postgres', 'SELECT max(stddev_plan_time) FROM pg_stat_monitor;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
my $stddev_plan_time = trim($stdout);
check_value("pgsm_track_planning", $postive, $expected, $total_plan_time + $min_plan_time + $max_plan_time + $stddev_plan_time);
return ($cmdret, $stdout, $stderr);
}
sub do_regression
{
my ($postive, $expected, $set) = @_;
($cmdret, $stdout, $stderr) = pgsm_setup_pg_stat_monitor($set);
($cmdret, $stdout, $stderr) = pgsm_start_pg;
($cmdret, $stdout, $stderr) = pgsm_create_extension;
($cmdret, $stdout, $stderr) = do_testing($postive, $expected);
ok($cmdret == 0, "Checking final result ...");
($cmdret, $stdout, $stderr) = pgsm_drop_extension;
($cmdret, $stdout, $stderr) = pgsm_stop_pg;
}
($cmdret, $stdout, $stderr) = pgsm_init_pg;
SKIP:
{
skip "Server version is 12 or less", 1
if ($major_version <= 12);
do_regression(1, 0, "pg_stat_monitor.pgsm_track_planning = 'no'");
do_regression(0, 0, "pg_stat_monitor.pgsm_track_planning = 'yes'");
}
($cmdret, $stdout, $stderr) = done_testing();

View File

@ -1,54 +0,0 @@
#!/usr/bin/perl
use warnings;
use String::Util qw(trim);
use File::Basename;
use File::Compare;
use Test::More;
use lib 't';
use pgsm;
sub check_value
{
my ($var, $postive, $expected, $val) = @_;
trim($val);
if ($postive)
{
is($expected, $val, "Checking $var...");
}
else
{
isnt($expected, $val, "Checking $var...");
}
}
sub do_testing
{
my ($expected, $postive) = @_;
($cmdret, $stdout, $stderr) = pgsm_psql_cmd('postgres', "select * from pg_class;", extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
my ($cmdret, $stdout, $stderr) = pgsm_psql_cmd('postgres', "SELECT case WHEN query_plan LIKE 'Seq Scan on pg_class' THEN 1 ELSE 0 END FROM pg_stat_monitor WHERE query LIKE 'select * from pg_class';", extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
check_value("query_plan", $postive, $expected, $stdout);
return ($cmdret, $stdout, $stderr);
}
sub do_regression
{
my ($postive, $expected, $set) = @_;
($cmdret, $stdout, $stderr) = pgsm_setup_pg_stat_monitor($set);
($cmdret, $stdout, $stderr) = pgsm_start_pg;
($cmdret, $stdout, $stderr) = pgsm_create_extension;
($cmdret, $stdout, $stderr) = do_testing($postive, $expected);
ok($cmdret == 0, "Checking final result ...");
($cmdret, $stdout, $stderr) = pgsm_drop_extension;
($cmdret, $stdout, $stderr) = pgsm_stop_pg;
}
($cmdret, $stdout, $stderr) = pgsm_init_pg;
do_regression(1, 0, "pg_stat_monitor.pgsm_enable_query_plan = 'no'");
do_regression(1, 1, "pg_stat_monitor.pgsm_enable_query_plan = 'yes'");
($cmdret, $stdout, $stderr) = done_testing();

View File

@ -5,69 +5,17 @@ use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use File::Copy; use File::Copy;
use PostgresNode;
use String::Util qw(trim); use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# PG's major server version
open my $FH_PG_VERSION, '<', "${pgdata}/PG_VERSION";
my $major_version = trim(<$FH_PG_VERSION>);
close $FH_PG_VERSION;
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
my($filename,$extension) = split('.', $perlfilename);
print '$filename';
print '$extension';
$perlfilename =~ s/.pl//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
if ($major_version <= 12)
{
$expected_filename = "${expected_filename}.${major_version}";
}
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -81,44 +29,42 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select datname, substr(query,0,100) as query, calls from pg_stat_monitor order by datname, query, calls desc Limit 20;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select datname, substr(query,0,100) as query, calls from pg_stat_monitor order by datname, query, calls desc Limit 20;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Select XXX from pg_stat_monitor"); ok($cmdret == 0, "Select XXX from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;
# compare the expected and out file # compare the expected and out file
my $compare = compare($expected_filename_with_path, $out_filename_with_path); my $compare = PGSM->compare_results();
# Test/check if expected and result/out file match. If Yes, test passes. # Test/check if expected and result/out file match. If Yes, test passes.
is($compare,0,"Compare Files: $expected_filename_with_path and $out_filename_with_path match."); is($compare,0,"Compare Files: $PGSM::expected_filename_with_path and $PGSM::out_filename_with_path files.");
# Done testing for this testcase file. # Done testing for this testcase file.
done_testing(); done_testing();

View File

@ -2,63 +2,20 @@
use strict; use strict;
use warnings; use warnings;
use String::Util qw(trim);
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim); use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
my $dynamic_out_filename_with_path = "${results_folder}/${out_filename}.dynamic" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# PG's major server version
open my $FH_PG_VERSION, '<', "${pgdata}/PG_VERSION";
my $major_version = trim(<$FH_PG_VERSION>);
close $FH_PG_VERSION;
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -73,76 +30,74 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Run 'SELECT * from pg_stat_monitor_settings;' two times and dump output to out file # Run 'SELECT * from pg_stat_monitor_settings;' two times and dump output to out file
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name = 'pg_stat_monitor.pgsm_track_planning';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name = 'pg_stat_monitor.pgsm_track_planning';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name = 'pg_stat_monitor.pgsm_track_planning';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name = 'pg_stat_monitor.pgsm_track_planning';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
SKIP: SKIP:
{ {
skip "Server version is 12 or less", 1 skip "Server version is 12 or less", 1
if ($major_version <= 12); if ($PGSM::PG_MAJOR_VERSION <= 12);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT query, calls, total_plan_time, min_plan_time, max_plan_time, mean_plan_time, stddev_plan_time from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT query, calls, total_plan_time, min_plan_time, max_plan_time, mean_plan_time, stddev_plan_time from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Select from PGSM view"); ok($cmdret == 0, "Select from PGSM view");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Test: total_plan_time is not 0 # Test: total_plan_time is not 0
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (total_plan_time = 0) from pg_stat_monitor where calls = 2 ;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (total_plan_time = 0) from pg_stat_monitor where calls = 2;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
trim($stdout); trim($stdout);
is($stdout,'f',"Compare: total_plan_time is not 0)."); is($stdout,'f',"Compare: total_plan_time is not 0).");
# Test: min_plan_time is not 0 # Test: min_plan_time is not 0
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (min_plan_time = 0) from pg_stat_monitor where calls = 2 ;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (min_plan_time = 0) from pg_stat_monitor where calls = 2;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
trim($stdout); trim($stdout);
is($stdout,'f',"Compare: min_plan_time is not 0)."); is($stdout,'f',"Compare: min_plan_time is not 0).");
# Test: max_plan_time is not 0 # Test: max_plan_time is not 0
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (max_plan_time = 0) from pg_stat_monitor where calls = 2 ;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (max_plan_time = 0) from pg_stat_monitor where calls = 2;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
trim($stdout); trim($stdout);
is($stdout,'f',"Compare: max_plan_time is not 0)."); is($stdout,'f',"Compare: max_plan_time is not 0).");
# Test: mean_plan_time is not 0 # Test: mean_plan_time is not 0
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (mean_plan_time = 0) from pg_stat_monitor where calls = 2 ;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (mean_plan_time = 0) from pg_stat_monitor where calls = 2;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
trim($stdout); trim($stdout);
is($stdout,'f',"Compare: mean_plan_time is not 0)."); is($stdout,'f',"Compare: mean_plan_time is not 0).");
# Test: stddev_plan_time is not 0 # Test: stddev_plan_time is not 0
#($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (stddev_plan_time = 0) from pg_stat_monitor where calls = 2 ;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); #($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (stddev_plan_time = 0) from pg_stat_monitor where calls = 2;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
#trim($stdout); #trim($stdout);
#is($stdout,'f',"Compare: stddev_plan_time is not 0)."); #is($stdout,'f',"Compare: stddev_plan_time is not 0).");
# Test: total_plan_time = min_plan_time + max_plan_time # Test: total_plan_time = min_plan_time + max_plan_time
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (round(total_plan_time::numeric,3) = round(min_plan_time::numeric + max_plan_time::numeric,3)) from pg_stat_monitor where calls = 2 ;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (round(total_plan_time::numeric,3) = round(min_plan_time::numeric + max_plan_time::numeric,3)) from pg_stat_monitor where calls = 2;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
trim($stdout); trim($stdout);
is($stdout,'t',"Compare: (round(total_plan_time::numeric,3) = round(min_plan_time::numeric + max_plan_time::numeric,3))."); is($stdout,'t',"Compare: (round(total_plan_time::numeric,3) = round(min_plan_time::numeric + max_plan_time::numeric,3)).");
} }
# Test: mean_plan_time = total_plan_time/2 # Test: mean_plan_time = total_plan_time/2
#($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (round(mean_plan_time::numeric,3) = round((total_plan_time/2)::numeric,3)) from pg_stat_monitor where calls = 2 ;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); #($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (round(mean_plan_time::numeric,3) = round((total_plan_time/2)::numeric,3)) from pg_stat_monitor where calls = 2;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
#trim($stdout); #trim($stdout);
#is($stdout,'t',"Test mean_plan_time: (round(mean_plan_time::numeric,3) = round((total_plan_time/2)::numeric,3))."); #is($stdout,'t',"Test mean_plan_time: (round(mean_plan_time::numeric,3) = round((total_plan_time/2)::numeric,3)).");
# Test: stddev_plan_time = mean_plan_time - min_plan_time # Test: stddev_plan_time = mean_plan_time - min_plan_time
#($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (round(stddev_plan_time::numeric,3) = round(mean_plan_time::numeric - min_plan_time::numeric,3)) from pg_stat_monitor where calls = 2 ;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); #($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (round(stddev_plan_time::numeric,3) = round(mean_plan_time::numeric - min_plan_time::numeric,3)) from pg_stat_monitor where calls = 2;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
#trim($stdout); #trim($stdout);
#is($stdout,'t',"Compare mean_plan_time: (round(stddev_plan_time::numeric,3) = round(mean_plan_time::numeric - min_plan_time::numeric,3))."); #is($stdout,'t',"Compare mean_plan_time: (round(stddev_plan_time::numeric,3) = round(mean_plan_time::numeric - min_plan_time::numeric,3)).");
# Dump output to out file # Dump output to out file
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select substr(query, 0,100) as query, calls, total_plan_time, min_plan_time,max_plan_time,mean_plan_time,stddev_plan_time from pg_stat_monitor order by query;', extra_params => ['-a','-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select substr(query, 0,100) as query, calls, total_plan_time, min_plan_time,max_plan_time,mean_plan_time,stddev_plan_time from pg_stat_monitor order by query;', extra_params => ['-a','-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($dynamic_out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Disable pgsm_track_planning # Disable pgsm_track_planning
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_track_planning = 'no'\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_track_planning = 'no'\n");
@ -151,65 +106,65 @@ $node->restart();
# Dump output to out file # Dump output to out file
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Run 'SELECT * from pg_stat_monitor_settings;' two times and dump output to out file # Run 'SELECT * from pg_stat_monitor_settings;' two times and dump output to out file
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name = 'pg_stat_monitor.pgsm_track_planning';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name = 'pg_stat_monitor.pgsm_track_planning';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name = 'pg_stat_monitor.pgsm_track_planning';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name = 'pg_stat_monitor.pgsm_track_planning';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
SKIP: SKIP:
{ {
skip "Server version is 12 or less", 1 skip "Server version is 12 or less", 1
if ($major_version <= 12); if ($PGSM::PG_MAJOR_VERSION <= 12);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT query, calls, total_plan_time, min_plan_time, max_plan_time, mean_plan_time, stddev_plan_time from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT query, calls, total_plan_time, min_plan_time, max_plan_time, mean_plan_time, stddev_plan_time from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Select from PGSM view"); ok($cmdret == 0, "Select from PGSM view");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Test: total_plan_time is 0 # Test: total_plan_time is 0
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (total_plan_time = 0) from pg_stat_monitor where calls = 2 ;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (total_plan_time = 0) from pg_stat_monitor where calls = 2;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
trim($stdout); trim($stdout);
is($stdout,'t',"Compare: total_plan_time is 0)."); is($stdout,'t',"Compare: total_plan_time is 0).");
# Test: min_plan_time is 0 # Test: min_plan_time is 0
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (min_plan_time = 0) from pg_stat_monitor where calls = 2 ;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (min_plan_time = 0) from pg_stat_monitor where calls = 2;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
trim($stdout); trim($stdout);
is($stdout,'t',"Compare: min_plan_time is 0)."); is($stdout,'t',"Compare: min_plan_time is 0).");
# Test: max_plan_time is 0 # Test: max_plan_time is 0
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (max_plan_time = 0) from pg_stat_monitor where calls = 2 ;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (max_plan_time = 0) from pg_stat_monitor where calls = 2;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
trim($stdout); trim($stdout);
is($stdout,'t',"Compare: max_plan_time is 0)."); is($stdout,'t',"Compare: max_plan_time is 0).");
# Test: mean_plan_time is 0 # Test: mean_plan_time is 0
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (mean_plan_time = 0) from pg_stat_monitor where calls = 2 ;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (mean_plan_time = 0) from pg_stat_monitor where calls = 2;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
trim($stdout); trim($stdout);
is($stdout,'t',"Compare: mean_plan_time is 0)."); is($stdout,'t',"Compare: mean_plan_time is 0).");
# Test: stddev_plan_time is 0 # Test: stddev_plan_time is 0
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (stddev_plan_time = 0) from pg_stat_monitor where calls = 2 ;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (stddev_plan_time = 0) from pg_stat_monitor where calls = 2;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
trim($stdout); trim($stdout);
is($stdout,'t',"Compare: stddev_plan_time is 0)."); is($stdout,'t',"Compare: stddev_plan_time is 0).");
} }
# Dump output to out file # Dump output to out file
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select substr(query, 0,100) as query, calls, total_plan_time, min_plan_time,max_plan_time,mean_plan_time,stddev_plan_time from pg_stat_monitor order by query;', extra_params => ['-a','-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select substr(query, 0,100) as query, calls, total_plan_time, min_plan_time,max_plan_time,mean_plan_time,stddev_plan_time from pg_stat_monitor order by query;', extra_params => ['-a','-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Dump output to out file # Dump output to out file
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;

View File

@ -4,53 +4,18 @@ use strict;
use warnings; use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -65,55 +30,53 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name = 'pg_stat_monitor.pgsm_extract_comments';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name = 'pg_stat_monitor.pgsm_extract_comments';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT 1 AS num /* First comment */, 'John' as Name /* Second comment*/;", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT 1 AS num /* First comment */, 'John' as Name /* Second comment*/;", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT query, comments FROM pg_stat_monitor ORDER BY query COLLATE "C";', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT query, comments FROM pg_stat_monitor ORDER BY query COLLATE "C";', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_extract_comments = 'no'\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_extract_comments = 'no'\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name = 'pg_stat_monitor.pgsm_extract_comments';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name = 'pg_stat_monitor.pgsm_extract_comments';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT 1 AS num /* First comment */, 'John' as Name /* Second comment*/;", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT 1 AS num /* First comment */, 'John' as Name /* Second comment*/;", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT query, comments FROM pg_stat_monitor ORDER BY query COLLATE "C";', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT query, comments FROM pg_stat_monitor ORDER BY query COLLATE "C";', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;
# compare the expected and out file # compare the expected and out file
my $compare = compare($expected_filename_with_path, $out_filename_with_path); my $compare = PGSM->compare_results();
# Test/check if expected and result/out file match. If Yes, test passes. # Test/check if expected and result/out file match. If Yes, test passes.
is($compare,0,"Compare Files: $expected_filename_with_path and $out_filename_with_path match."); is($compare,0,"Compare Files: $PGSM::expected_filename_with_path and $PGSM::out_filename_with_path files.");
# Done testing for this testcase file. # Done testing for this testcase file.
done_testing(); done_testing();

View File

@ -4,53 +4,18 @@ use strict;
use warnings; use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -65,54 +30,51 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_track';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_track';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_track = 'all'\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_track = 'all'\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_track';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_track';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_track = 'top'\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_track = 'top'\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_track';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_track';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;
# compare the expected and out file # compare the expected and out file
my $compare = compare($expected_filename_with_path, $out_filename_with_path); my $compare = PGSM->compare_results();
# Test/check if expected and result/out file match. If Yes, test passes. # Test/check if expected and result/out file match. If Yes, test passes.
is($compare,0,"Compare Files: $expected_filename_with_path and $out_filename_with_path match."); is($compare,0,"Compare Files: $PGSM::expected_filename_with_path and $PGSM::out_filename_with_path files.");
# Done testing for this testcase file. # Done testing for this testcase file.
done_testing(); done_testing();

View File

@ -2,56 +2,20 @@
use strict; use strict;
use warnings; use warnings;
use String::Util qw(trim);
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -66,39 +30,37 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_enable_query_plan';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_enable_query_plan';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE TABLE TBL_0(key text primary key, txt_0 text, value_0 int);', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE TABLE TBL_0(key text primary key, txt_0 text, value_0 int);', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "INSERT INTO TBL_0(key, txt_0, value_0) VALUES('000000', '846930886', 1804289383);", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "INSERT INTO TBL_0(key, txt_0, value_0) VALUES('000000', '846930886', 1804289383);", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'UPDATE TBL_0 SET value_0 = 1681692777;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'UPDATE TBL_0 SET value_0 = 1681692777;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select substr(query, 0,50) as query, calls, query_plan from pg_stat_monitor order by query,calls;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select substr(query, 0,50) as query, calls, query_plan from pg_stat_monitor order by query,calls;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Test: planid is not 0 or empty # Test: planid is not 0 or empty
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select planid from pg_stat_monitor where calls = 2 ;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select planid from pg_stat_monitor where calls = 2;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
trim($stdout); trim($stdout);
isnt($stdout,'',"Test: planid should not be empty"); isnt($stdout,'',"Test: planid should not be empty");
ok(length($stdout) == 16, 'Length of planid is 16'); ok(length($stdout) == 16, 'Length of planid is 16');
@ -108,36 +70,36 @@ $node->restart();
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Drop TABLE TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Drop TABLE TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_enable_query_plan';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_enable_query_plan';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE TABLE TBL_0(key text primary key, txt_0 text, value_0 int);', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE TABLE TBL_0(key text primary key, txt_0 text, value_0 int);', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "INSERT INTO TBL_0(key, txt_0, value_0) VALUES('000000', '846930886', 1804289383);", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "INSERT INTO TBL_0(key, txt_0, value_0) VALUES('000000', '846930886', 1804289383);", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'UPDATE TBL_0 SET value_0 = 1681692777;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'UPDATE TBL_0 SET value_0 = 1681692777;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select substr(query, 0,50) as query, calls, query_plan from pg_stat_monitor order by query,calls;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select substr(query, 0,50) as query, calls, query_plan from pg_stat_monitor order by query,calls;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Test: planid is empty # Test: planid is empty
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select planid from pg_stat_monitor where calls = 2 ;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select planid from pg_stat_monitor where calls = 2;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
trim($stdout); trim($stdout);
is($stdout,'',"Test: planid should be empty"); is($stdout,'',"Test: planid should be empty");
ok(length($stdout) == 0, 'Length of planid is 0'); ok(length($stdout) == 0, 'Length of planid is 0');
@ -145,16 +107,16 @@ ok(length($stdout) == 0, 'Length of planid is 0');
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;
# compare the expected and out file # compare the expected and out file
my $compare = compare($expected_filename_with_path, $out_filename_with_path); my $compare = PGSM->compare_results();
# Test/check if expected and result/out file match. If Yes, test passes. # Test/check if expected and result/out file match. If Yes, test passes.
is($compare,0,"Compare Files: $expected_filename_with_path and $out_filename_with_path match."); is($compare,0,"Compare Files: $PGSM::expected_filename_with_path and $PGSM::out_filename_with_path files.");
# Done testing for this testcase file. # Done testing for this testcase file.
done_testing(); done_testing();

View File

@ -4,53 +4,18 @@ use strict;
use warnings; use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -65,43 +30,41 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_overflow_target';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_overflow_target';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_overflow_target = 1\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_overflow_target = 1\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_overflow_target';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_overflow_target';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;
# compare the expected and out file # compare the expected and out file
my $compare = compare($expected_filename_with_path, $out_filename_with_path); my $compare = PGSM->compare_results();
# Test/check if expected and result/out file match. If Yes, test passes. # Test/check if expected and result/out file match. If Yes, test passes.
is($compare,0,"Compare Files: $expected_filename_with_path and $out_filename_with_path match."); is($compare,0,"Compare Files: $PGSM::expected_filename_with_path and $PGSM::out_filename_with_path files.");
# Done testing for this testcase file. # Done testing for this testcase file.
done_testing(); done_testing();

View File

@ -4,54 +4,19 @@ use strict;
use warnings; use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
print $conf "shared_preload_libraries = 'pg_stat_monitor'\n"; print $conf "shared_preload_libraries = 'pg_stat_monitor'\n";
@ -67,103 +32,100 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_query_shared_buffer';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_query_shared_buffer';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Create example database and run pgbench init # Create example database and run pgbench init
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE database example;', extra_params => ['-a']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE database example;', extra_params => ['-a']);
print "cmdret $cmdret\n"; print "cmdret $cmdret\n";
ok($cmdret == 0, "Create Database example"); ok($cmdret == 0, "Create Database example");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
my $port = $node->port; my $port = $node->port;
print "port $port \n"; print "port $port \n";
my $out = system ("pgbench -i -s 100 -p $port example"); my $out = system ("pgbench -i -s 100 -p $port example");
print " out: $out \n" ; print " out: $out \n";
ok($cmdret == 0, "Perform pgbench init"); ok($cmdret == 0, "Perform pgbench init");
$out = system ("pgbench -c 10 -j 2 -t 1000 -p $port example"); $out = system ("pgbench -c 10 -j 2 -t 1000 -p $port example");
print " out: $out \n" ; print " out: $out \n";
ok($cmdret == 0, "Run pgbench"); ok($cmdret == 0, "Run pgbench");
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select datname, substr(query,0,150) as query, calls from pg_stat_monitor order by datname, query, calls desc Limit 20;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select datname, substr(query,0,150) as query, calls from pg_stat_monitor order by datname, query, calls desc Limit 20;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
print "cmdret $cmdret\n"; print "cmdret $cmdret\n";
ok($cmdret == 0, "Select XXX from pg_stat_monitor"); ok($cmdret == 0, "Select XXX from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_query_shared_buffer = 100\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_query_shared_buffer = 100\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_query_shared_buffer';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_query_shared_buffer';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$out = system ("pgbench -i -s 100 -p $port example"); $out = system ("pgbench -i -s 100 -p $port example");
print " out: $out \n" ; print " out: $out \n";
ok($cmdret == 0, "Perform pgbench init"); ok($cmdret == 0, "Perform pgbench init");
$out = system ("pgbench -c 10 -j 2 -t 1000 -p $port example"); $out = system ("pgbench -c 10 -j 2 -t 1000 -p $port example");
print " out: $out \n" ; print " out: $out \n";
ok($cmdret == 0, "Run pgbench"); ok($cmdret == 0, "Run pgbench");
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select datname, substr(query,0,150) as query, calls from pg_stat_monitor order by datname, query, calls desc Limit 20;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select datname, substr(query,0,150) as query, calls from pg_stat_monitor order by datname, query, calls desc Limit 20;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
print "cmdret $cmdret\n"; print "cmdret $cmdret\n";
ok($cmdret == 0, "Select XXX from pg_stat_monitor"); ok($cmdret == 0, "Select XXX from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_query_shared_buffer = 20\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_query_shared_buffer = 20\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_query_shared_buffer';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_query_shared_buffer';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$out = system ("pgbench -i -s 100 -p $port example"); $out = system ("pgbench -i -s 100 -p $port example");
print " out: $out \n" ; print " out: $out \n";
ok($cmdret == 0, "Perform pgbench init"); ok($cmdret == 0, "Perform pgbench init");
$out = system ("pgbench -c 10 -j 2 -t 1000 -p $port example"); $out = system ("pgbench -c 10 -j 2 -t 1000 -p $port example");
print " out: $out \n" ; print " out: $out \n";
ok($cmdret == 0, "Run pgbench"); ok($cmdret == 0, "Run pgbench");
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select datname, substr(query,0,150) as query, calls from pg_stat_monitor order by datname, query, calls desc Limit 20;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select datname, substr(query,0,150) as query, calls from pg_stat_monitor order by datname, query, calls desc Limit 20;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
print "cmdret $cmdret\n"; print "cmdret $cmdret\n";
ok($cmdret == 0, "Select XXX from pg_stat_monitor"); ok($cmdret == 0, "Select XXX from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;
# compare the expected and out file # compare the expected and out file
my $compare = compare($expected_filename_with_path, $out_filename_with_path); my $compare = PGSM->compare_results();
# Test/check if expected and result/out file match. If Yes, test passes. # Test/check if expected and result/out file match. If Yes, test passes.
is($compare,0,"Compare Files: $expected_filename_with_path and $out_filename_with_path match."); is($compare,0,"Compare Files: $PGSM::expected_filename_with_path and $PGSM::out_filename_with_path files.");
# Done testing for this testcase file. # Done testing for this testcase file.
done_testing(); done_testing();

View File

@ -4,53 +4,18 @@ use strict;
use warnings; use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -65,88 +30,85 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_buckets = 1000\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_buckets = 1000\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_buckets = 100\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_buckets = 100\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_buckets = 10\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_buckets = 10\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_buckets = 5\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_buckets = 5\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_buckets = 1\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_buckets = 1\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;
# compare the expected and out file # compare the expected and out file
my $compare = compare($expected_filename_with_path, $out_filename_with_path); my $compare = PGSM->compare_results();
# Test/check if expected and result/out file match. If Yes, test passes. # Test/check if expected and result/out file match. If Yes, test passes.
is($compare,0,"Compare Files: $expected_filename_with_path and $out_filename_with_path match."); is($compare,0,"Compare Files: $PGSM::expected_filename_with_path and $PGSM::out_filename_with_path files.");
# Done testing for this testcase file. # Done testing for this testcase file.
done_testing(); done_testing();

View File

@ -4,53 +4,18 @@ use strict;
use warnings; use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -65,66 +30,63 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_max';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_max';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_max = 10\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_max = 10\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_max';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_max';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_max = 100\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_max = 100\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_max';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_max';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_max = 1000\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_max = 1000\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_max';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_max';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;
# compare the expected and out file # compare the expected and out file
my $compare = compare($expected_filename_with_path, $out_filename_with_path); my $compare = PGSM->compare_results();
# Test/check if expected and result/out file match. If Yes, test passes. # Test/check if expected and result/out file match. If Yes, test passes.
is($compare,0,"Compare Files: $expected_filename_with_path and $out_filename_with_path match."); is($compare,0,"Compare Files: $PGSM::expected_filename_with_path and $PGSM::out_filename_with_path files.");
# Done testing for this testcase file. # Done testing for this testcase file.
done_testing(); done_testing();

View File

@ -4,53 +4,18 @@ use strict;
use warnings; use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -65,65 +30,63 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_min';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_min';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_min = 1000\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_min = 1000\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_min';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_min';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_min = 10000\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_min = 10000\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_min';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_min';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_min = 99999\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_histogram_min = 99999\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_min';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_histogram_min';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;
# compare the expected and out file # compare the expected and out file
my $compare = compare($expected_filename_with_path, $out_filename_with_path); my $compare = PGSM->compare_results();
# Test/check if expected and result/out file match. If Yes, test passes. # Test/check if expected and result/out file match. If Yes, test passes.
is($compare,0,"Compare Files: $expected_filename_with_path and $out_filename_with_path match."); is($compare,0,"Compare Files: $PGSM::expected_filename_with_path and $PGSM::out_filename_with_path files.");
# Done testing for this testcase file. # Done testing for this testcase file.
done_testing(); done_testing();

View File

@ -4,53 +4,18 @@ use strict;
use warnings; use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -65,88 +30,85 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_bucket_time';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_bucket_time';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_bucket_time = 1000\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_bucket_time = 1000\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_bucket_time';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_bucket_time';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_bucket_time = 100\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_bucket_time = 100\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_bucket_time';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_bucket_time';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_bucket_time = 60\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_bucket_time = 60\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_bucket_time';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_bucket_time';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_bucket_time = 1\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_bucket_time = 1\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_bucket_time';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_bucket_time';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_bucket_time = 0\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_bucket_time = 0\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_bucket_time';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_bucket_time';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;
# compare the expected and out file # compare the expected and out file
my $compare = compare($expected_filename_with_path, $out_filename_with_path); my $compare = PGSM->compare_results();
# Test/check if expected and result/out file match. If Yes, test passes. # Test/check if expected and result/out file match. If Yes, test passes.
is($compare,0,"Compare Files: $expected_filename_with_path and $out_filename_with_path match."); is($compare,0,"Compare Files: $PGSM::expected_filename_with_path and $PGSM::out_filename_with_path files.");
# Done testing for this testcase file. # Done testing for this testcase file.
done_testing(); done_testing();

View File

@ -4,53 +4,18 @@ use strict;
use warnings; use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -65,88 +30,85 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max_buckets = 2\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max_buckets = 2\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max_buckets = 5\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max_buckets = 5\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max_buckets = 10\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max_buckets = 10\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max_buckets = 11\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max_buckets = 11\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max_buckets = 0\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max_buckets = 0\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max_buckets';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;
# compare the expected and out file # compare the expected and out file
my $compare = compare($expected_filename_with_path, $out_filename_with_path); my $compare = PGSM->compare_results();
# Test/check if expected and result/out file match. If Yes, test passes. # Test/check if expected and result/out file match. If Yes, test passes.
is($compare,0,"Compare Files: $expected_filename_with_path and $out_filename_with_path match."); is($compare,0,"Compare Files: $PGSM::expected_filename_with_path and $PGSM::out_filename_with_path files.");
# Done testing for this testcase file. # Done testing for this testcase file.
done_testing(); done_testing();

View File

@ -4,59 +4,18 @@ use strict;
use warnings; use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim); use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# PG's major server version
open my $FH_PG_VERSION, '<', "${pgdata}/PG_VERSION";
my $major_version = trim(<$FH_PG_VERSION>);
close $FH_PG_VERSION;
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -71,85 +30,83 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_normalized_query';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_normalized_query';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE TABLE TBL_0(key text primary key, txt_0 text, value_0 int);', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE TABLE TBL_0(key text primary key, txt_0 text, value_0 int);', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "INSERT INTO TBL_0(key, txt_0, value_0) VALUES('000000', '846930886', 1804289383);", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "INSERT INTO TBL_0(key, txt_0, value_0) VALUES('000000', '846930886', 1804289383);", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'UPDATE TBL_0 SET value_0 = 1681692777;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'UPDATE TBL_0 SET value_0 = 1681692777;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select datname, substr(query,0,100) as query, calls from pg_stat_monitor order by datname, query, calls desc Limit 20;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select datname, substr(query,0,100) as query, calls from pg_stat_monitor order by datname, query, calls desc Limit 20;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Select XXX from pg_stat_monitor"); ok($cmdret == 0, "Select XXX from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_normalized_query = 'yes'\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_normalized_query = 'yes'\n");
$node->restart(); $node->restart();
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Drop TABLE TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Drop TABLE TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_normalized_query';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_normalized_query';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE TABLE TBL_0(key text primary key, txt_0 text, value_0 int);', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE TABLE TBL_0(key text primary key, txt_0 text, value_0 int);', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "INSERT INTO TBL_0(key, txt_0, value_0) VALUES('000000', '846930886', 1804289383);", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "INSERT INTO TBL_0(key, txt_0, value_0) VALUES('000000', '846930886', 1804289383);", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'UPDATE TBL_0 SET value_0 = 1681692777;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'UPDATE TBL_0 SET value_0 = 1681692777;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select datname, substr(query,0,100) as query, calls from pg_stat_monitor order by datname, query, calls desc Limit 20;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select datname, substr(query,0,100) as query, calls from pg_stat_monitor order by datname, query, calls desc Limit 20;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Select XXX from pg_stat_monitor"); ok($cmdret == 0, "Select XXX from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;
# compare the expected and out file # compare the expected and out file
my $compare = compare($expected_filename_with_path, $out_filename_with_path); my $compare = PGSM->compare_results();
# Test/check if expected and result/out file match. If Yes, test passes. # Test/check if expected and result/out file match. If Yes, test passes.
is($compare,0,"Compare Files: $expected_filename_with_path and $out_filename_with_path match."); is($compare,0,"Compare Files: $PGSM::expected_filename_with_path and $PGSM::out_filename_with_path files.");
# Done testing for this testcase file. # Done testing for this testcase file.
done_testing(); done_testing();

View File

@ -4,53 +4,18 @@ use strict;
use warnings; use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -65,94 +30,91 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_track_utility';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_track_utility';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE TABLE TBL_0(key text primary key, txt_0 text, value_0 int);', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE TABLE TBL_0(key text primary key, txt_0 text, value_0 int);', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "INSERT INTO TBL_0(key, txt_0, value_0) VALUES('000000', '846930886', 1804289383);", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "INSERT INTO TBL_0(key, txt_0, value_0) VALUES('000000', '846930886', 1804289383);", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'UPDATE TBL_0 SET value_0 = 1681692777;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'UPDATE TBL_0 SET value_0 = 1681692777;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Analyze TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Analyze TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'DROP TABLE TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'DROP TABLE TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select datname, substr(query,0,100) as query, calls from pg_stat_monitor order by datname, query, calls desc Limit 20;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select datname, substr(query,0,100) as query, calls from pg_stat_monitor order by datname, query, calls desc Limit 20;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Select XXX from pg_stat_monitor"); ok($cmdret == 0, "Select XXX from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_track_utility = 'yes'\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_track_utility = 'yes'\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_track_utility';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_track_utility';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE TABLE TBL_0(key text primary key, txt_0 text, value_0 int);', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE TABLE TBL_0(key text primary key, txt_0 text, value_0 int);', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "INSERT INTO TBL_0(key, txt_0, value_0) VALUES('000000', '846930886', 1804289383);", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "INSERT INTO TBL_0(key, txt_0, value_0) VALUES('000000', '846930886', 1804289383);", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT key, txt_0, value_0 FROM TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'UPDATE TBL_0 SET value_0 = 1681692777;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'UPDATE TBL_0 SET value_0 = 1681692777;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Analyze TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Analyze TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'DROP TABLE TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'DROP TABLE TBL_0;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select datname, substr(query,0,100) as query, calls from pg_stat_monitor order by datname, query, calls desc Limit 20;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select datname, substr(query,0,100) as query, calls from pg_stat_monitor order by datname, query, calls desc Limit 20;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Select XXX from pg_stat_monitor"); ok($cmdret == 0, "Select XXX from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;
# compare the expected and out file # compare the expected and out file
my $compare = compare($expected_filename_with_path, $out_filename_with_path); my $compare = PGSM->compare_results();
# Test/check if expected and result/out file match. If Yes, test passes. # Test/check if expected and result/out file match. If Yes, test passes.
is($compare,0,"Compare Files: $expected_filename_with_path and $out_filename_with_path match."); is($compare,0,"Compare Files: $PGSM::expected_filename_with_path and $PGSM::out_filename_with_path files.");
# Done testing for this testcase file. # Done testing for this testcase file.
done_testing(); done_testing();

View File

@ -4,53 +4,18 @@ use strict;
use warnings; use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -65,77 +30,74 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_query_max_len';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_query_max_len';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_query_max_len = 1024\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_query_max_len = 1024\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_query_max_len';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_query_max_len';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_query_max_len = 100\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_query_max_len = 100\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_query_max_len';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_query_max_len';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_query_max_len = 10\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_query_max_len = 10\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_query_max_len';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_query_max_len';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_query_max_len = 0\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_query_max_len = 0\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_query_max_len';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_query_max_len';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;
# compare the expected and out file # compare the expected and out file
my $compare = compare($expected_filename_with_path, $out_filename_with_path); my $compare = PGSM->compare_results();
# Test/check if expected and result/out file match. If Yes, test passes. # Test/check if expected and result/out file match. If Yes, test passes.
is($compare,0,"Compare Files: $expected_filename_with_path and $out_filename_with_path match."); is($compare,0,"Compare Files: $PGSM::expected_filename_with_path and $PGSM::out_filename_with_path files.");
# Done testing for this testcase file. # Done testing for this testcase file.
done_testing(); done_testing();

View File

@ -4,53 +4,18 @@ use strict;
use warnings; use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -65,77 +30,74 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max = 500\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max = 500\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max = 100\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max = 100\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max = 10\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max = 10\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max = 0\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max = 0\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_max';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;
# compare the expected and out file # compare the expected and out file
my $compare = compare($expected_filename_with_path, $out_filename_with_path); my $compare = PGSM->compare_results();
# Test/check if expected and result/out file match. If Yes, test passes. # Test/check if expected and result/out file match. If Yes, test passes.
is($compare,0,"Compare Files: $expected_filename_with_path and $out_filename_with_path match."); is($compare,0,"Compare Files: $PGSM::expected_filename_with_path and $PGSM::out_filename_with_path files.");
# Done testing for this testcase file. # Done testing for this testcase file.
done_testing(); done_testing();

View File

@ -2,63 +2,20 @@
use strict; use strict;
use warnings; use warnings;
use String::Util qw(trim);
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim); use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# PG's major server version
open my $FH_PG_VERSION, '<', "${pgdata}/PG_VERSION";
my $major_version = trim(<$FH_PG_VERSION>);
close $FH_PG_VERSION;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
my $dynamic_out_filename_with_path = "${results_folder}/${out_filename}.dynamic" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -75,7 +32,7 @@ my $col_max_time = "max_exec_time";
my $col_mean_time = "mean_exec_time"; my $col_mean_time = "mean_exec_time";
my $col_stddev_time = "stddev_exec_time"; my $col_stddev_time = "stddev_exec_time";
if ($major_version <= 12) if ($PGSM::PG_MAJOR_VERSION <= 12)
{ {
$col_total_time = "total_time"; $col_total_time = "total_time";
$col_min_time = "min_time"; $col_min_time = "min_time";
@ -87,81 +44,79 @@ if ($major_version <= 12)
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Run 'SELECT * from pg_stat_monitor_settings;' two times and dump output to out file # Run 'SELECT * from pg_stat_monitor_settings;' two times and dump output to out file
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT query, calls, ${col_total_time}, ${col_min_time}, ${col_max_time}, ${col_mean_time}, ${col_stddev_time} from pg_stat_monitor;", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT query, calls, ${col_total_time}, ${col_min_time}, ${col_max_time}, ${col_mean_time}, ${col_stddev_time} from pg_stat_monitor;", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Select from PGSM view"); ok($cmdret == 0, "Select from PGSM view");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Test: ${col_total_time} is not 0 # Test: ${col_total_time} is not 0
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT (${col_total_time} = 0) from pg_stat_monitor where calls = 2 ;", extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT (${col_total_time} = 0) from pg_stat_monitor where calls = 2;", extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
trim($stdout); trim($stdout);
is($stdout,'f',"Compare: ${col_total_time} is not 0)."); is($stdout,'f',"Compare: ${col_total_time} is not 0).");
# Test: ${col_min_time} is not 0 # Test: ${col_min_time} is not 0
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT (${col_min_time} = 0) from pg_stat_monitor where calls = 2 ;", extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT (${col_min_time} = 0) from pg_stat_monitor where calls = 2;", extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
trim($stdout); trim($stdout);
is($stdout,'f',"Compare: ${col_min_time} is not 0)."); is($stdout,'f',"Compare: ${col_min_time} is not 0).");
# Test: ${col_max_time} is not 0 # Test: ${col_max_time} is not 0
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT (${col_max_time} = 0) from pg_stat_monitor where calls = 2 ;", extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT (${col_max_time} = 0) from pg_stat_monitor where calls = 2;", extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
trim($stdout); trim($stdout);
is($stdout,'f',"Compare: ${col_max_time} is not 0)."); is($stdout,'f',"Compare: ${col_max_time} is not 0).");
# Test: ${col_mean_time} is not 0 # Test: ${col_mean_time} is not 0
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT (${col_mean_time} = 0) from pg_stat_monitor where calls = 2 ;", extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT (${col_mean_time} = 0) from pg_stat_monitor where calls = 2;", extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
trim($stdout); trim($stdout);
is($stdout,'f',"Compare: ${col_mean_time} is not 0)."); is($stdout,'f',"Compare: ${col_mean_time} is not 0).");
# Test: ${col_stddev_time} is not 0 # Test: ${col_stddev_time} is not 0
#($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT (${col_stddev_time} = 0) from pg_stat_monitor where calls = 2 ;", extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); #($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT (${col_stddev_time} = 0) from pg_stat_monitor where calls = 2;", extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
#trim($stdout); #trim($stdout);
#is($stdout,'f',"Test: ${col_stddev_time} should not be 0)."); #is($stdout,'f',"Test: ${col_stddev_time} should not be 0).");
# Test: ${col_total_time} = ${col_min_time} + ${col_max_time} # Test: ${col_total_time} = ${col_min_time} + ${col_max_time}
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT (round(${col_total_time}::numeric,3) = round(${col_min_time}::numeric + ${col_max_time}::numeric,3)) from pg_stat_monitor where calls = 2 ;", extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT (round(${col_total_time}::numeric,3) = round(${col_min_time}::numeric + ${col_max_time}::numeric,3)) from pg_stat_monitor where calls = 2;", extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
trim($stdout); trim($stdout);
is($stdout,'t',"Compare: (round(${col_total_time}::numeric,3) = round(${col_min_time}::numeric + ${col_max_time}::numeric,3))."); is($stdout,'t',"Compare: (round(${col_total_time}::numeric,3) = round(${col_min_time}::numeric + ${col_max_time}::numeric,3)).");
# Test: ${col_mean_time} = ${col_total_time}/2 # Test: ${col_mean_time} = ${col_total_time}/2
#($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (round(${col_mean_time}::numeric,3) = round((${col_total_time}/2)::numeric,3)) from pg_stat_monitor where calls = 2 ;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); #($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (round(${col_mean_time}::numeric,3) = round((${col_total_time}/2)::numeric,3)) from pg_stat_monitor where calls = 2;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
#trim($stdout); #trim($stdout);
#is($stdout,'t',"Compare ${col_mean_time}: (round(${col_mean_time}::numeric,3) = round((${col_total_time}/2)::numeric,3))."); #is($stdout,'t',"Compare ${col_mean_time}: (round(${col_mean_time}::numeric,3) = round((${col_total_time}/2)::numeric,3)).");
# Test: ${col_stddev_time} = ${col_mean_time} - ${col_min_time} # Test: ${col_stddev_time} = ${col_mean_time} - ${col_min_time}
#($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (round(${col_stddev_time}::numeric,3) = round(${col_mean_time}::numeric - ${col_min_time}::numeric,3)) from pg_stat_monitor where calls = 2 ;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); #($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select (round(${col_stddev_time}::numeric,3) = round(${col_mean_time}::numeric - ${col_min_time}::numeric,3)) from pg_stat_monitor where calls = 2;', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
#trim($stdout); #trim($stdout);
#is($stdout,'t',"Compare ${col_mean_time}: (round(${col_stddev_time}::numeric,3) = round(${col_mean_time}::numeric - ${col_min_time}::numeric,3))."); #is($stdout,'t',"Compare ${col_mean_time}: (round(${col_stddev_time}::numeric,3) = round(${col_mean_time}::numeric - ${col_min_time}::numeric,3)).");
# Dump output to out file # Dump output to out file
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT substr(query, 0,100) as query, calls, ${col_total_time}, ${col_min_time},${col_max_time},${col_mean_time},${col_stddev_time} from pg_stat_monitor order by query;", extra_params => ['-a','-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT substr(query, 0,100) as query, calls, ${col_total_time}, ${col_min_time},${col_max_time},${col_mean_time},${col_stddev_time} from pg_stat_monitor order by query;", extra_params => ['-a','-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($dynamic_out_filename_with_path, $stdout . "\n"); PGSM::append_to_debug_file($stdout);
# Dump output to out file # Dump output to out file
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;

View File

@ -2,56 +2,20 @@
use strict; use strict;
use warnings; use warnings;
use String::Util qw(trim);
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -68,63 +32,60 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'create table foo (id int generated by default as identity,col1 varchar(100) not null,primary key(id));', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'create table foo (id int generated by default as identity,col1 varchar(100) not null,primary key(id));', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Create Table foo"); ok($cmdret == 0, "Create Table foo");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_overflow_target';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_overflow_target';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', '\i scripts/data_1.sql' , extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', '\i scripts/data_1.sql' , extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Run sql file: scripts/data.sql"); ok($cmdret == 0, "Run sql file: scripts/data.sql");
#TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); #PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT substr(query, 0, 50), length(query), bucket, queryid, calls, elevel, sqlcode, message from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT substr(query, 0, 50), length(query), bucket, queryid, calls, elevel, sqlcode, message from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "SELECT substr(query, 0, 50), calls, message from pg_stat_monitor"); ok($cmdret == 0, "SELECT substr(query, 0, 50), calls, message from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT count(queryid) from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT count(queryid) from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "SELECT count(queryid) from pg_stat_monitor"); ok($cmdret == 0, "SELECT count(queryid) from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_overflow_target = 1\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_overflow_target = 1\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_overflow_target';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_overflow_target';", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', '\i scripts/data_2.sql' , extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', '\i scripts/data_2.sql' , extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Run sql file: scripts/data2.sql"); ok($cmdret == 0, "Run sql file: scripts/data2.sql");
#TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); #PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT substr(query, 0, 50), length(query), bucket, queryid, calls, elevel, sqlcode, message from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT substr(query, 0, 50), length(query), bucket, queryid, calls, elevel, sqlcode, message from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "SELECT substr(query, 0, 50), calls, message from pg_stat_monitor"); ok($cmdret == 0, "SELECT substr(query, 0, 50), calls, message from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT count(queryid) from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT count(queryid) from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "SELECT count(queryid) from pg_stat_monitor"); ok($cmdret == 0, "SELECT count(queryid) from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;

View File

@ -2,56 +2,20 @@
use strict; use strict;
use warnings; use warnings;
use String::Util qw(trim);
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -70,63 +34,61 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'create table foo (id int generated by default as identity,col1 varchar(100) not null,primary key(id));', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'create table foo (id int generated by default as identity,col1 varchar(100) not null,primary key(id));', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Create Table foo"); ok($cmdret == 0, "Create Table foo");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', '\i scripts/data_1.sql' , extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', '\i scripts/data_1.sql' , extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Run sql file: scripts/data.sql"); ok($cmdret == 0, "Run sql file: scripts/data.sql");
#TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); #PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT substr(query, 0, 50), length(query), bucket, queryid, calls, elevel, sqlcode, message from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT substr(query, 0, 50), length(query), bucket, queryid, calls, elevel, sqlcode, message from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "SELECT substr(query, 0, 50), calls, message from pg_stat_monitor"); ok($cmdret == 0, "SELECT substr(query, 0, 50), calls, message from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT count(queryid) from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT count(queryid) from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "SELECT count(queryid) from pg_stat_monitor"); ok($cmdret == 0, "SELECT count(queryid) from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_overflow_target = 1\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_overflow_target = 1\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', '\i scripts/data_2.sql' , extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', '\i scripts/data_2.sql' , extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Run sql file: scripts/data2.sql"); ok($cmdret == 0, "Run sql file: scripts/data2.sql");
#TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); #PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT substr(query, 0, 50), length(query), bucket, queryid, calls, elevel, sqlcode, message from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT substr(query, 0, 50), length(query), bucket, queryid, calls, elevel, sqlcode, message from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "SELECT substr(query, 0, 50), calls, message from pg_stat_monitor"); ok($cmdret == 0, "SELECT substr(query, 0, 50), calls, message from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT count(queryid) from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT count(queryid) from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "SELECT count(queryid) from pg_stat_monitor"); ok($cmdret == 0, "SELECT count(queryid) from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;

View File

@ -2,56 +2,20 @@
use strict; use strict;
use warnings; use warnings;
use String::Util qw(trim);
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -73,63 +37,61 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'create table foo (id int generated by default as identity,col1 varchar(100) not null,primary key(id));', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'create table foo (id int generated by default as identity,col1 varchar(100) not null,primary key(id));', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Create Table foo"); ok($cmdret == 0, "Create Table foo");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', '\i scripts/data_1.sql' , extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', '\i scripts/data_1.sql' , extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Run sql file: scripts/data.sql"); ok($cmdret == 0, "Run sql file: scripts/data.sql");
#TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); #PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT substr(query, 0, 50), length(query), bucket, queryid, calls, elevel, sqlcode, message from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT substr(query, 0, 50), length(query), bucket, queryid, calls, elevel, sqlcode, message from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "SELECT substr(query, 0, 50), calls, message from pg_stat_monitor"); ok($cmdret == 0, "SELECT substr(query, 0, 50), calls, message from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT count(queryid) from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT count(queryid) from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "SELECT count(queryid) from pg_stat_monitor"); ok($cmdret == 0, "SELECT count(queryid) from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_overflow_target = 1\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_overflow_target = 1\n");
$node->restart(); $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', '\i scripts/data_2.sql' , extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', '\i scripts/data_2.sql' , extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Run sql file: scripts/data2.sql"); ok($cmdret == 0, "Run sql file: scripts/data2.sql");
#TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); #PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT substr(query, 0, 50), length(query), bucket, queryid, calls, elevel, sqlcode, message from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT substr(query, 0, 50), length(query), bucket, queryid, calls, elevel, sqlcode, message from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "SELECT substr(query, 0, 50), calls, message from pg_stat_monitor"); ok($cmdret == 0, "SELECT substr(query, 0, 50), calls, message from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT count(queryid) from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT count(queryid) from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "SELECT count(queryid) from pg_stat_monitor"); ok($cmdret == 0, "SELECT count(queryid) from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;

View File

@ -2,56 +2,20 @@
use strict; use strict;
use warnings; use warnings;
use String::Util qw(trim);
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
open my $conf, '>>', "$pgdata/postgresql.conf"; open my $conf, '>>', "$pgdata/postgresql.conf";
@ -73,35 +37,33 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'create table foo (id int generated by default as identity,col1 varchar(100) not null,primary key(id));', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'create table foo (id int generated by default as identity,col1 varchar(100) not null,primary key(id));', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Create Table foo"); ok($cmdret == 0, "Create Table foo");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', '\i scripts/data_1.sql' , extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', '\i scripts/data_1.sql' , extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Run sql file: scripts/data.sql"); ok($cmdret == 0, "Run sql file: scripts/data.sql");
#TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); #PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT substr(query, 0, 50), length(query), bucket, queryid, calls, elevel, sqlcode, message from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT substr(query, 0, 50), length(query), bucket, queryid, calls, elevel, sqlcode, message from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "SELECT substr(query, 0, 50), calls, message from pg_stat_monitor"); ok($cmdret == 0, "SELECT substr(query, 0, 50), calls, message from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT count(queryid) from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT count(queryid) from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "SELECT count(queryid) from pg_stat_monitor"); ok($cmdret == 0, "SELECT count(queryid) from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max = 100\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_max = 100\n");
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_query_max_len = 1024\n"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_query_max_len = 1024\n");
@ -122,32 +84,32 @@ $node->restart();
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', '\i scripts/data_2.sql' , extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', '\i scripts/data_2.sql' , extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Run sql file: scripts/data2.sql"); ok($cmdret == 0, "Run sql file: scripts/data2.sql");
#TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); #PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT substr(query, 0, 50), length(query), bucket, queryid, calls, elevel, sqlcode, message from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT substr(query, 0, 50), length(query), bucket, queryid, calls, elevel, sqlcode, message from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "SELECT substr(query, 0, 50), calls, message from pg_stat_monitor"); ok($cmdret == 0, "SELECT substr(query, 0, 50), calls, message from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT count(queryid) from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT count(queryid) from pg_stat_monitor;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "SELECT count(queryid) from pg_stat_monitor"); ok($cmdret == 0, "SELECT count(queryid) from pg_stat_monitor");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;

View File

@ -34,14 +34,18 @@ use strict;
use warnings; use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Get filename and create out file name and dirs where requried
PGSM::setup_files_dir(basename($0));
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
$node->append_conf('postgresql.conf', "shared_preload_libraries = 'pg_stat_monitor'"); $node->append_conf('postgresql.conf', "shared_preload_libraries = 'pg_stat_monitor'");
# Set bucket duration to 14 seconds so tests don't take too long. # Set bucket duration to 14 seconds so tests don't take too long.
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_bucket_time = 14"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_bucket_time = 14");

View File

@ -1,68 +1,27 @@
#!/usr/bin/perl #!/usr/bin/perl
use strict; use strict;
use warnings; use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim); use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed if ($PGSM::PG_MAJOR_VERSION <= 12)
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
# Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test');
my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# PG's major server version
open my $FH_PG_VERSION, '<', "${pgdata}/PG_VERSION";
my $major_version = trim(<$FH_PG_VERSION>);
close $FH_PG_VERSION;
if ($major_version <= 12)
{ {
plan skip_all => "pg_stat_statements test cases for versions 12 and below."; plan skip_all => "pg_stat_monitor test cases for versions 12 and below.";
} }
#Remove .pl from filename and store in a variable # Create new PostgreSQL node and do initdb
$perlfilename =~ s/\.[^.]+$//; my $node = PGSM->pgsm_init_pg();
my $filename_without_extension = $perlfilename; my $pgdata = $node->data_dir;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
my $dynamic_out_filename_with_path = "${results_folder}/${out_filename}.dynamic" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
$node->append_conf('postgresql.conf', "shared_preload_libraries = 'pg_stat_statements,pg_stat_monitor'"); $node->append_conf('postgresql.conf', "shared_preload_libraries = 'pg_stat_statements,pg_stat_monitor'");
@ -79,36 +38,32 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_statements;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_statements;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSS Extension"); ok($cmdret == 0, "Create PGSS Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Create extension and change out file permissions # Create extension and change out file permissions
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_statements_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_statements_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSS Extension"); ok($cmdret == 0, "Reset PGSS Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Run 'SELECT * from pg_stat_monitor_settings;' two times and dump output to out file # Run 'SELECT * from pg_stat_monitor_settings;' two times and dump output to out file
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Create example database and run pgbench init # Create example database and run pgbench init
# ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE database example;', extra_params => ['-a']); # ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE database example;', extra_params => ['-a']);
# ok($cmdret == 0, "Create Database example"); # ok($cmdret == 0, "Create Database example");
# TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); # PGSM::append_to_file($stdout);
my $port = $node->port; my $port = $node->port;
@ -125,11 +80,11 @@ ok($cmdret == 0, "Run pgbench");
($cmdret, $stdout, $stderr) = $node->psql('postgres', "Delete from pgbench_accounts where aid % 2 = 1;", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "Delete from pgbench_accounts where aid % 2 = 1;", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select substr(query,0,130) as query, calls, rows, total_exec_time,min_exec_time,max_exec_time,mean_exec_time,stddev_exec_time from pg_stat_statements where query Like \'%bench%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select substr(query,0,130) as query, calls, rows, total_exec_time,min_exec_time,max_exec_time,mean_exec_time,stddev_exec_time from pg_stat_statements where query Like \'%bench%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($dynamic_out_filename_with_path, $stdout . "\n"); PGSM::append_to_debug_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select substr(query,0,130) as query, calls, rows_retrieved, total_exec_time, min_exec_time, max_exec_time, mean_exec_time,stddev_exec_time, cpu_user_time, cpu_sys_time from pg_stat_monitor where query Like \'%bench%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select substr(query,0,130) as query, calls, rows_retrieved, total_exec_time, min_exec_time, max_exec_time, mean_exec_time,stddev_exec_time, cpu_user_time, cpu_sys_time from pg_stat_monitor where query Like \'%bench%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($dynamic_out_filename_with_path, $stdout . "\n"); PGSM::append_to_debug_file($stdout);
TestLib::append_to_file($dynamic_out_filename_with_path, "\n\n"); PGSM::append_to_debug_file("--------------");
# Compare values for query 'Delete from pgbench_accounts where $1 = $2' # Compare values for query 'Delete from pgbench_accounts where $1 = $2'
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select PGSM.total_exec_time != 0 from pg_stat_monitor as PGSM where PGSM.query Like \'%Delete from pgbench_accounts%\';', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select PGSM.total_exec_time != 0 from pg_stat_monitor as PGSM where PGSM.query Like \'%Delete from pgbench_accounts%\';', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
@ -251,7 +206,7 @@ is($stdout,'t',"Check: cpu_sys_time should not be 0.");
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;

View File

@ -4,71 +4,29 @@ use strict;
use warnings; use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim); use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed if ($PGSM::PG_MAJOR_VERSION <= 12)
my $results_folder = "t/results"; {
plan skip_all => "pg_stat_monitor test cases for versions 12 and below.";
# Check if results folder exists or not, create if it doesn't }
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
my $dynamic_out_filename_with_path = "${results_folder}/${out_filename}.dynamic" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# PG's major server version
open my $FH_PG_VERSION, '<', "${pgdata}/PG_VERSION";
my $major_version = trim(<$FH_PG_VERSION>);
close $FH_PG_VERSION;
if ($major_version <= 12)
{
plan skip_all => "pg_stat_statements test cases for versions 12 and below.";
}
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
$node->append_conf('postgresql.conf', "shared_preload_libraries = 'pg_stat_statements,pg_stat_monitor'"); $node->append_conf('postgresql.conf', "shared_preload_libraries = 'pg_stat_statements,pg_stat_monitor'");
# Set bucket duration to 3600 seconds so bucket doesn't change. # Set bucket duration to 3600 seconds so bucket doesn't change.
$node->append_conf('postgresql.conf', "pg_stat_statements.track_utility = off"); $node->append_conf('postgresql.conf', "pg_stat_statements.track_utility = off");
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_bucket_time = 1800"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_bucket_time = 3600");
$node->append_conf('postgresql.conf', "track_io_timing = on"); $node->append_conf('postgresql.conf', "track_io_timing = on");
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_track_utility = no"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_track_utility = no");
$node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_normalized_query = yes"); $node->append_conf('postgresql.conf', "pg_stat_monitor.pgsm_normalized_query = yes");
@ -80,36 +38,32 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_statements;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_statements;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSS Extension"); ok($cmdret == 0, "Create PGSS Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Create extension and change out file permissions # Create extension and change out file permissions
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_statements_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_statements_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSS Extension"); ok($cmdret == 0, "Reset PGSS Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Run 'SELECT * from pg_stat_monitor_settings;' two times and dump output to out file # Run 'SELECT * from pg_stat_monitor_settings;' two times and dump output to out file
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Create example database and run pgbench init # Create example database and run pgbench init
# ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE database example;', extra_params => ['-a']); # ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE database example;', extra_params => ['-a']);
# ok($cmdret == 0, "Create Database example"); # ok($cmdret == 0, "Create Database example");
# TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); # PGSM::append_to_file($stdout);
my $port = $node->port; my $port = $node->port;
@ -126,23 +80,23 @@ ok($cmdret == 0, "Run pgbench");
($cmdret, $stdout, $stderr) = $node->psql('postgres', "Delete from pgbench_accounts where aid % 2 = 1;", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "Delete from pgbench_accounts where aid % 2 = 1;", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select queryid, substr(query,0,130) as query, calls, rows, total_exec_time,min_exec_time,max_exec_time,mean_exec_time from pg_stat_statements where query Like \'%bench%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select queryid, substr(query,0,130) as query, calls, rows, total_exec_time,min_exec_time,max_exec_time,mean_exec_time from pg_stat_statements where query Like \'%bench%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($dynamic_out_filename_with_path, $stdout . "\n"); PGSM::append_to_debug_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select client_ip, bucket, queryid, substr(query,0,130) as query, cmd_type_text, calls, rows_retrieved, total_exec_time, min_exec_time, max_exec_time, mean_exec_time, cpu_user_time, cpu_sys_time from pg_stat_monitor where query Like \'%bench%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select client_ip, bucket, queryid, substr(query,0,130) as query, cmd_type_text, calls, rows_retrieved, total_exec_time, min_exec_time, max_exec_time, mean_exec_time, cpu_user_time, cpu_sys_time from pg_stat_monitor where query Like \'%bench%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($dynamic_out_filename_with_path, $stdout . "\n"); PGSM::append_to_debug_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select substr(query,0,30) as query,calls,rows,shared_blks_hit,shared_blks_read,shared_blks_dirtied,shared_blks_written,local_blks_hit,local_blks_read,local_blks_dirtied,local_blks_written,temp_blks_read,temp_blks_written,blk_read_time,blk_write_time,wal_records,wal_fpi,wal_bytes from pg_stat_statements where query Like \'%bench%\' order by query,calls ;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select substr(query,0,30) as query,calls,rows,shared_blks_hit,shared_blks_read,shared_blks_dirtied,shared_blks_written,local_blks_hit,local_blks_read,local_blks_dirtied,local_blks_written,temp_blks_read,temp_blks_written,blk_read_time,blk_write_time,wal_records,wal_fpi,wal_bytes from pg_stat_statements where query Like \'%bench%\' order by query,calls;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($dynamic_out_filename_with_path, $stdout . "\n"); PGSM::append_to_debug_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select substr(query,0,30) as query,calls,rows_retrieved as rows, shared_blks_hit,shared_blks_read,shared_blks_dirtied,shared_blks_written,local_blks_hit,local_blks_read,local_blks_dirtied,local_blks_written,temp_blks_read,temp_blks_written,blk_read_time,blk_write_time,wal_records,wal_fpi,wal_bytes, cmd_type_text from pg_stat_monitor where query Like \'%bench%\' order by query,calls ;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select substr(query,0,30) as query,calls,rows_retrieved as rows, shared_blks_hit,shared_blks_read,shared_blks_dirtied,shared_blks_written,local_blks_hit,local_blks_read,local_blks_dirtied,local_blks_written,temp_blks_read,temp_blks_written,blk_read_time,blk_write_time,wal_records,wal_fpi,wal_bytes, cmd_type_text from pg_stat_monitor where query Like \'%bench%\' order by query,calls;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($dynamic_out_filename_with_path, $stdout . "\n"); PGSM::append_to_debug_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select substr(query,0,30),calls, rows, ROUND(total_exec_time::numeric,4) as total_exec_time, ROUND(min_exec_time::numeric,4) as min_exec_time, ROUND(max_exec_time::numeric,4) as max_exec_time, ROUND(mean_exec_time::numeric,4) as mean_exec_time, ROUND(stddev_exec_time::numeric,4) as stddev_exec_time, ROUND(blk_read_time::numeric,4) as blk_read_time, ROUND(blk_write_time::numeric,4) as blk_write_time, wal_records, wal_fpi, wal_bytes from pg_stat_statements where query Like \'%bench%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select substr(query,0,30),calls, rows, ROUND(total_exec_time::numeric,4) as total_exec_time, ROUND(min_exec_time::numeric,4) as min_exec_time, ROUND(max_exec_time::numeric,4) as max_exec_time, ROUND(mean_exec_time::numeric,4) as mean_exec_time, ROUND(stddev_exec_time::numeric,4) as stddev_exec_time, ROUND(blk_read_time::numeric,4) as blk_read_time, ROUND(blk_write_time::numeric,4) as blk_write_time, wal_records, wal_fpi, wal_bytes from pg_stat_statements where query Like \'%bench%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($dynamic_out_filename_with_path, $stdout . "\n"); PGSM::append_to_debug_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select substr(query,0,30), calls, rows_retrieved as rows, total_exec_time, min_exec_time, max_exec_time, mean_exec_time, stddev_exec_time, ROUND(blk_read_time::numeric,4) as blk_read_time, ROUND(blk_write_time::numeric,4) as blk_write_time, wal_records, wal_fpi, wal_bytes from pg_stat_monitor where query Like \'%bench%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'select substr(query,0,30), calls, rows_retrieved as rows, total_exec_time, min_exec_time, max_exec_time, mean_exec_time, stddev_exec_time, ROUND(blk_read_time::numeric,4) as blk_read_time, ROUND(blk_write_time::numeric,4) as blk_write_time, wal_records, wal_fpi, wal_bytes from pg_stat_monitor where query Like \'%bench%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($dynamic_out_filename_with_path, $stdout . "\n"); PGSM::append_to_debug_file($stdout);
TestLib::append_to_file($dynamic_out_filename_with_path, "\n\n"); PGSM::append_to_debug_file("-------------");
# Compare values for query 'Delete from pgbench_accounts where $1 = $2' # Compare values for query 'Delete from pgbench_accounts where $1 = $2'
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select PGSM.calls = PGSS.calls from pg_stat_monitor as PGSM INNER JOIN pg_stat_statements as PGSS ON PGSS.query = PGSM.query where PGSM.query Like \'%Delete from pgbench_accounts%\';', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select PGSM.calls = PGSS.calls from pg_stat_monitor as PGSM INNER JOIN pg_stat_statements as PGSS ON PGSS.query = PGSM.query where PGSM.query Like \'%Delete from pgbench_accounts%\';', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
@ -343,7 +297,7 @@ is($stdout,'t',"Compare: wal_bytes are equal.");
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;

View File

@ -4,65 +4,23 @@ use strict;
use warnings; use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim); use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed if ($PGSM::PG_MAJOR_VERSION <= 12)
my $results_folder = "t/results"; {
plan skip_all => "pg_stat_monitor test cases for versions 12 and below.";
# Check if results folder exists or not, create if it doesn't }
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";;
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
my $dynamic_out_filename_with_path = "${results_folder}/${out_filename}.dynamic" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# PG's major server version
open my $FH_PG_VERSION, '<', "${pgdata}/PG_VERSION";
my $major_version = trim(<$FH_PG_VERSION>);
close $FH_PG_VERSION;
if ($major_version <= 12)
{
plan skip_all => "pg_stat_statements test cases for versions 12 and below.";
}
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
$node->append_conf('postgresql.conf', "shared_preload_libraries = 'pg_stat_statements,pg_stat_monitor'"); $node->append_conf('postgresql.conf', "shared_preload_libraries = 'pg_stat_statements,pg_stat_monitor'");
@ -80,36 +38,32 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_statements;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_statements;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSS Extension"); ok($cmdret == 0, "Create PGSS Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Create extension and change out file permissions # Create extension and change out file permissions
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_statements_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_statements_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSS Extension"); ok($cmdret == 0, "Reset PGSS Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Run 'SELECT * from pg_stat_monitor_settings;' two times and dump output to out file # Run 'SELECT * from pg_stat_monitor_settings;' two times and dump output to out file
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Create example database and run pgbench init # Create example database and run pgbench init
# ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE database example;', extra_params => ['-a']); # ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE database example;', extra_params => ['-a']);
# ok($cmdret == 0, "Create Database example"); # ok($cmdret == 0, "Create Database example");
# TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); # PGSM::append_to_file($stdout);
my $port = $node->port; my $port = $node->port;
@ -126,11 +80,11 @@ ok($cmdret == 0, "Run pgbench");
($cmdret, $stdout, $stderr) = $node->psql('postgres', "Delete from pgbench_accounts where aid % 2 = 1;", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', "Delete from pgbench_accounts where aid % 2 = 1;", extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select substr(query,0,130) as query, calls, rows, total_exec_time,min_exec_time,max_exec_time,mean_exec_time,stddev_exec_time from pg_stat_statements where query Like \'%bench%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select substr(query,0,130) as query, calls, rows, total_exec_time,min_exec_time,max_exec_time,mean_exec_time,stddev_exec_time from pg_stat_statements where query Like \'%bench%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($dynamic_out_filename_with_path, $stdout . "\n"); PGSM::append_to_debug_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select substr(query,0,130) as query, calls, rows_retrieved, total_exec_time, min_exec_time, max_exec_time, mean_exec_time,stddev_exec_time, cpu_user_time, cpu_sys_time from pg_stat_monitor where query Like \'%bench%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select substr(query,0,130) as query, calls, rows_retrieved, total_exec_time, min_exec_time, max_exec_time, mean_exec_time,stddev_exec_time, cpu_user_time, cpu_sys_time from pg_stat_monitor where query Like \'%bench%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($dynamic_out_filename_with_path, $stdout . "\n"); PGSM::append_to_debug_file($stdout);
TestLib::append_to_file($dynamic_out_filename_with_path, "\n\n"); PGSM::append_to_debug_file("--------");
# Compare values for query 'Delete from pgbench_accounts where $1 = $2' # Compare values for query 'Delete from pgbench_accounts where $1 = $2'
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select PGSM.shared_blks_hit != 0 from pg_stat_monitor as PGSM where PGSM.query Like \'%Delete from pgbench_accounts%\';', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select PGSM.shared_blks_hit != 0 from pg_stat_monitor as PGSM where PGSM.query Like \'%Delete from pgbench_accounts%\';', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
@ -210,7 +164,7 @@ is($stdout,'t',"Check: blk_write_time should not be 0.");
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;

View File

@ -4,65 +4,23 @@ use strict;
use warnings; use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim); use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed if ($PGSM::PG_MAJOR_VERSION <= 12)
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{ {
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";; plan skip_all => "pg_stat_monitor test cases for versions 12 and below.";
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
my $dynamic_out_filename_with_path = "${results_folder}/${out_filename}.dynamic" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
} }
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# PG's major server version
open my $FH_PG_VERSION, '<', "${pgdata}/PG_VERSION";
my $major_version = trim(<$FH_PG_VERSION>);
close $FH_PG_VERSION;
if ($major_version <= 12)
{
plan skip_all => "pg_stat_statements test cases for versions 12 and below.";
}
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
$node->append_conf('postgresql.conf', "shared_preload_libraries = 'pg_stat_monitor'"); $node->append_conf('postgresql.conf', "shared_preload_libraries = 'pg_stat_monitor'");
@ -79,33 +37,29 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_statements;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_statements;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSS Extension"); ok($cmdret == 0, "Create PGSS Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Create extension and change out file permissions # Create extension and change out file permissions
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Run 'SELECT * from pg_stat_monitor_settings;' two times and dump output to out file # Run 'SELECT * from pg_stat_monitor_settings;' two times and dump output to out file
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', ' CREATE Temporary TABLE t1(a int); INSERT INTO t1 VALUES(generate_series(1,10000)); ANALYZE t1; SELECT * FROM t1;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', ' CREATE Temporary TABLE t1(a int); INSERT INTO t1 VALUES(generate_series(1,10000)); ANALYZE t1; SELECT * FROM t1;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Create Temporary Table and insert values in a single session."); ok($cmdret == 0, "Create Temporary Table and insert values in a single session.");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select substr(query,0,130) as query, calls, rows_retrieved, cpu_user_time, cpu_sys_time, local_blks_hit, local_blks_read, local_blks_dirtied, local_blks_written, temp_blks_read, temp_blks_written from pg_stat_monitor where query Like \'%t1%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select substr(query,0,130) as query, calls, rows_retrieved, cpu_user_time, cpu_sys_time, local_blks_hit, local_blks_read, local_blks_dirtied, local_blks_written, temp_blks_read, temp_blks_written from pg_stat_monitor where query Like \'%t1%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($dynamic_out_filename_with_path, $stdout . "\n"); PGSM::append_to_debug_file($stdout);
# Compare values for query 'INSERT INTO t1 VALUES(generate_series($1,$2))' # Compare values for query 'INSERT INTO t1 VALUES(generate_series($1,$2))'
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select PGSM.local_blks_hit != 0 from pg_stat_monitor as PGSM where PGSM.query Like \'%INSERT INTO t1%\';', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select PGSM.local_blks_hit != 0 from pg_stat_monitor as PGSM where PGSM.query Like \'%INSERT INTO t1%\';', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
@ -132,7 +86,7 @@ is($stdout,'t',"Check: shared_blks_hit should not be 0.");
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;

View File

@ -4,65 +4,23 @@ use strict;
use warnings; use warnings;
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode; use File::Copy;
use String::Util qw(trim); use String::Util qw(trim);
use Test::More; use Test::More;
use lib 't';
use pgsm;
# Expected folder where expected output will be present # Get filename and create out file name and dirs where requried
my $expected_folder = "t/expected"; PGSM::setup_files_dir(basename($0));
# Results/out folder where generated results files will be placed if ($PGSM::PG_MAJOR_VERSION <= 12)
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{ {
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";; plan skip_all => "pg_stat_monitor test cases for versions 12 and below.";
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";;
}
# Get filename of the this perl file
my $perlfilename = basename($0);
#Remove .pl from filename and store in a variable
$perlfilename =~ s/\.[^.]+$//;
my $filename_without_extension = $perlfilename;
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
my $expected_filename_with_path = "${expected_folder}/${expected_filename}" ;
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
my $out_filename_with_path = "${results_folder}/${out_filename}" ;
my $dynamic_out_filename_with_path = "${results_folder}/${out_filename}.dynamic" ;
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
} }
# Create new PostgreSQL node and do initdb # Create new PostgreSQL node and do initdb
my $node = PostgresNode->get_new_node('test'); my $node = PGSM->pgsm_init_pg();
my $pgdata = $node->data_dir; my $pgdata = $node->data_dir;
$node->dump_info;
$node->init;
# PG's major server version
open my $FH_PG_VERSION, '<', "${pgdata}/PG_VERSION";
my $major_version = trim(<$FH_PG_VERSION>);
close $FH_PG_VERSION;
if ($major_version <= 12)
{
plan skip_all => "pg_stat_statements test cases for versions 12 and below.";
}
# Update postgresql.conf to include/load pg_stat_monitor library # Update postgresql.conf to include/load pg_stat_monitor library
$node->append_conf('postgresql.conf', "shared_preload_libraries = 'pg_stat_monitor'"); $node->append_conf('postgresql.conf', "shared_preload_libraries = 'pg_stat_monitor'");
@ -81,52 +39,48 @@ ok($rt_value == 1, "Start Server");
# Create extension and change out file permissions # Create extension and change out file permissions
my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_statements;', extra_params => ['-a']); my ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_statements;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSS Extension"); ok($cmdret == 0, "Create PGSS Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Create extension and change out file permissions # Create extension and change out file permissions
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Create PGSM Extension"); ok($cmdret == 0, "Create PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
# Run required commands/queries and dump output to out file. # Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset PGSM Extension"); ok($cmdret == 0, "Reset PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Run 'SELECT * from pg_stat_monitor_settings;' two times and dump output to out file # Run 'SELECT * from pg_stat_monitor_settings;' two times and dump output to out file
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * from pg_stat_monitor_settings;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Print PGSM Extension Settings"); ok($cmdret == 0, "Print PGSM Extension Settings");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', ' CREATE TABLE t1(a int);', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', ' CREATE TABLE t1(a int);', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Create Table t1"); ok($cmdret == 0, "Create Table t1");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE INDEX idx_t1_a on t1(a);', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'CREATE INDEX idx_t1_a on t1(a);', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Create index."); ok($cmdret == 0, "Create index.");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'INSERT INTO t1 VALUES(generate_series(1,1000000));', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'INSERT INTO t1 VALUES(generate_series(1,1000000));', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Insert 10000 records."); ok($cmdret == 0, "Insert 10000 records.");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'ANALYZE t1;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'ANALYZE t1;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Analyze t1."); ok($cmdret == 0, "Analyze t1.");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * FROM t1 AS XX INNER JOIN t1 as TT ON XX.a = TT.a;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT * FROM t1 AS XX INNER JOIN t1 as TT ON XX.a = TT.a;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Select * from t1."); ok($cmdret == 0, "Select * from t1.");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select substr(query,0,130) as query, calls, rows_retrieved, cpu_user_time, cpu_sys_time, local_blks_hit, local_blks_read, local_blks_dirtied, local_blks_written, temp_blks_read, temp_blks_written from pg_stat_monitor where query Like \'%t1%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select substr(query,0,130) as query, calls, rows_retrieved, cpu_user_time, cpu_sys_time, local_blks_hit, local_blks_read, local_blks_dirtied, local_blks_written, temp_blks_read, temp_blks_written from pg_stat_monitor where query Like \'%t1%\' order by query,calls desc;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($dynamic_out_filename_with_path, $stdout . "\n"); PGSM::append_to_debug_file($stdout);
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT temp_files, temp_bytes FROM pg_stat_database db;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'SELECT temp_files, temp_bytes FROM pg_stat_database db;', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
TestLib::append_to_file($dynamic_out_filename_with_path, $stdout . "\n"); PGSM::append_to_debug_file($stdout);
# Compare values for query 'SELECT * FROM t1' # Compare values for query 'SELECT * FROM t1'
($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select PGSM.temp_blks_read != 0 from pg_stat_monitor as PGSM where PGSM.query Like \'%FROM t1%\';', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']); ($cmdret, $stdout, $stderr) = $node->psql('postgres', 'Select PGSM.temp_blks_read != 0 from pg_stat_monitor as PGSM where PGSM.query Like \'%FROM t1%\';', extra_params => ['-Pformat=unaligned','-Ptuples_only=on']);
@ -140,7 +94,7 @@ is($stdout,'t',"Check: temp_blks_read should not be 0.");
# Drop extension # Drop extension
$stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']); $stdout = $node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "Drop PGSM Extension"); ok($cmdret == 0, "Drop PGSM Extension");
TestLib::append_to_file($out_filename_with_path, $stdout . "\n"); PGSM::append_to_file($stdout);
# Stop the server # Stop the server
$node->stop; $node->stop;

View File

@ -0,0 +1,114 @@
CREATE EXTENSION pg_stat_monitor;
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_query_shared_buffer';
name | value | default_value | description | minimum | maximum | options | restart
------------------------------------------+-------+---------------+-------------------------------------------------------------------------------------------+---------+---------+---------+---------
pg_stat_monitor.pgsm_query_shared_buffer | 1 | 20 | Sets the maximum size of shared memory in (MB) used for query tracked by pg_stat_monitor. | 1 | 10000 | | yes
(1 row)
CREATE database example;
select datname, substr(query,0,150) as query, calls from pg_stat_monitor order by datname, query, calls desc Limit 20;
datname | query | calls
---------+---------------------------------------------------------------------------------------------------------------+-------
example | BEGIN | 10000
example | END | 10000
example | INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES ($1, $2, $3, $4, CURRENT_TIMESTAMP) | 10000
example | SELECT abalance FROM pgbench_accounts WHERE aid = $1 | 10000
example | UPDATE pgbench_accounts SET abalance = abalance + $1 WHERE aid = $2 | 10000
example | UPDATE pgbench_branches SET bbalance = bbalance + $1 WHERE bid = $2 | 10000
example | UPDATE pgbench_tellers SET tbalance = tbalance + $1 WHERE tid = $2 | 10000
example | alter table pgbench_accounts add primary key (aid) | 1
example | alter table pgbench_branches add primary key (bid) | 1
example | alter table pgbench_tellers add primary key (tid) | 1
example | begin | 1
example | commit | 1
example | copy pgbench_accounts from stdin with (freeze on) | 1
example | create table pgbench_accounts(aid int not null,bid int,abalance int,filler char(84)) with (fillfactor=100) | 1
example | create table pgbench_branches(bid int not null,bbalance int,filler char(88)) with (fillfactor=100) | 1
example | create table pgbench_history(tid int,bid int,aid int,delta int,mtime timestamp,filler char(22)) | 1
example | create table pgbench_tellers(tid int not null,bid int,tbalance int,filler char(84)) with (fillfactor=100) | 1
example | drop table if exists pgbench_accounts, pgbench_branches, pgbench_history, pgbench_tellers | 1
example | insert into pgbench_branches(bid,bbalance) values($1,$2) | 100
example | insert into pgbench_tellers(tid,bid,tbalance) values ($1,$2,$3) | 1000
(20 rows)
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_query_shared_buffer';
name | value | default_value | description | minimum | maximum | options | restart
------------------------------------------+-------+---------------+-------------------------------------------------------------------------------------------+---------+---------+---------+---------
pg_stat_monitor.pgsm_query_shared_buffer | 100 | 20 | Sets the maximum size of shared memory in (MB) used for query tracked by pg_stat_monitor. | 1 | 10000 | | yes
(1 row)
select datname, substr(query,0,150) as query, calls from pg_stat_monitor order by datname, query, calls desc Limit 20;
datname | query | calls
---------+---------------------------------------------------------------------------------------------------------------+-------
example | BEGIN | 10000
example | END | 10000
example | INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES ($1, $2, $3, $4, CURRENT_TIMESTAMP) | 10000
example | SELECT abalance FROM pgbench_accounts WHERE aid = $1 | 10000
example | UPDATE pgbench_accounts SET abalance = abalance + $1 WHERE aid = $2 | 10000
example | UPDATE pgbench_branches SET bbalance = bbalance + $1 WHERE bid = $2 | 10000
example | UPDATE pgbench_tellers SET tbalance = tbalance + $1 WHERE tid = $2 | 10000
example | alter table pgbench_accounts add primary key (aid) | 1
example | alter table pgbench_branches add primary key (bid) | 1
example | alter table pgbench_tellers add primary key (tid) | 1
example | begin | 1
example | commit | 1
example | copy pgbench_accounts from stdin with (freeze on) | 1
example | create table pgbench_accounts(aid int not null,bid int,abalance int,filler char(84)) with (fillfactor=100) | 1
example | create table pgbench_branches(bid int not null,bbalance int,filler char(88)) with (fillfactor=100) | 1
example | create table pgbench_history(tid int,bid int,aid int,delta int,mtime timestamp,filler char(22)) | 1
example | create table pgbench_tellers(tid int not null,bid int,tbalance int,filler char(84)) with (fillfactor=100) | 1
example | drop table if exists pgbench_accounts, pgbench_branches, pgbench_history, pgbench_tellers | 1
example | insert into pgbench_branches(bid,bbalance) values($1,$2) | 100
example | insert into pgbench_tellers(tid,bid,tbalance) values ($1,$2,$3) | 1000
(20 rows)
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------
(1 row)
SELECT * from pg_stat_monitor_settings where name='pg_stat_monitor.pgsm_query_shared_buffer';
name | value | default_value | description | minimum | maximum | options | restart
------------------------------------------+-------+---------------+-------------------------------------------------------------------------------------------+---------+---------+---------+---------
pg_stat_monitor.pgsm_query_shared_buffer | 20 | 20 | Sets the maximum size of shared memory in (MB) used for query tracked by pg_stat_monitor. | 1 | 10000 | | yes
(1 row)
select datname, substr(query,0,150) as query, calls from pg_stat_monitor order by datname, query, calls desc Limit 20;
datname | query | calls
---------+---------------------------------------------------------------------------------------------------------------+-------
example | BEGIN | 10000
example | END | 10000
example | INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES ($1, $2, $3, $4, CURRENT_TIMESTAMP) | 10000
example | SELECT abalance FROM pgbench_accounts WHERE aid = $1 | 10000
example | UPDATE pgbench_accounts SET abalance = abalance + $1 WHERE aid = $2 | 10000
example | UPDATE pgbench_branches SET bbalance = bbalance + $1 WHERE bid = $2 | 10000
example | UPDATE pgbench_tellers SET tbalance = tbalance + $1 WHERE tid = $2 | 10000
example | alter table pgbench_accounts add primary key (aid) | 1
example | alter table pgbench_branches add primary key (bid) | 1
example | alter table pgbench_tellers add primary key (tid) | 1
example | begin | 1
example | commit | 1
example | copy pgbench_accounts from stdin with (freeze on) | 1
example | create table pgbench_accounts(aid int not null,bid int,abalance int,filler char(84)) with (fillfactor=100) | 1
example | create table pgbench_branches(bid int not null,bbalance int,filler char(88)) with (fillfactor=100) | 1
example | create table pgbench_history(tid int,bid int,aid int,delta int,mtime timestamp,filler char(22)) | 1
example | create table pgbench_tellers(tid int not null,bid int,tbalance int,filler char(84)) with (fillfactor=100) | 1
example | drop table if exists pgbench_accounts, pgbench_branches, pgbench_history, pgbench_tellers | 1
example | insert into pgbench_branches(bid,bbalance) values($1,$2) | 100
example | insert into pgbench_tellers(tid,bid,tbalance) values ($1,$2,$3) | 1000
(20 rows)
Drop extension pg_stat_monitor;

171
t/pgsm.pm
View File

@ -1,72 +1,157 @@
package pgsm; package PGSM;
use String::Util qw(trim); use String::Util qw(trim);
use File::Basename; use File::Basename;
use File::Compare; use File::Compare;
use PostgresNode;
use Test::More; use Test::More;
our @ISA= qw( Exporter ); our @ISA= qw( Exporter );
# these CAN be exported. # These CAN be exported.
our @EXPORT = qw( pgsm_init_pg pgsm_start_pg pgsm_stop_pg pgsm_psql_cmd pgsm_setup_pg_stat_monitor pgsm_create_extension pgsm_reset_pg_stat_monitor pgsm_drop_extension ); our @EXPORT = qw( pgsm_init_pg pgsm_start_pg pgsm_stop_pg pgsm_psql_cmd pgsm_setup_pg_stat_monitor pgsm_create_extension pgsm_reset_pg_stat_monitor pgsm_drop_extension );
# Instance of pg server that would be spanwed by TAP testing. A new server will be created for each TAP test.
our $pg_node; our $pg_node;
# Create new PostgreSQL node and do initdb # Expected .out filename of TAP testcase being executed. These are already part of repo under t/expected/*.
our $expected_filename_with_path;
# Major version of PG Server that we are using.
our $PG_MAJOR_VERSION;
# Result .out filename of TAP testcase being executed. Where needed, a new *.out will be created for each TAP test.
our $out_filename_with_path;
# Runtime output file that is used only for debugging purposes for comparison to PGSS, blocks and timings.
our $debug_out_filename_with_path;
BEGIN {
# Get PG Server Major version from pg_config
$PG_MAJOR_VERSION = `pg_config --version | awk {'print \$2'} | cut -f1 -d"." | sed -e 's/[^0-9].*\$//g'`;
$PG_MAJOR_VERSION =~ s/^\s+|\s+$//g;
# Depending upon PG server version load the required module at runtime when pgsm.pm is loaded.
my $node_module = $PG_MAJOR_VERSION > 14 ? "PostgreSQL::Test::Cluster" : "PostgresNode";
my $node_module_file = $node_module;
$node_module_file =~ s[::][/]g;
$node_module_file .= '.pm';
require $node_module_file;
$node_module->import;
}
sub pgsm_init_pg sub pgsm_init_pg
{ {
$pg_node = PostgresNode->get_new_node('pgsm_regression'); print "Postgres major version: $PG_MAJOR_VERSION \n";
# For Server version 15 & above, spawn the server using PostgreSQL::Test::Cluster
if ($PG_MAJOR_VERSION > 14) {
$pg_node = PostgreSQL::Test::Cluster->new('pgsm_regression');
}
# For Server version 14 & below, spawn the server using PostgresNode
elsif ($PG_MAJOR_VERSION < 15) {
$pg_node = PostgresNode->get_new_node('pgsm_regression');
}
$pg_node->dump_info; $pg_node->dump_info;
$pg_node->init; $pg_node->init;
return $pg_node;
} }
sub pgsm_start_pg sub append_to_file
{ {
my $rt_value = $pg_node->start; my ($str) = @_;
ok($rt_value == 1, "Starting PostgreSQL");
return $rt_value; # For Server version 15 & above, use PostgreSQL::Test::Utils to write to files
if ($PG_MAJOR_VERSION > 14) {
PostgreSQL::Test::Utils::append_to_file($out_filename_with_path, $str . "\n");
}
# For Server version 14 & below, use PostgresNode to write to files
elsif ($PG_MAJOR_VERSION < 15) {
TestLib::append_to_file($out_filename_with_path, $str . "\n");
}
chmod(0640 , $out_filename_with_path)
or die("unable to set permissions for $out_filename_with_path");
return;
} }
sub pgsm_stop_pg sub append_to_debug_file
{ {
return $pg_node->stop; my ($str) = @_;
# For Server version 15 & above, use PostgreSQL::Test::Utils to write to files
if ($PG_MAJOR_VERSION > 14) {
PostgreSQL::Test::Utils::append_to_file($debug_out_filename_with_path, $str . "\n");
}
# For Server version 14 & below, use PostgresNode to write to files
elsif ($PG_MAJOR_VERSION < 15) {
TestLib::append_to_file($debug_out_filename_with_path, $str . "\n");
}
chmod(0640 , $debug_out_filename_with_path)
or die("unable to set permissions for $debug_out_filename_with_path");
return;
} }
sub pgsm_psql_cmd sub setup_files_dir
{ {
my ($cmdret, $stdout, $stderr) = $pg_node->psql(@_); my ($perlfilename) = @_;
# Expected folder where expected output will be present
my $expected_folder = "t/expected";
# Results/out folder where generated results files will be placed
my $results_folder = "t/results";
# Check if results folder exists or not, create if it doesn't
unless (-d $results_folder)
{
mkdir $results_folder or die "Can't create folder $results_folder: $!\n";
}
# Check if expected folder exists or not, bail out if it doesn't
unless (-d $expected_folder)
{
BAIL_OUT "Expected files folder $expected_folder doesn't exist: \n";
}
#Remove .pl from filename and store in a variable
my @split_arr = split /\./, $perlfilename;
my $filename_without_extension = $split_arr[0];
# Create expected filename with path
my $expected_filename = "${filename_without_extension}.out";
if ($PG_MAJOR_VERSION <= 12 and "$filename_without_extension" == "001_settings_default")
{
$expected_filename = "${expected_filename}.${PG_MAJOR_VERSION}";
}
if ($PG_MAJOR_VERSION >= 15 and "$filename_without_extension" == "007_settings_pgsm_query_shared_buffer")
{
$expected_filename = "${expected_filename}.${PG_MAJOR_VERSION}";
}
$expected_filename_with_path = "${expected_folder}/${expected_filename}";
# Create results filename with path
my $out_filename = "${filename_without_extension}.out";
$out_filename_with_path = "${results_folder}/${out_filename}";
# Delete already existing result out file, if it exists.
if ( -f $out_filename_with_path)
{
unlink($out_filename_with_path) or die "Can't delete already existing $out_filename_with_path: $!\n";
}
$debug_out_filename_with_path = "${results_folder}/${out_filename}.debug";
} }
sub pgsm_setup_pg_stat_monitor sub compare_results
{ {
my ($set) = @_; # Compare expected and results files and return the result
my $pgdata = $pg_node->data_dir; return compare($expected_filename_with_path, $out_filename_with_path);
open my $conf, '>>', "$pgdata/postgresql.conf";
print $conf "shared_preload_libraries = 'pg_stat_monitor'\n";
print $conf "$set\n";
close $conf;
} }
sub pgsm_create_extension 1;
{
my ($cmdret, $stdout, $stderr) = $pg_node->psql('postgres', 'CREATE EXTENSION pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "CREATE EXTENSION pg_stat_monitor...");
return ($cmdret, $stdout, $stderr);
}
sub pgsm_reset_pg_stat_monitor
{
# Run required commands/queries and dump output to out file.
($cmdret, $stdout, $stderr) = $pg_node->psql('postgres', 'SELECT pg_stat_monitor_reset();', extra_params => ['-a', '-Pformat=aligned','-Ptuples_only=off']);
ok($cmdret == 0, "Reset pg_stat_monitor...");
return ($cmdret, $stdout, $stderr);
}
sub pgsm_drop_extension
{
my ($cmdret, $stdout) = $pg_node->safe_psql('postgres', 'Drop extension pg_stat_monitor;', extra_params => ['-a']);
ok($cmdret == 0, "DROP EXTENSION pg_stat_monitor...");
return ($cmdret, $stdout, $stderr);
}
1;