diff --git a/test/Performance/benchmark/pdo_sqlsrv/large/PDOFetchLargeBench.php b/test/Performance/benchmark/pdo_sqlsrv/large/PDOFetchLargeBench.php new file mode 100644 index 00000000..b3152745 --- /dev/null +++ b/test/Performance/benchmark/pdo_sqlsrv/large/PDOFetchLargeBench.php @@ -0,0 +1,31 @@ +tableName = "LargeDB.dbo.datatypes"; + } + + public function connect(){ + $this->conn = PDOSqlsrvUtil::connect(); + } + /* + * Each iteration calls prepare, execute and fetch APIs to fetch the already populdated data + */ + public function benchFetchWithPrepare(){ + PDOSqlsrvUtil::fetchWithPrepare( $this->conn, $this->tableName ); + } + + public function disconnect(){ + PDOSqlsrvUtil::disconnect( $this->conn ); + } +} diff --git a/test/Performance/benchmark/pdo_sqlsrv/regular/PDOConnectionBench.php b/test/Performance/benchmark/pdo_sqlsrv/regular/PDOConnectionBench.php new file mode 100644 index 00000000..f6244bac --- /dev/null +++ b/test/Performance/benchmark/pdo_sqlsrv/regular/PDOConnectionBench.php @@ -0,0 +1,14 @@ + diff --git a/test/Performance/benchmark/pdo_sqlsrv/regular/PDOCreateDbTableProcBench.php b/test/Performance/benchmark/pdo_sqlsrv/regular/PDOCreateDbTableProcBench.php new file mode 100644 index 00000000..53642b6a --- /dev/null +++ b/test/Performance/benchmark/pdo_sqlsrv/regular/PDOCreateDbTableProcBench.php @@ -0,0 +1,30 @@ +conn = PDOSqlsrvUtil::connect(); + } + /* + * Each iteration creates a database, a table and a stored procedure in that database and drops the database at the end. + * Not that, execDirect function are used to execute all the queries. + */ + public function benchCreateDbTableProc(){ + $randomNum = rand(); + $databaseName = "test_db_$randomNum"; + $tableName = "test_table_$randomNum"; + $procName = "test_proc_$randomNum"; + PDOSqlsrvUtil::createDbTableProc( $this->conn, $databaseName, $tableName, $procName ); + PDOSqlsrvUtil::dropDatabase( $this->conn, $databaseName ); + } + + public function disconnect(){ + PDOSqlsrvUtil::disconnect( $this->conn ); + } +} diff --git a/test/Performance/benchmark/pdo_sqlsrv/regular/PDODeleteBench.php b/test/Performance/benchmark/pdo_sqlsrv/regular/PDODeleteBench.php new file mode 100644 index 00000000..b3eb788e --- /dev/null +++ b/test/Performance/benchmark/pdo_sqlsrv/regular/PDODeleteBench.php @@ -0,0 +1,53 @@ +tableName = "datatypes_".rand(); + } + + public function connect(){ + $this->conn = PDOSqlsrvUtil::connect(); + } + + public function createTable(){ + PDOSqlsrvUtil::createCRUDTable( $this->conn, $this->tableName ); + } + + public function generateInsertValues(){ + $this->insertValues = PDOSqlsrvUtil::generateInsertValues(); + } + + public function insertWithPrepare(){ + for( $i=0; $i<1000; $i++ ){ + PDOSqlsrvUtil::insertWithPrepare( $this->conn, $this->tableName, $this->insertValues ); + } + } + /** + * Each iteration inserts 1000 rows into the table, benchDelete deletes top row from the table 1000 times. + * Note that, every delete calls prepare and execute APIs. + */ + public function benchDelete(){ + for( $i=0; $i<1000; $i++ ){ + PDOSqlsrvUtil::deleteWithPrepare( $this->conn, $this->tableName ); + } + } + + public function dropTable(){ + PDOSqlsrvUtil::dropTable( $this->conn, $this->tableName ); + } + + public function disconnect(){ + PDOSqlsrvUtil::disconnect( $this->conn ); + } + +} diff --git a/test/Performance/benchmark/pdo_sqlsrv/regular/PDOFetchBench.php b/test/Performance/benchmark/pdo_sqlsrv/regular/PDOFetchBench.php new file mode 100644 index 00000000..e7e17b4d --- /dev/null +++ b/test/Performance/benchmark/pdo_sqlsrv/regular/PDOFetchBench.php @@ -0,0 +1,50 @@ +tableName = "datatypes_".rand(); + } + public function connect(){ + $this->conn = PDOSqlsrvUtil::connect(); + } + + public function createTable(){ + PDOSqlsrvUtil::createCRUDTable( $this->conn, $this->tableName ); + } + + public function generateInsertValues(){ + $this->insertValues = PDOSqlsrvUtil::generateInsertValues(); + } + + public function insertWithPrepare(){ + PDOSqlsrvUtil::insertWithPrepare( $this->conn, $this->tableName, $this->insertValues ); + } + + /** + * Each iteration inserts a row into the table, benchFetchWithPrepare() fetches that row 1000 times. + * Note that, every fetch calls prepare, execute and fetch APIs. + */ + public function benchFetchWithPrepare(){ + for( $i=0; $i<1000; $i++){ + PDOSqlsrvUtil::fetchWithPrepare( $this->conn, $this->tableName ); + } + } + + public function dropTable(){ + PDOSqlsrvUtil::dropTable( $this->conn, $this->tableName ); + } + + public function disconnect(){ + PDOSqlsrvUtil::disconnect( $this->conn ); + } +} diff --git a/test/Performance/benchmark/pdo_sqlsrv/regular/PDOInsertBench.php b/test/Performance/benchmark/pdo_sqlsrv/regular/PDOInsertBench.php new file mode 100644 index 00000000..73b0e191 --- /dev/null +++ b/test/Performance/benchmark/pdo_sqlsrv/regular/PDOInsertBench.php @@ -0,0 +1,45 @@ +tableName = "datatypes_".rand(); + } + public function connect(){ + $this->conn = PDOSqlsrvUtil::connect(); + } + + public function createTable(){ + PDOSqlsrvUtil::createCRUDTable( $this->conn, $this->tableName ); + } + + public function generateInsertValues(){ + $this->insertValues = PDOSqlsrvUtil::generateInsertValues(); + } + /** + * Each iteration inserts 1000 rows into the table. + * Note that, every insertion calls prepare, bindParam and execute APIs. + */ + public function benchInsertWithPrepare(){ + for ( $i=0; $i<1000; $i++){ + PDOSqlsrvUtil::insertWithPrepare( $this->conn, $this->tableName, $this->insertValues ); + } + } + + public function dropTable(){ + PDOSqlsrvUtil::dropTable( $this->conn, $this->tableName ); + } + + public function disconnect(){ + PDOSqlsrvUtil::disconnect( $this->conn ); + } +} diff --git a/test/Performance/benchmark/pdo_sqlsrv/regular/PDOSelectVersionBench.php b/test/Performance/benchmark/pdo_sqlsrv/regular/PDOSelectVersionBench.php new file mode 100644 index 00000000..10bc5a77 --- /dev/null +++ b/test/Performance/benchmark/pdo_sqlsrv/regular/PDOSelectVersionBench.php @@ -0,0 +1,25 @@ +conn = PDOSqlsrvUtil::connect(); + } + /* + * Each iteration calls execDirect API to fetch @@Version + */ + public function benchSelectVersion(){ + $version = PDOSqlsrvUtil::selectVersion( $this->conn ); + } + + public function disconnect(){ + PDOSqlsrvUtil::disconnect( $this->conn ); + } +} diff --git a/test/Performance/benchmark/pdo_sqlsrv/regular/PDOUpdateBench.php b/test/Performance/benchmark/pdo_sqlsrv/regular/PDOUpdateBench.php new file mode 100644 index 00000000..2a582f0b --- /dev/null +++ b/test/Performance/benchmark/pdo_sqlsrv/regular/PDOUpdateBench.php @@ -0,0 +1,61 @@ +tableName = "datatypes_".rand(); + } + + public function connect(){ + $this->conn = PDOSqlsrvUtil::connect(); + } + + public function createTable(){ + PDOSqlsrvUtil::createCRUDTable( $this->conn, $this->tableName ); + } + + public function generateInsertValues(){ + $this->insertValues = PDOSqlsrvUtil::generateInsertValues(); + } + + public function insertWithPrepare(){ + PDOSqlsrvUtil::insertWithPrepare( $this->conn, $this->tableName, $this->insertValues ); + } + + public function generateUpdateValues(){ + $this->updateValues = PDOSqlsrvUtil::generateUpdateValues(); + } + + public function generateUpdateParams(){ + $this->updateParams = PDOSqlsrvUtil::generateUpdateParams(); + } + /** + * Each iteration inserts a row into the table, updateWithPrepare() updates that row 1000 times. + * Note that, every update calls prepare, bindParam and execute APIs. + */ + public function benchUpdateWithPrepare(){ + for( $i=0; $i<1000; $i++ ){ + $stmt = PDOSqlsrvUtil::updateWithPrepare( $this->conn, $this->tableName, $this->updateValues, $this->updateParams ); + } + } + + public function dropTable(){ + PDOSqlsrvUtil::dropTable( $this->conn, $this->tableName ); + } + + public function disconnect(){ + PDOSqlsrvUtil::disconnect( $this->conn ); + } + +} diff --git a/test/Performance/benchmark/sqlsrv/large/SqlsrvFetchLargeBench.php b/test/Performance/benchmark/sqlsrv/large/SqlsrvFetchLargeBench.php new file mode 100644 index 00000000..f7dc79fb --- /dev/null +++ b/test/Performance/benchmark/sqlsrv/large/SqlsrvFetchLargeBench.php @@ -0,0 +1,31 @@ +tableName = "LargeDB.dbo.datatypes"; + } + + public function connect(){ + $this->conn = SqlsrvUtil::connect(); + } + /* + * Each iteration calls prepare, execute and fetch APIs to fetch the already populdated data + */ + public function benchFetchWithPrepare(){ + SqlsrvUtil::fetchWithPrepare( $this->conn, $this->tableName ); + } + + public function disconnect(){ + SqlsrvUtil::disconnect( $this->conn ); + } +} diff --git a/test/Performance/benchmark/sqlsrv/regular/SqlsrvConnectionBench.php b/test/Performance/benchmark/sqlsrv/regular/SqlsrvConnectionBench.php new file mode 100644 index 00000000..1a59f6db --- /dev/null +++ b/test/Performance/benchmark/sqlsrv/regular/SqlsrvConnectionBench.php @@ -0,0 +1,13 @@ +conn = SqlsrvUtil::connect(); + } + + /* + * Each iteration creates a database, a table and a stored procedure in that database and drops the database at the end. + * Not that, execDirect function are used to execute all the queries. + */ + public function benchCreateDbTableProc(){ + $randomNum = rand(); + $databaseName = "test_db_$randomNum"; + $tableName = "test_table_$randomNum"; + $procName = "test_proc_$randomNum"; + SqlsrvUtil::createDbTableProc( $this->conn, $databaseName, $tableName, $procName ); + SqlsrvUtil::dropDatabase( $this->conn, $databaseName ); + } + + public function disconnect(){ + SqlsrvUtil::disconnect( $this->conn ); + } +} diff --git a/test/Performance/benchmark/sqlsrv/regular/SqlsrvDeleteBench.php b/test/Performance/benchmark/sqlsrv/regular/SqlsrvDeleteBench.php new file mode 100644 index 00000000..e47070a5 --- /dev/null +++ b/test/Performance/benchmark/sqlsrv/regular/SqlsrvDeleteBench.php @@ -0,0 +1,53 @@ +tableName = "datatypes_".rand(); + } + + public function connect(){ + $this->conn = SqlsrvUtil::connect(); + } + + public function createTable(){ + SqlsrvUtil::createCRUDTable( $this->conn, $this->tableName ); + } + + public function generateInsertValues(){ + $this->insertValues = SqlsrvUtil::generateInsertValues(); + } + + public function insertWithPrepare(){ + for ( $i=0; $i<1000; $i++ ){ + SqlsrvUtil::insertWithPrepare( $this->conn, $this->tableName, $this->insertValues ); + } + } + /** + * Each iteration inserts 1000 rows into the table, benchDelete deletes top row from the table 1000 times. + * Note that, every delete calls prepare and execute APIs. + */ + public function benchDelete(){ + for( $i=0; $i<1000; $i++ ){ + SqlsrvUtil::delete( $this->conn, $this->tableName ); + } + } + + public function dropTable(){ + SqlsrvUtil::dropTable( $this->conn, $this->tableName ); + } + + public function disconnect(){ + SqlsrvUtil::disconnect( $this->conn ); + } + +} diff --git a/test/Performance/benchmark/sqlsrv/regular/SqlsrvFetchBench.php b/test/Performance/benchmark/sqlsrv/regular/SqlsrvFetchBench.php new file mode 100644 index 00000000..035903c2 --- /dev/null +++ b/test/Performance/benchmark/sqlsrv/regular/SqlsrvFetchBench.php @@ -0,0 +1,53 @@ +tableName = "datatypes_".rand(); + } + + public function connect(){ + $this->conn = SqlsrvUtil::connect(); + } + + public function createTable(){ + SqlsrvUtil::createCRUDTable( $this->conn, $this->tableName ); + } + + public function generateInsertValues(){ + $this->insertValues = SqlsrvUtil::generateInsertValues(); + } + + public function insertWithPrepare(){ + SqlsrvUtil::insertWithPrepare( $this->conn, $this->tableName, $this->insertValues ); + } + + /** + * Each iteration inserts a row into the table, benchFetchWithPrepare() fetches that row 1000 times. + * Note that, every fetch calls prepare, execute and fetch APIs. + */ + public function benchFetchWithPrepare(){ + for( $i=0; $i<1000; $i++){ + SqlsrvUtil::fetchWithPrepare( $this->conn, $this->tableName ); + } + } + + public function dropTable(){ + SqlsrvUtil::dropTable( $this->conn, $this->tableName ); + } + + public function disconnect(){ + SqlsrvUtil::disconnect( $this->conn ); + } +} diff --git a/test/Performance/benchmark/sqlsrv/regular/SqlsrvInsertBench.php b/test/Performance/benchmark/sqlsrv/regular/SqlsrvInsertBench.php new file mode 100644 index 00000000..b5959cdd --- /dev/null +++ b/test/Performance/benchmark/sqlsrv/regular/SqlsrvInsertBench.php @@ -0,0 +1,45 @@ +tableName = "datatypes_".rand(); + } + public function connect(){ + $this->conn = SqlsrvUtil::connect(); + } + + public function createTable(){ + SqlsrvUtil::createCRUDTable( $this->conn, $this->tableName ); + } + + public function generateInsertValues(){ + $this->insertValues = SqlsrvUtil::generateInsertValues(); + } + /** + * Each iteration inserts 1000 rows into the table. + * Note that, every insertion calls prepare, bindParam and execute APIs. + */ + public function benchInsertWithPrepare(){ + for( $i=0; $i<1000; $i++ ){ + SqlsrvUtil::insertWithPrepare( $this->conn, $this->tableName, $this->insertValues ); + } + } + + public function dropTable(){ + SqlsrvUtil::dropTable( $this->conn, $this->tableName ); + } + + public function disconnect(){ + SqlsrvUtil::disconnect( $this->conn ); + } +} diff --git a/test/Performance/benchmark/sqlsrv/regular/SqlsrvSelectVersionBench.php b/test/Performance/benchmark/sqlsrv/regular/SqlsrvSelectVersionBench.php new file mode 100644 index 00000000..9567a093 --- /dev/null +++ b/test/Performance/benchmark/sqlsrv/regular/SqlsrvSelectVersionBench.php @@ -0,0 +1,25 @@ +conn = SqlsrvUtil::connect(); + } + /* + * Each iteration calls execDirect API to fetch @@Version + */ + public function benchSelectVersion(){ + $version = SqlsrvUtil::selectVersion( $this->conn ); + } + + public function disconnect(){ + SqlsrvUtil::disconnect( $this->conn ); + } +} diff --git a/test/Performance/benchmark/sqlsrv/regular/SqlsrvUpdateBench.php b/test/Performance/benchmark/sqlsrv/regular/SqlsrvUpdateBench.php new file mode 100644 index 00000000..806c336a --- /dev/null +++ b/test/Performance/benchmark/sqlsrv/regular/SqlsrvUpdateBench.php @@ -0,0 +1,61 @@ +tableName = "datatypes_".rand(); + } + + public function connect(){ + $this->conn = SqlsrvUtil::connect(); + } + + public function createTable(){ + SqlsrvUtil::createCRUDTable( $this->conn, $this->tableName ); + } + + public function generateInsertValues(){ + $this->insertValues = SqlsrvUtil::generateInsertValues(); + } + + public function insertWithPrepare(){ + SqlsrvUtil::insertWithPrepare( $this->conn, $this->tableName, $this->insertValues ); + } + + public function generateUpdateValues(){ + $this->updateValues = SqlsrvUtil::generateUpdateValues(); + } + + public function generateUpdateParams(){ + $this->updateParams = SqlsrvUtil::generateUpdateParams(); + } + /** + * Each iteration inserts a row into the table, updateWithPrepare() updates that row 1000 times. + * Note that, every update calls prepare, bindParam and execute APIs. + */ + public function benchUpdateWithPrepare(){ + for( $i=0; $i<1000; $i++ ){ + $stmt = SqlsrvUtil::updateWithPrepare( $this->conn, $this->tableName, $this->updateValues, $this->updateParams ); + } + } + + public function dropTable(){ + SqlsrvUtil::dropTable( $this->conn, $this->tableName ); + } + + public function disconnect(){ + SqlsrvUtil::disconnect( $this->conn ); + } + +} diff --git a/test/Performance/lib/PDOSqlsrvUtil.php b/test/Performance/lib/PDOSqlsrvUtil.php new file mode 100644 index 00000000..bba6d845 --- /dev/null +++ b/test/Performance/lib/PDOSqlsrvUtil.php @@ -0,0 +1,228 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + return $conn; + } + catch( PDOException $e ){ + var_dump( $e ); + exit; + } + } + + public static function disconnect( $conn ){ + $conn = null; + } + + public static function selectVersion( $conn ){ + $sql = "SELECT @@Version"; + $stmt = self::query( $conn, $sql ); + return self::fetch( $stmt ); + } + + public static function createDbTableProc( $conn, $databaseName, $tableName, $procName ){ + $tableParams = "id INTEGER, name VARCHAR(32), value INTEGER, start_date DATE, time_added TIMESTAMP, set_time TIME(7)"; + $procParams = "@id INTEGER, @name VARCHAR(32)"; + $procTextBase = "SET NOCOUNT ON; SELECT id, name, value FROM $databaseName.$tableName WHERE id = @id AND name = @name"; + self::createDatabase( $conn, $databaseName ); + self::useDatabase( $conn, $databaseName ); + self::createTable( $conn, $tableName, $tableParams ); + self::createStoredProc( $conn, $procName, $procParams, $procTextBase ); + } + + public static function generateInsertValues(){ + $vcharVal = "test string"; + $nvcharVal = "wstring"; + $intVal = 3; + $dateTimeVal = '2016-10-31 01:39:39.7341976'; + $charVal = "fixedstr"; + $ncharVal = "fixed w string"; + $realVal = 14.2; + $binVal = 0x0123456789ABCDE; + $vbinVal = 13; + $dateTimeOffsetVal = '7032-12-17 02:32:18.5210310+00:00'; + $values = array( $vcharVal, $nvcharVal, $intVal, $dateTimeVal, $charVal, $ncharVal, $realVal, $binVal, $vbinVal, $dateTimeOffsetVal ); + return $values; + } + + public static function generateUpdateValues(){ + $vcharVal = "test string updated"; + $nvcharVal = "wstring updated"; + $intVal = 5; + $dateTimeVal = '2005-10-31 01:20:39.7341976'; + $charVal = "fixedstr updated"; + $ncharVal = "fixed w string updated"; + $realVal = 19.2; + $binVal = 0x01345789ABCDE; + $vbinVal = 18; + $dateTimeOffsetVal = '1032-12-17 02:42:18.5210310+00:00'; + $updatedValues = array( $vcharVal, $nvcharVal, $intVal, $dateTimeVal, $charVal, $ncharVal, $realVal, $binVal, $vbinVal, $dateTimeOffsetVal ); + return $updatedValues; + } + + public static function generateUpdateParams(){ + $fieldNames = array( + "vstring", + "nvstring", + "num", + "dttwo", + "string", + "nstring", + "real", + "bin", + "vbin", + "dtoffset"); + + $params = ""; + foreach( $fieldNames as $fieldName ){ + $params = $params.$fieldName."=?,"; + } + $params = rtrim($params,", "); + return $params; + } + + public static function insertWithPrepare( $conn, $tableName, $values ){ + $sql = "INSERT INTO $tableName VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + $stmt = self::prepare( $conn, $sql ); + self::bindParams( $stmt, $values ); + self::execute( $stmt ); + } + + public static function fetchWithPrepare( $conn, $tableName ){ + $sql = "SELECT * FROM $tableName"; + $stmt = self::prepare( $conn, $sql ); + self::execute( $stmt ); + while ( $row = self::fetch( $stmt )){} + } + + public static function deleteWithPrepare( $conn, $tableName ){ + $sql = "DELETE TOP (1) FROM $tableName"; + $stmt = self::prepare( $conn, $sql ); + self::execute( $stmt ); + } + + public static function updateWithPrepare( $conn, $tableName, $updateValues, $params ){ + $sql = "UPDATE $tableName SET ".$params; + $stmt = self::prepare( $conn, $sql ); + self::bindParams( $stmt, $updateValues ); + self::execute( $stmt ); + } + + private function bindParams( $stmt, $values ){ + //This functionn assumes the fields are from createCRUDTable() + self::bindParam( $stmt, 1, $values[0], PDO::PARAM_STR); + self::bindParam( $stmt, 2, $values[1], PDO::PARAM_STR); + self::bindParam( $stmt, 3, $values[2], PDO::PARAM_INT); + self::bindParam( $stmt, 4, $values[3], PDO::PARAM_STR); + self::bindParam( $stmt, 5, $values[4], PDO::PARAM_STR); + self::bindParam( $stmt, 6, $values[5], PDO::PARAM_STR); + self::bindParam( $stmt, 7, $values[6], PDO::PARAM_STR); + self::bindParam( $stmt, 8, $values[7], PDO::PARAM_LOB); + self::bindParam( $stmt, 9, $values[8], PDO::PARAM_LOB); + self::bindParam( $stmt, 10, $values[9], PDO::PARAM_STR); + } + + public static function createCRUDTable( $conn, $tableName ){ + $fields = array( + "vstring" => "VARCHAR(64)", + "nvstring" => "NVARCHAR(64)", + "num" => "int", + "dttwo" => "DATETIME2", + "string" => "CHAR(64)", + "nstring" => "NCHAR(64)", + "real" => "NUMERIC", + "bin" => "BINARY(16)", + "vbin" => "VARBINARY", + "dtoffset" => "DATETIMEOFFSET"); + $params = ""; + foreach( $fields as $fieldName => $type ){ + $params .= $fieldName." ".$type.","; + } + $params = rtrim($params,", "); + self::createTable( $conn, $tableName, $params ); + } + + private function createDatabase( $conn, $dbName ){ + $sql = "CREATE DATABASE $dbName"; + self::query( $conn, $sql ); + } + + public static function dropDatabase( $conn, $dbName ){ + $sql = "USE MASTER;DROP DATABASE $dbName"; + self::query( $conn, $sql ); + } + + public static function createTable( $conn, $tableName, $params ){ + $sql = "CREATE TABLE $tableName ($params)"; + self::query( $conn, $sql ); + } + + public static function dropTable( $conn, $tableName ){ + $sql = "DROP TABLE $tableName"; + self::query( $conn, $sql ); + } + + private function useDatabase( $conn, $dbName ){ + $sql = "USE $dbName"; + self::query( $conn, $sql ); + } + + private function createStoredProc( $conn, $procName, $params, $text ){ + $sql = "CREATE PROCEDURE $procName $params AS $text"; + self::query( $conn, $sql ); + } + + private function dropStoredProc( $conn, $procName ){ + $sql = "DROP PROCEDURE $procName"; + self::query( $conn, $sql ); + } + + private function query( $conn, $sql ){ + try{ + return $conn->query( $sql ); + } + catch( PDOException $e ){ + var_dump( $e ); + exit; + } + } + + private function fetch( $stmt ){ + return $stmt->fetch(); + } + + private function prepare( $conn, $sql ){ + try{ + $stmt = $conn->prepare( $sql ); + if( $stmt === false ){ + die( "Failed to prepare\n"); + } + return $stmt; + } + catch( PDOException $e ){ + var_dump( $e ); + exit; + } + } + + private function execute( $stmt ){ + $ret = $stmt->execute(); + if( $ret === false ){ + die( "Failed to execute\n" ); + } + } + + private function bindParam( $stmt, $index, $value, $type ){ + $ret = $stmt->bindParam( $index, $value, $type ); + if ( $ret === false){ + die( "Faild to bind\n"); + } + } +} diff --git a/test/Performance/lib/SqlsrvUtil.php b/test/Performance/lib/SqlsrvUtil.php new file mode 100644 index 00000000..e80fe8ca --- /dev/null +++ b/test/Performance/lib/SqlsrvUtil.php @@ -0,0 +1,225 @@ +$database, "UID"=>$uid, "PWD"=>$pwd, "ConnectionPooling"=>$pooling, "MultipleActiveResultSets"=>$mars ); + $conn = sqlsrv_connect( $server, $options ); + if ( $conn === false ){ + die( print_r( sqlsrv_errors(), true)); + } + return $conn; + } + + public static function disconnect( $conn ){ + if ( $conn === false || $conn === null ){ + die( print_r( "Invalid connection resource\n")); + } + sqlsrv_close( $conn ); + } + + public static function selectVersion( $conn ){ + $sql = "SELECT @@Version"; + $stmt = self::query( $conn, $sql ); + self::fetch( $stmt ); + return self::getField( $stmt, 0 ); + } + + public static function createDbTableProc( $conn, $databaseName, $tableName, $procName ){ + $tableParams = "id INTEGER, name VARCHAR(32), value INTEGER, start_date DATE, time_added TIMESTAMP, set_time TIME(7)"; + $procParams = "@id INTEGER, @name VARCHAR(32)"; + $procTextBase = "SET NOCOUNT ON; SELECT id, name, value FROM $databaseName.$tableName WHERE id = @id AND name = @name"; + self::createDatabase( $conn, $databaseName ); + self::useDatabase( $conn, $databaseName ); + self::createTable( $conn, $tableName, $tableParams ); + self::createStoredProc( $conn, $procName, $procParams, $procTextBase ); + } + + public static function generateInsertValues(){ + $vcharVal = "test string"; + $nvcharVal = "wstring"; + $intVal = 3; + $dateTimeVal = '2016-10-31 01:39:39.7341976'; + $charVal = "fixedstr"; + $ncharVal = "fixed w string"; + $realVal = 14.2; + $binVal = 0x0123456789ABCDE; + $vbinVal = 13; + $dateTimeOffsetVal = '7032-12-17 02:32:18.5210310+00:00'; + $values = array( $vcharVal, $nvcharVal, $intVal, $dateTimeVal, $charVal, $ncharVal, $realVal, $binVal, $vbinVal, $dateTimeOffsetVal ); + return $values; + } + + public static function generateUpdateValues(){ + $vcharVal = "test string updated"; + $nvcharVal = "wstring updated"; + $intVal = 5; + $dateTimeVal = '2005-10-31 01:20:39.7341976'; + $charVal = "fixedstr updated"; + $ncharVal = "fixed w string updated"; + $realVal = 19.2; + $binVal = 0x01345789ABCDE; + $vbinVal = 18; + $dateTimeOffsetVal = '1032-12-17 02:42:18.5210310+00:00'; + $updatedValues = array( $vcharVal, $nvcharVal, $intVal, $dateTimeVal, $charVal, $ncharVal, $realVal, $binVal, $vbinVal, $dateTimeOffsetVal ); + return $updatedValues; + } + + public static function generateUpdateParams(){ + $fieldNames = array( + "vstring", + "nvstring", + "num", + "dttwo", + "string", + "nstring", + "real", + "bin", + "vbin", + "dtoffset"); + + $params = ""; + foreach( $fieldNames as $fieldName ){ + $params = $params.$fieldName."=?,"; + } + $params = rtrim($params,", "); + return $params; + } + + public static function insertWithPrepare( $conn, $tableName, $values ){ + $sql = "INSERT INTO $tableName VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + $stmt = self::prepare( $conn, $sql, $values ); + self::execute( $stmt ); + } + + public static function updateWithPrepare( $conn, $tableName, $updateValues, $params ){ + $sql = "UPDATE $tableName SET ".$params; + $stmt = self::prepare( $conn, $sql, $updateValues ); + self::execute( $stmt ); + } + + public static function fetchWithPrepare( $conn, $tableName ){ + $sql = "SELECT * FROM $tableName"; + $stmt = self::prepare( $conn, $sql, array()); + self::execute( $stmt ); + while ( self::fetch( $stmt )){ + for ( $i=0; $i<10; $i++ ){ + self::getField( $stmt, $i ); + } + } + } + + public static function createCRUDTable( $conn, $tableName ){ + $fields = array( + "vstring" => "VARCHAR(64)", + "nvstring" => "NVARCHAR(64)", + "num" => "int", + "dttwo" => "DATETIME2", + "string" => "CHAR(64)", + "nstring" => "NCHAR(64)", + "real" => "NUMERIC", + "bin" => "BINARY(16)", + "vbin" => "VARBINARY", + "dtoffset" => "DATETIMEOFFSET"); + $params = ""; + foreach( $fields as $fieldName => $type ){ + $params .= $fieldName." ".$type.","; + } + $params = rtrim($params,", "); + self::createTable( $conn, $tableName, $params ); + } + + public static function query( $conn, $sql ){ + $stmt = sqlsrv_query( $conn, $sql ); + if( $stmt === false ){ + die( print_r( sqlsrv_errors(), true)); + } + return $stmt; + } + + public static function fetch( $stmt ){ + $ret = sqlsrv_fetch( $stmt ); + if( $ret === false ){ + die( print_r( sqlsrv_errors(), true)); + } + return $ret; + } + + public static function getField( $stmt, $index ){ + return sqlsrv_get_field( $stmt, $index ); + } + + private function createDatabase( $conn, $dbName ){ + $sql = "CREATE DATABASE $dbName"; + self::query( $conn, $sql ); + } + + public static function dropDatabase( $conn, $dbName ){ + $sql = "USE MASTER;DROP DATABASE $dbName"; + self::query( $conn, $sql ); + } + + public static function createTable( $conn, $tableName, $params ){ + $sql = "CREATE TABLE $tableName ($params)"; + self::query( $conn, $sql ); + } + + public static function dropTable( $conn, $tableName ){ + $sql = "DROP TABLE $tableName"; + self::query( $conn, $sql ); + } + + private function useDatabase( $conn, $dbName ){ + $sql = "USE $dbName"; + self::query( $conn, $sql ); + } + + private function createStoredProc( $conn, $procName, $params, $text ){ + $sql = "CREATE PROCEDURE $procName $params AS $text"; + self::query( $conn, $sql ); + } + + private function dropStoredProc( $conn, $procName ){ + $sql = "DROP PROCEDURE $procName"; + self::query( $conn, $sql ); + } + + private function insert( $conn, $tableName, $values ){ + $sql = "INSERT INTO $tableName values ($values)"; + self::query( $conn, $sql ); + } + + private function update( $conn, $tableName, $params, $condition ){ + $sql = "UPDATE $tableName SET $params WHERE $condition"; + self::query( $sql ); + } + + public function delete( $conn, $tableName){ + $sql = "DELETE TOP (1) FROM $tableName"; + self::query( $conn, $sql ); + } + + public function deleteWithPrepare( $conn, $tableName ){ + $sql = "DELETE TOP (1) FROM $tableName"; + $stmt = self::prepare( $conn, $sql, array() ); + self::execute( $stmt ); + } + + private function prepare( $conn, $sql, $params ){ + $stmt = sqlsrv_prepare( $conn, $sql, $params ); + if( $stmt === false ){ + die( print_r( sqlsrv_errors(), true)); + } + return $stmt; + } + + public function execute( $stmt ){ + $ret = sqlsrv_execute( $stmt ); + if ( $ret === false ){ + die( print_r( sqlsrv_errors(), true)); + } + } +} diff --git a/test/Performance/lib/connect.php b/test/Performance/lib/connect.php new file mode 100644 index 00000000..a63da87b --- /dev/null +++ b/test/Performance/lib/connect.php @@ -0,0 +1,8 @@ +