Skip to content

Commit

Permalink
Add to the JDBC test framework the ability to set a performance SLA p…
Browse files Browse the repository at this point in the history
…er file (babelfish-for-postgresql#700)

Description
Currently the tests developed on Babelfish do not have performance SLAs. This change adds SLAs to the JDBC test framework.
Added a new parameter in config.txt to specify the default SLA. This will be considered as default timeout unless it is explicitly mentioned in the test file.
One can specify the SLA value in milli seconds for a specific test file by adding a comment at the starting of the test file mentioning the SLA value. Ex: -- SLA 10000
  • Loading branch information
basasairohan authored Oct 31, 2022
1 parent 13b63c6 commit 9e90e3d
Show file tree
Hide file tree
Showing 53 changed files with 143 additions and 16 deletions.
24 changes: 24 additions & 0 deletions test/JDBC/expected/sla_demo.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- psql
select 1;
GO
~~START~~
int4
1
~~END~~


select pg_sleep(3);
GO
~~START~~
void

~~END~~


select 2;
GO
~~START~~
int4
2
~~END~~

1 change: 1 addition & 0 deletions test/JDBC/input/BABEL-1756-vu-verify.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 20000
SELECT colid, name, collation_100 FROM sys.spt_tablecollations_view WHERE object_id = sys.object_id('babel_1756_vu_prepare_t1') ORDER BY colid
GO

Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/BABEL-LOGIN-USER-EXT.mix
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 170000
-- psql
ALTER SYSTEM SET babelfishpg_tsql.allow_antlr_to_unsupported_grammar_for_testing = true;
SELECT pg_reload_conf();
Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/BABEL-SP_COLUMN_PRIVILEGES.mix
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 10000
-- tsql
CREATE DATABASE db1
GO
Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/BABEL-SP_FKEYS-dep-vu-verify.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
-- sla 20000
exec babel_sp_fkeys_dep_vu_prepare_p1
go
1 change: 1 addition & 0 deletions test/JDBC/input/BABEL-SP_FKEYS-vu-verify.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 1000000
use babel_sp_fkeys_vu_prepare_db1
go

Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/BABEL-SP_FKEYS.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 1000000
create database db1
go
use db1
Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/BABEL-SP_SPECIAL_COLUMNS-vu-verify.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 60000
use babel_sp_special_columns_vu_prepare_db1
go

Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/BABEL-SP_SPECIAL_COLUMNS.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 60000
create database db1
go
use db1
Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/BABEL-SP_SPROC_COLUMNS-vu-verify.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 25000
USE babel_sp_sproc_columns_vu_prepare_db1
GO

Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/BABEL-SP_TABLE_PRIVILIGES-vu-verify.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 20000
use babel_sp_table_priviliges_vu_prepare_db1
go

Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/babel_cursor.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 10000
CREATE TABLE babel_cursor_t1 (i INT, d numeric(8, 4), c varchar(10), u uniqueidentifier, v sql_variant);
INSERT INTO babel_cursor_t1 VALUES (1, 1.1, 'a', '1E984725-C51C-4BF4-9960-E1C80E27ABA0', 1);
INSERT INTO babel_cursor_t1 VALUES (2, 22.22, 'bb', '2E984725-C51C-4BF4-9960-E1C80E27ABA0', 22.22);
Expand Down
10 changes: 10 additions & 0 deletions test/JDBC/input/sla_demo.mix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- sla 5000
-- psql
select 1;
GO

select pg_sleep(3);
GO

select 2;
GO
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 60000
EXEC sys_all_sql_modules_dep_vu_prepare_p1;
GO

Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/views/sys-all_sql_modules-vu-verify.mix
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 60000
-- NOTE: Some tests will be marked as "DISABLED DUE TO NON-UNIQUE OBJECT_ID"
-- This is because during MVU, items in pg_class an pg_proc could have the same OBJECT_ID
-- Some tests have no way of identification except for OBJECT_ID since definition will be missing
Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/views/sys-all_sql_modules.mix
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 80000
-- Setup
CREATE DATABASE sys_all_sql_modules_vu_prepare_db
GO
Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/views/sys-columns-vu-verify.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 10000
-- Tests for sys.columns catalog view
-- Test precision and scale for all numeric datatypes
select name, column_id, precision, scale from sys.columns where object_id=OBJECT_ID('t1_sys_syscolumns') order by name;
Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/views/sys-objects-vu-verify.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 50000
USE sys_objects_vu_prepare_db1
GO

Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/views/sys-procedures-vu-verify.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 50000
USE sys_procedures_vu_prepare_db1
GO

Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/views/sys-sql_modules-dep-vu-verify.mix
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 50000
EXEC sys_sql_modules_dep_vu_prepare_p1;
GO

Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/views/sys-sql_modules-vu-verify.mix
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 35000
-- NOTE: Some tests will be marked as "DISABLED DUE TO NON-UNIQUE OBJECT_ID"
-- This is because during MVU, items in pg_class an pg_proc could have the same OBJECT_ID
-- Some tests have no way of identification except for OBJECT_ID since definition will be missing
Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/views/sys-sql_modules.mix
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 35000
-- Setup
CREATE DATABASE sys_sql_modules_vu_prepare_db
GO
Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/views/sys-syscolumns.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 10000
create database db1;
go

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 50000
USE sys_system_sql_modules_dep_vu_prepare_db1
GO

Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/views/sys-system_sql_modules-vu-verify.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 50000
-- NOTE: Some tests will be marked as "DISABLED DUE TO NON-UNIQUE OBJECT_ID"
-- This is because during MVU, items in pg_class an pg_proc could have the same OBJECT_ID
-- Some tests have no way of identification except for OBJECT_ID since definition will be missing
Expand Down
1 change: 1 addition & 0 deletions test/JDBC/input/views/sys-system_sql_modules.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 50000
-- Test for system function
SELECT
definition,
Expand Down
1 change: 1 addition & 0 deletions test/JDBC/src/main/java/com/sqlsamples/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class Config {
static String scheduleFileName = properties.getProperty("scheduleFile");
static String testFileRoot = properties.getProperty("testFileRoot");
static boolean isUpgradeTestMode = Boolean.parseBoolean(properties.getProperty("isUpgradeTestMode"));
static long defaultSLA = Long.parseLong(properties.getProperty("defaultSLA"));

static String connectionString = constructConnectionString();

Expand Down
3 changes: 2 additions & 1 deletion test/JDBC/src/main/java/com/sqlsamples/Statistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
public class Statistics {
DescriptiveStatistics descriptiveStatistics;
static ArrayList<Long> exec_times = new ArrayList<>();

static long curr_exec_time = 0L;
static long sla = 0L;
public Statistics(double[] values) {
descriptiveStatistics = new DescriptiveStatistics(values);
}
Expand Down
18 changes: 12 additions & 6 deletions test/JDBC/src/main/java/com/sqlsamples/batch_run.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import static com.sqlsamples.Config.*;
import static com.sqlsamples.Statistics.exec_times;
import static com.sqlsamples.Statistics.curr_exec_time;
import static com.sqlsamples.Statistics.sla;

public class batch_run {

Expand All @@ -20,6 +22,7 @@ static void batch_run_sql(Connection con_bbl, BufferedWriter bw, String testFile
boolean isCrossDialectFile = false;
boolean tsqlDialect = false;
boolean psqlDialect = false;
boolean customSLA = false;

if (testFilePath.contains(".mix")) {
isCrossDialectFile = true;
Expand Down Expand Up @@ -65,12 +68,10 @@ static void batch_run_sql(Connection con_bbl, BufferedWriter bw, String testFile
}
continue;
}

long startTime = System.nanoTime();

// if line starts with keyword "prepst", it means it is either a prep exec statement or an exec statement
if (strLine.startsWith("prepst")) {

// Convert .NET input file format for prepared statement to JDBC
strLine = strLine.replaceAll("@[a-zA-Z0-9]+", "?");

Expand All @@ -91,7 +92,6 @@ static void batch_run_sql(Connection con_bbl, BufferedWriter bw, String testFile
jdbcPreparedStatement.testPreparedStatementWithFile(result, bw, strLine, logger);

} else if (!result[1].equals("exec")) {

jdbcPreparedStatement.closePreparedStatements(bw, logger);

SQL = result[1];
Expand Down Expand Up @@ -186,7 +186,6 @@ static void batch_run_sql(Connection con_bbl, BufferedWriter bw, String testFile

// if line starts with keyword "cursor", it means it is a cursor operation
} else if (strLine.startsWith("cursor")) {

// Convert .NET input file format for prepared statement to JDBC
// Used if cursor opened on a result set from a prepared statement
if (strLine.contains("prepst")) {
Expand Down Expand Up @@ -255,7 +254,14 @@ static void batch_run_sql(Connection con_bbl, BufferedWriter bw, String testFile
} else {
// execute statement as a normal SQL statement
if (isSQLFile) {
if (!strLine.equalsIgnoreCase("GO")) {
customSLA = strLine.toLowerCase().startsWith("-- sla");
if (customSLA){
String[] tokens=strLine.split(" ");
sla = Long.parseLong(tokens[2]);
sla = sla*(1000000L);
continue;
}
else if (!strLine.equalsIgnoreCase("GO")) {
sqlBatch.append(strLine).append(System.lineSeparator());
continue;
} else {
Expand All @@ -273,10 +279,10 @@ static void batch_run_sql(Connection con_bbl, BufferedWriter bw, String testFile
jdbcStatement.createStatements(con_bbl, bw, logger);
jdbcStatement.testStatementWithFile(SQL, bw, strLine, logger);
}

long endTime = System.nanoTime();
long duration = (endTime - startTime);
exec_times.add(duration);
curr_exec_time += duration;
}
} catch (IOException ioe) {
logger.error("IO Exception: " + ioe.getMessage(), ioe);
Expand Down
3 changes: 3 additions & 0 deletions test/JDBC/src/main/resources/config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ testFileRoot = ./

# WHETHER TEST RUN MODE IS UPGRADE TEST RUN
isUpgradeTestMode = false

# Default SLA in milliseconds
defaultSLA = 15000
54 changes: 45 additions & 9 deletions test/JDBC/src/test/java/com/sqlsamples/TestQueryFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import static com.sqlsamples.Config.*;

import static com.sqlsamples.Statistics.exec_times;
import static com.sqlsamples.Statistics.curr_exec_time;
import static com.sqlsamples.Statistics.sla;

public class TestQueryFile {

Expand All @@ -26,6 +28,7 @@ public class TestQueryFile {
static Logger summaryLogger = LogManager.getLogger("testSummaryLogger"); //logger to write summary of tests executed
static Logger logger = LogManager.getLogger("eventLoggger"); //logger to log any test framework events
static ArrayList<AbstractMap.SimpleEntry<String, Boolean>> summaryMap = new ArrayList<>(); //map to store test names and status
static ArrayList<AbstractMap.SimpleEntry<String, ArrayList<Long>>> slaMap = new ArrayList<>(); //map to store execution time and SLA
static ArrayList<AbstractMap.SimpleEntry<String, ArrayList<Integer>>> testCountMap = new ArrayList<>(); //map to store test names and number of tests passed
static ArrayList <String> fileList = new ArrayList<>();
static HashMap<String, String> filePaths = new HashMap<>(); //map to store fileName and their paths
Expand Down Expand Up @@ -252,16 +255,23 @@ public static void logSummary() {
String testMethodName = summaryMap.get(i).getKey();

boolean status = summaryMap.get(i).getValue();
Long exec_time = slaMap.get(i).getValue().get(0);
Long sla_val = slaMap.get(i).getValue().get(1);
int testsPassed = 0, totalTests = 0;

if(status){
//extra spaces for right side padding
summaryLogger.info((testMethodName + ":" + " ").substring(0, maxlen+2) + "Passed!" + testStats);
summaryLogger.info((testMethodName + ":" + " ").substring(0, maxlen+2) + "Passed! (" + exec_time + "/" + sla_val + "ms OK)" + testStats);
passed++;
}
else if(exec_time <= sla_val){
//extra spaces for right side padding
summaryLogger.info((testMethodName + ":" + " ").substring(0, maxlen+2) + "Failed! (" + exec_time + "/" + sla_val + "ms OK)" + testStats);
failed++;
}
else{
//extra spaces for right side padding
summaryLogger.info((testMethodName + ":" + " ").substring(0, maxlen+2) + "Failed!" + testStats);
summaryLogger.info((testMethodName + ":" + " ").substring(0, maxlen+2) + "Failed! (" + exec_time + "/" + sla_val + "ms TIME OUT)" + testStats);
failed++;
}
}
Expand Down Expand Up @@ -378,8 +388,8 @@ public void TestQueryBatch(String inputFileName) throws SQLException, ClassNotFo
String testFilePath = filePaths.get(inputFileName);

boolean result; // whether test passed or failed
boolean timeout;
int failed;

String outputFileName;

if(inputFilesDirectoryPath.substring(inputFilesDirectoryPath.length() - 1) == "/"){
Expand All @@ -394,12 +404,21 @@ public void TestQueryBatch(String inputFileName) throws SQLException, ClassNotFo
// generate buffer reader associated with the file
FileWriter fw = new FileWriter(outputFile);
BufferedWriter bw = new BufferedWriter(fw);
curr_exec_time = 0L;
batch_run.batch_run_sql(connection_bbl, bw, testFilePath, logger);
bw.close();

if(sla == 0){
sla = defaultSLA*1000000L;
}
File expectedFile = new File(generatedFilesDirectoryPath + outputFileName + ".out");
File sqlExpectedFile = new File(sqlServerGeneratedFilesDirectoryPath + outputFileName + ".out");

if(curr_exec_time <= sla){
timeout = false;
}
else{
timeout = true;
}
if (expectedFile.exists()) {
// get the diff
result = compareOutFiles(outputFile, expectedFile);
Expand All @@ -410,14 +429,31 @@ public void TestQueryBatch(String inputFileName) throws SQLException, ClassNotFo
result = false;
}

summaryMap.add(new AbstractMap.SimpleEntry<>(inputFileName, result)); //add test name and result to map
ArrayList<Long> tempSla = new ArrayList<>();
tempSla.add(curr_exec_time/1000000L);
tempSla.add(sla/1000000L);
summaryMap.add(new AbstractMap.SimpleEntry<>(inputFileName, result && !timeout)); //add test name and result to map
slaMap.add(new AbstractMap.SimpleEntry<>(inputFileName, tempSla)); //add execution time and SLA
sla = 0L;

try {
Assertions.assertTrue(result);
Assertions.assertTrue(result && !timeout);
} catch (AssertionError e) {
Throwable throwable = new Throwable(inputFileName + " FAILED! Output diff can be found in '" + diffFile.getAbsolutePath() + "'");
throwable.setStackTrace(new StackTraceElement[0]);
throw throwable;
if(timeout && result){
Throwable throwable = new Throwable(inputFileName + " FAILED! Execution timed out!!");
throwable.setStackTrace(new StackTraceElement[0]);
throw throwable;
}
else if(timeout && !result){
Throwable throwable = new Throwable(inputFileName + " FAILED! Execution timed out! Output diff can be found in '" + diffFile.getAbsolutePath() + "'");
throwable.setStackTrace(new StackTraceElement[0]);
throw throwable;
}
else{
Throwable throwable = new Throwable(inputFileName + " FAILED! Output diff can be found in '" + diffFile.getAbsolutePath() + "'");
throwable.setStackTrace(new StackTraceElement[0]);
throw throwable;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 20000
exec sys_columns_dep_vu_prepare_p1
go

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 20000
exec sys_columns_dep_vu_prepare_p1
go

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 200000
USE sys_all_sql_modules_vu_prepare_db
GO

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 10000
SELECT * FROM sys_all_views_dep_view_vu_prepare
GO

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 20000
exec sys_columns_dep_vu_prepare_p1
go

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- sla 10000
EXEC sys_default_constraints_dep_vu_prepare_p1
GO

Expand Down
Loading

0 comments on commit 9e90e3d

Please sign in to comment.