Performance tests - initial commit

This commit is contained in:
ulvii 2017-05-05 17:32:09 -07:00
parent ae0368f8d2
commit 8631a51988
19 changed files with 1082 additions and 0 deletions

View file

@ -0,0 +1,31 @@
<?php
use PDOSqlsrvPerfTest\PDOSqlsrvUtil;
/**
* @BeforeMethods({"connect", "setTableName" })
* @AfterMethods({ "disconnect"})
*/
class PDOFetchLargeBench{
private $conn;
private $tableName;
public function setTableName(){
//Assumes the table is already populated with data
$this->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 );
}
}

View file

@ -0,0 +1,14 @@
<?php
use PDOSqlsrvPerfTest\PDOSqlsrvUtil;
class PDOConnectionBench{
/*
* Opens a connection and closes it immediately
*/
public function benchConnectAndDisconnect(){
$conn = PDOSqlsrvUtil::connect();
PDOSqlsrvUtil::disconnect( $conn );
}
}
?>

View file

@ -0,0 +1,30 @@
<?php
use PDOSqlsrvPerfTest\PDOSqlsrvUtil;
/**
* @BeforeMethods({"connect"})
* @AfterMethods({"disconnect"})
*/
class PDOCreateDbTableProcBench{
private $conn;
public function connect(){
$this->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 );
}
}

View file

@ -0,0 +1,53 @@
<?php
use PDOSqlsrvPerfTest\PDOSqlsrvUtil;
/**
* @BeforeMethods({"connect", "setTableName", "createTable", "generateInsertValues", "insertWithPrepare"})
* @AfterMethods({ "dropTable","disconnect"})
*/
class PDODeleteBench{
private $conn;
private $tableName;
private $insertValues;
public function setTableName(){
$this->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 );
}
}

View file

@ -0,0 +1,50 @@
<?php
use PDOSqlsrvPerfTest\PDOSqlsrvUtil;
/**
* @BeforeMethods({"connect", "setTableName", "createTable", "generateInsertValues", "insertWithPrepare"})
* @AfterMethods({"dropTable", "disconnect"})
*/
class PDOFetchBench{
private $conn;
private $tableName;
private $insertValues;
public function setTableName(){
$this->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 );
}
}

View file

@ -0,0 +1,45 @@
<?php
use PDOSqlsrvPerfTest\PDOSqlsrvUtil;
/**
* @BeforeMethods({"connect", "setTableName", "createTable", "generateInsertValues"})
* @AfterMethods({ "dropTable", "disconnect"})
*/
class PDOInsertBench{
private $conn;
private $tableName;
private $insertValues;
public function setTableName(){
$this->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 );
}
}

View file

@ -0,0 +1,25 @@
<?php
use PDOSqlsrvPerfTest\PDOSqlsrvUtil;
/**
* @BeforeMethods({"connect"})
* @AfterMethods({"disconnect"})
*/
class PDOSelectVersionBench{
private $conn;
public function connect(){
$this->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 );
}
}

View file

@ -0,0 +1,61 @@
<?php
use PDOSqlsrvPerfTest\PDOSqlsrvUtil;
/**
* @BeforeMethods({"connect", "setTableName", "createTable", "generateInsertValues", "insertWithPrepare", "generateUpdateValues", "generateUpdateParams"})
* @AfterMethods({"dropTable", "disconnect"})
*/
class PDOUpdateBench{
private $conn;
private $tableName;
private $insertValues;
private $updateValues;
private $updateParams;
public function setTableName(){
$this->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 );
}
}

View file

@ -0,0 +1,31 @@
<?php
use SqlsrvPerfTest\SqlsrvUtil;
/**
* @BeforeMethods({"connect", "setTableName" })
* @AfterMethods({ "disconnect"})
*/
class SqlsrvFetchLargeBench{
private $conn;
private $tableName;
public function setTableName(){
//Assumes the table is already populated with data
$this->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 );
}
}

View file

@ -0,0 +1,13 @@
<?php
use SqlsrvPerfTest\SqlsrvUtil;
class SqlsrvConnectionBench{
/*
* Opens a connection and closes it immediately
*/
public function benchConnectAndDisconnect(){
$conn = SqlsrvUtil::connect();
SqlsrvUtil::disconnect( $conn );
}
}

View file

@ -0,0 +1,31 @@
<?php
use SqlsrvPerfTest\SqlsrvUtil;
/**
* @BeforeMethods({"connect"})
* @AfterMethods({"disconnect"})
*/
class SqlsrvCreateDbTableProcBench{
private $conn;
public function connect(){
$this->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 );
}
}

View file

@ -0,0 +1,53 @@
<?php
use SqlsrvPerfTest\SqlsrvUtil;
/**
* @BeforeMethods({"connect", "setTableName", "createTable", "generateInsertValues", "insertWithPrepare"})
* @AfterMethods({ "dropTable", "disconnect"})
*/
class SqlsrvDeleteBench{
private $conn;
private $tableName;
private $insertValues;
public function setTableName(){
$this->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 );
}
}

View file

@ -0,0 +1,53 @@
<?php
use SqlsrvPerfTest\SqlsrvUtil;
/**
* @BeforeMethods({"connect", "setTableName", "createTable", "generateInsertValues", "insertWithPrepare"})
* @AfterMethods({ "dropTable", "disconnect"})
*/
class SqlsrvFetchBench{
private $conn;
private $tableName;
private $insertValues;
public function setTableName(){
$this->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 );
}
}

View file

@ -0,0 +1,45 @@
<?php
use SqlsrvPerfTest\SqlsrvUtil;
/**
* @BeforeMethods({"connect", "setTableName", "createTable", "generateInsertValues"})
* @AfterMethods({"dropTable","disconnect"})
*/
class SqlsrvInsertBench{
private $conn;
private $tableName;
private $insertValues;
public function setTableName(){
$this->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 );
}
}

View file

@ -0,0 +1,25 @@
<?php
use SqlsrvPerfTest\SqlsrvUtil;
/**
* @BeforeMethods({"connect"})
* @AfterMethods({"disconnect"})
*/
class SqlsrvSelectVersionBench{
private $conn;
public function connect(){
$this->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 );
}
}

View file

@ -0,0 +1,61 @@
<?php
use SqlsrvPerfTest\SqlsrvUtil;
/**
* @BeforeMethods({"connect", "setTableName", "createTable", "generateInsertValues", "insertWithPrepare", "generateUpdateValues", "generateUpdateParams"})
* @AfterMethods({ "dropTable", "disconnect"})
*/
class SqlsrvUpdateBench{
private $conn;
private $tableName;
private $insertValues;
private $updateValues;
private $updateParams;
public function setTableName(){
$this->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 );
}
}

View file

@ -0,0 +1,228 @@
<?php
namespace PDOSqlsrvPerfTest;
use PDO;
class PDOSqlsrvUtil{
public static function connect(){
require dirname(__FILE__).DIRECTORY_SEPARATOR.'connect.php';
try{
$conn = new PDO( "sqlsrv:Server=$server; Database=$database; ConnectionPooling=$pooling; MultipleActiveResultSets=$mars" , $uid, $pwd );
$conn->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");
}
}
}

View file

@ -0,0 +1,225 @@
<?php
namespace SqlsrvPerfTest;
class SqlsrvUtil{
public static function connect(){
require dirname(__FILE__).DIRECTORY_SEPARATOR.'connect.php';
$options = array( "Database"=>$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));
}
}
}

View file

@ -0,0 +1,8 @@
<?php
$server = 'PT-006-SQL-2K16.galaxy.ad';
$database = 'tempdb';
$uid = 'sa';
$pwd = 'Moonshine4me';
$pooling=false;
$mars=false;
?>