To log driver performance data using ODBC Administrator
To log driver performance data programmatically
"C:\\Odbcperf.log"
To pull driver performance data into an application
The SQLPERF structure is defined in Odbcss.h as follows:
typedef struct sqlperf
{
// Application profile statistics
DWORD TimerResolution;
DWORD SQLidu;
DWORD SQLiduRows;
DWORD SQLSelects;
DWORD SQLSelectRows;
DWORD Transactions;
DWORD SQLPrepares;
DWORD ExecDirects;
DWORD SQLExecutes;
DWORD CursorOpens;
DWORD CursorSize;
DWORD CursorUsed;
LDOUBLE PercentCursorUsed;
LDOUBLE AvgFetchTime;
LDOUBLE AvgCursorSize;
LDOUBLE AvgCursorUsed;
DWORD SQLFetchTime;
DWORD SQLFetchCount;
DWORD CurrentStmtCount;
DWORD MaxOpenStmt;
DWORD SumOpenStmt;
// Connection statistics
DWORD CurrentConnectionCount;
DWORD MaxConnectionsOpened;
DWORD SumConnectionsOpened;
DWORD SumConnectionTime;
LDOUBLE AvgTimeOpened;
// Network statistics
DWORD ServerRndTrips;
DWORD BuffersSent;
DWORD BuffersRec;
DWORD BytesSent;
DWORD BytesRec;
// Time statistics
DWORD msExecutionTime;
DWORD msNetworkServerTime;
} SQLPERF;
This example shows both the creation of a performance data log file and displaying performance data directly from the SQLPERF data structure. Error-checking code was removed to simplify this example.
// Sample showing the SQL Server ODBC driver-specific options
// to record performance statistics. The sample creates
// one file:
//
// C:\Odbcperf.log contains performance statistics and
// can be imported to a spreadsheet program such as Microsoft Excel
// as a tab-delimited file for analysis.
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <odbcss.h>
SQLHENV henv = SQL_NULL_HENV;
SQLHDBC hdbc1 = SQL_NULL_HDBC;
SQLHSTMT hstmt1 = SQL_NULL_HSTMT;
int main() {
RETCODE retcode;
// Pointer to the ODBC driver performance structure.
SQLPERF *PerfPtr;
SQLINTEGER cbPerfPtr;
// Allocate the ODBC environment and save handle.
retcode = SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv);
// Let ODBC know this is an ODBC 3.0 app.
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER);
// Allocate ODBC connection handle and connect.
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);
retcode = SQLConnect(hdbc1, "MyDSN", SQL_NTS,
"sa", SQL_NTS, "MyPassWord", SQL_NTS);
// Set options to log performance statistics.
// Specify file to use for the log.
retcode = SQLSetConnectAttr
(hdbc1,
SQL_COPT_SS_PERF_DATA_LOG,
&"c:\\odbcperf.log",
SQL_NTS);
// Start the performance statistics log.
retcode = SQLSetConnectAttr
(hdbc1,
SQL_COPT_SS_PERF_DATA,
(SQLPOINTER)SQL_PERF_START,
SQL_IS_UINTEGER);
// Allocate statement handle, then execute command.
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc1, &hstmt1);
retcode = SQLExecDirect(hstmt1,
"SELECT * FROM pubs.dbo.authors", SQL_NTS);
// Clear any result sets generated.
while ( ( retcode = SQLMoreResults(hstmt1) ) != SQL_NO_DATA )
;
retcode = SQLExecDirect(hstmt1,
"SELECT * FROM pubs.dbo.stores", SQL_NTS);
// Clear any result sets generated.
while ( ( retcode = SQLMoreResults(hstmt1) ) != SQL_NO_DATA )
;
// Generate a long-running query.
retcode = SQLExecDirect(hstmt1,
"waitfor delay '00:00:04' ", SQL_NTS);
// Clear any result sets generated.
while ( ( retcode = SQLMoreResults(hstmt1) ) != SQL_NO_DATA )
;
// Write current statistics to the performance log.
retcode = SQLSetConnectAttr
(hdbc1,
SQL_COPT_SS_PERF_DATA_LOG_NOW,
(SQLPOINTER)NULL,
SQL_IS_UINTEGER);
// Get pointer to current SQLPerf structure.
// Print a couple of statistics.
retcode = SQLGetConnectAttr
(hdbc1,
SQL_COPT_SS_PERF_DATA,
(SQLPOINTER)&PerfPtr,
SQL_IS_POINTER,
&cbPerfPtr);
printf("SQLSelects = %d, SQLSelectRows = %d\n",
PerfPtr->SQLSelects, PerfPtr->SQLSelectRows);
/* Clean up. */
SQLFreeHandle(SQL_HANDLE_STMT, hstmt1);
SQLDisconnect(hdbc1);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
return(0);
}
Profiling ODBC Driver Performance | SQLSetConnectAttr |
SQLGetConnectAttr |