home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 32 / IOPROG_32.ISO / SOFT / SqlEval7 / devtools / samples / ODBC / mfcperf / mfcperf.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-29  |  13.7 KB  |  484 lines

  1. // MFCPerf.cpp : Defines the class behaviors for the application.
  2. //
  3. // This file is part of Microsoft SQL Server online documentation.
  4. // Copyright (C) 1992-1997 Microsoft Corporation. All rights reserved.
  5. //
  6. // This source code is an intended supplement to the Microsoft SQL
  7. // Server online references and related electronic documentation.
  8. #include "stdafx.h"
  9. #include "MFCPerf.h"
  10.  
  11. #include "MainFrm.h"
  12. #include "ChildFrm.h"
  13. #include "MFCPCust.h"
  14. #include "DocSlow.h"
  15. #include "ViewSlow.h"
  16. #include "DocFast.h"
  17. #include "ViewFast.h"
  18. #include "PerfDoc.h"
  19. #include "PerfView.h"
  20.  
  21. #define PERF_ITERATIONS     20
  22.  
  23. #ifdef _DEBUG
  24. #define new DEBUG_NEW
  25. #undef THIS_FILE
  26. static char THIS_FILE[] = __FILE__;
  27. #endif
  28.  
  29. static void PerfStart(SQLHDBC hdbc, SQLPERF** ppSQLPerf);
  30. static void PerfSnapshot(SQLHDBC hdbc, LPCTSTR szComment);
  31. static void PerfStop(SQLHDBC hdbc);
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CMFCPerfApp
  35. BEGIN_MESSAGE_MAP(CMFCPerfApp, CWinApp)
  36.     //{{AFX_MSG_MAP(CMFCPerfApp)
  37.     ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  38.     ON_COMMAND(ID_PERFTEST_RUNTEST, OnPerftestRuntest)
  39.     ON_COMMAND(ID_PERFTEST_SETLOGFILENAME, OnPerftestSetlogfilename)
  40.     //}}AFX_MSG_MAP
  41.     // Standard file based document commands
  42.     ON_COMMAND(ID_FILE_NEW_FAST, CMFCPerfApp::OnFileNewFast)
  43.     ON_COMMAND(ID_FILE_NEW_SLOW, CMFCPerfApp::OnFileNewSlow)
  44. END_MESSAGE_MAP()
  45.  
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CMFCPerfApp construction
  48. CMFCPerfApp::CMFCPerfApp()
  49.     {
  50.     }
  51.  
  52. /////////////////////////////////////////////////////////////////////////////
  53. // The one and only CMFCPerfApp object
  54. CMFCPerfApp theApp;
  55.  
  56. // Global font objects.
  57. CFont       g_fontEdits;
  58. CFont       g_fontStatics;
  59.  
  60. // Global statistics log file name.
  61. CString     g_strLogFN = _T("");
  62.  
  63. /////////////////////////////////////////////////////////////////////////////
  64. // CMFCPerfApp initialization
  65. BOOL CMFCPerfApp::InitInstance()
  66.     {
  67.     // Initialize OLE control containing for Grid
  68.     AfxEnableControlContainer();
  69.  
  70.     // Standard initialization
  71.     // If you are not using these features and wish to reduce the size
  72.     //  of your final executable, you should remove from the following
  73.     //  the specific initialization routines you do not need.
  74.  
  75. #ifdef _AFXDLL
  76.     Enable3dControls();            // Call this when using MFC in a shared DLL
  77. #else
  78.     Enable3dControlsStatic();    // Call this when linking to MFC statically
  79. #endif
  80.  
  81.     LoadStdProfileSettings();  // Load standard INI file options (including MRU)
  82.  
  83.     // Register the application's document templates.  Document templates
  84.     //  serve as the connection between documents, frame windows and views.
  85.  
  86.     CMultiDocTemplate* pDocTemplate;
  87.     pDocTemplate = new CMultiDocTemplate(
  88.         IDR_MFCPERTYPE,
  89.         RUNTIME_CLASS(CMFCPDocSlow),
  90.         RUNTIME_CLASS(CChildFrame), // custom MDI child frame
  91.         RUNTIME_CLASS(CMFCPViewSlow));
  92.     AddDocTemplate(pDocTemplate);
  93.  
  94.     pDocTemplate = new CMultiDocTemplate(
  95.         IDR_MFCPERFAST,
  96.         RUNTIME_CLASS(CMFCPDocFast),
  97.         RUNTIME_CLASS(CChildFrame), // custom MDI child frame
  98.         RUNTIME_CLASS(CMFCPViewFast));
  99.     AddDocTemplate(pDocTemplate);
  100.  
  101.     pDocTemplate = new CMultiDocTemplate(
  102.         IDR_PERFSTATS,
  103.         RUNTIME_CLASS(CPerfStatsDoc),
  104.         RUNTIME_CLASS(CChildFrame), // custom MDI child frame
  105.         RUNTIME_CLASS(CPerfStatsView));
  106.     AddDocTemplate(pDocTemplate);
  107.  
  108.     // create main MDI Frame window
  109.     CMainFrame* pMainFrame = new CMainFrame;
  110.     if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  111.         return FALSE;
  112.     m_pMainWnd = pMainFrame;
  113.  
  114.     // Replace standard processing for MFC database application.
  115.     //  Simply show the application main window.
  116.     m_nCmdShow = SW_SHOWNORMAL;
  117.     pMainFrame->ShowWindow(m_nCmdShow);
  118.     pMainFrame->UpdateWindow();
  119.  
  120.     // Create the fonts for the form's edit and static controls.
  121.     CDC* pDC = m_pMainWnd->GetDC();
  122.     
  123.     g_fontEdits.CreateFont(-MulDiv(10, pDC->GetDeviceCaps(LOGPIXELSY), 72),
  124.         0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET,
  125.         OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
  126.         DEFAULT_PITCH, "Arial");
  127.     g_fontStatics.CreateFont(-MulDiv(8, pDC->GetDeviceCaps(LOGPIXELSY), 72),
  128.         0, 0, 0, FW_THIN, FALSE, FALSE, FALSE, ANSI_CHARSET,
  129.         OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
  130.         DEFAULT_PITCH, "Arial");
  131.  
  132.     m_pMainWnd->ReleaseDC(pDC);
  133.  
  134.     return TRUE;
  135.     }
  136.  
  137. /////////////////////////////////////////////////////////////////////////////
  138. // CAboutDlg dialog used for App About
  139. class CAboutDlg : public CDialog
  140.     {
  141. public:
  142.     CAboutDlg();
  143.  
  144. // Dialog Data
  145.     //{{AFX_DATA(CAboutDlg)
  146.     enum { IDD = IDD_ABOUTBOX };
  147.     //}}AFX_DATA
  148.  
  149.     // ClassWizard generated virtual function overrides
  150.     //{{AFX_VIRTUAL(CAboutDlg)
  151.     protected:
  152.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  153.     //}}AFX_VIRTUAL
  154.  
  155. // Implementation
  156. protected:
  157.     //{{AFX_MSG(CAboutDlg)
  158.         // No message handlers
  159.     //}}AFX_MSG
  160.     DECLARE_MESSAGE_MAP()
  161.     };
  162.  
  163. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  164.     {
  165.     //{{AFX_DATA_INIT(CAboutDlg)
  166.     //}}AFX_DATA_INIT
  167.     }
  168.  
  169. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  170.     {
  171.     CDialog::DoDataExchange(pDX);
  172.     //{{AFX_DATA_MAP(CAboutDlg)
  173.     //}}AFX_DATA_MAP
  174.     }
  175.  
  176. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  177.     //{{AFX_MSG_MAP(CAboutDlg)
  178.         // No message handlers
  179.     //}}AFX_MSG_MAP
  180. END_MESSAGE_MAP()
  181.  
  182. // App command to run the dialog
  183. void CMFCPerfApp::OnAppAbout()
  184.     {
  185.     CAboutDlg aboutDlg;
  186.     aboutDlg.DoModal();
  187.     }
  188.  
  189. /////////////////////////////////////////////////////////////////////////////
  190. // CLogFileDlg dialog used to set the name of the user's stats log file.
  191. class CLogFileDlg : public CDialog
  192. {
  193. // Construction
  194. public:
  195.     CLogFileDlg(CWnd* pParent = NULL);   // standard constructor
  196.  
  197. // Dialog Data
  198.     //{{AFX_DATA(CLogFileDlg)
  199.     enum { IDD = IDD_DIALOG_LOGFN };
  200.     CString    m_editLogFN;
  201.     //}}AFX_DATA
  202.  
  203.  
  204. // Overrides
  205.     // ClassWizard generated virtual function overrides
  206.     //{{AFX_VIRTUAL(CLogFileDlg)
  207.     protected:
  208.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  209.     //}}AFX_VIRTUAL
  210.  
  211. // Implementation
  212. protected:
  213.  
  214.     // Generated message map functions
  215.     //{{AFX_MSG(CLogFileDlg)
  216.         // NOTE: the ClassWizard will add member functions here
  217.     //}}AFX_MSG
  218.     DECLARE_MESSAGE_MAP()
  219. };
  220.  
  221. /////////////////////////////////////////////////////////////////////////////
  222. // CLogFileDlg dialog
  223. CLogFileDlg::CLogFileDlg(CWnd* pParent /*=NULL*/)
  224.     : CDialog(CLogFileDlg::IDD, pParent)
  225.     {
  226.     //{{AFX_DATA_INIT(CLogFileDlg)
  227.     m_editLogFN = _T("");
  228.     //}}AFX_DATA_INIT
  229.     }
  230.  
  231. void CLogFileDlg::DoDataExchange(CDataExchange* pDX)
  232.     {
  233.     CDialog::DoDataExchange(pDX);
  234.     //{{AFX_DATA_MAP(CLogFileDlg)
  235.     DDX_Text(pDX, IDC_EDIT_LOGFN, m_editLogFN);
  236.     DDV_MaxChars(pDX, m_editLogFN, 256);
  237.     //}}AFX_DATA_MAP
  238.     }
  239.  
  240. BEGIN_MESSAGE_MAP(CLogFileDlg, CDialog)
  241.     //{{AFX_MSG_MAP(CLogFileDlg)
  242.         // NOTE: the ClassWizard will add message map macros here
  243.     //}}AFX_MSG_MAP
  244. END_MESSAGE_MAP()
  245.  
  246. /////////////////////////////////////////////////////////////////////////////
  247. // CMFCPerfApp commands
  248. void CMFCPerfApp::OnFileNewFast()
  249.     {
  250.     CDocTemplate* pDocTemplate;
  251.  
  252.     POSITION pos = GetFirstDocTemplatePosition();
  253.  
  254.     pDocTemplate = (CDocTemplate*) GetNextDocTemplate(pos);
  255.     pDocTemplate = (CDocTemplate*) GetNextDocTemplate(pos);
  256.     pDocTemplate->OpenDocumentFile(NULL);
  257.     }
  258.  
  259. void CMFCPerfApp::OnFileNewSlow()
  260.     {
  261.     CDocTemplate* pDocTemplate;
  262.  
  263.     POSITION pos = GetFirstDocTemplatePosition();
  264.  
  265.     pDocTemplate = (CDocTemplate*) GetNextDocTemplate(pos);
  266.     pDocTemplate->OpenDocumentFile(NULL);
  267.     }
  268.  
  269. void CMFCPerfApp::OnPerftestRuntest() 
  270.     {
  271.     CDocTemplate*   pDocTemplate;
  272.     CMFCPDocFast*   pDocFast;
  273.     CMFCPViewFast*  pViewFast;
  274.     CMFCPDocSlow*   pDocSlow;
  275.     CMFCPViewSlow*  pViewSlow;
  276.     CMDIChildWnd*   pChild;
  277.     UINT            i;
  278.     POSITION        pos;
  279.     SQLPERF*        pperf;
  280.     CPerfStatsDoc*  pDocStats;
  281.  
  282.     CWaitCursor     WaitForIt;
  283.  
  284.     // Get the performance stats document.
  285.     pos = GetFirstDocTemplatePosition();
  286.     pDocTemplate = (CDocTemplate*) GetNextDocTemplate(pos);
  287.     pDocTemplate = (CDocTemplate*) GetNextDocTemplate(pos);
  288.     pDocTemplate = (CDocTemplate*) GetNextDocTemplate(pos);
  289.     pDocStats = (CPerfStatsDoc*) pDocTemplate->OpenDocumentFile(NULL);
  290.  
  291.     // Do slow...
  292.     pos = GetFirstDocTemplatePosition();
  293.     pDocTemplate = (CDocTemplate*) GetNextDocTemplate(pos);
  294.  
  295.     // Check for cancel on login dialog box or other error.
  296.     if ((pDocSlow = (CMFCPDocSlow*) pDocTemplate->OpenDocumentFile(NULL))
  297.         == NULL)
  298.         {
  299.         return;
  300.         }
  301.  
  302.     // Check for login to correct database.
  303.     if (!pDocSlow->m_Database.IsOpen())
  304.         {
  305.         pDocSlow->OnCloseDocument();
  306.         return;
  307.         }
  308.  
  309.     // Get the view (child contains handling for CRecordset notifications).
  310.     pChild = ((CMDIFrameWnd*) m_pMainWnd)->MDIGetActive();
  311.     pViewSlow = (CMFCPViewSlow*) pChild->GetActiveView();
  312.     pViewSlow->SetRedraw(FALSE);
  313.  
  314.     // Start performance data logging.
  315.     PerfStart(pDocSlow->m_Database.m_hdbc, &pperf);
  316.  
  317.     // Animate the child.
  318.     for (i = 0; i < PERF_ITERATIONS; i++)
  319.         {
  320.         pViewSlow->OnMove(ID_RECORD_NEXT);
  321.         }
  322.  
  323.     // Snapshot the data, record it in the statistics doc, and then close the
  324.     //  stats source doc.
  325.     PerfSnapshot(pDocSlow->m_Database.m_hdbc, NULL);
  326.     PerfStop(pDocSlow->m_Database.m_hdbc);
  327.     memcpy((void*) &(pDocStats->m_perfSlow), (void*) (pperf), sizeof(SQLPERF));
  328.     pDocSlow->OnCloseDocument();
  329.  
  330.     // Do Fast...
  331.     pDocTemplate = (CDocTemplate*) GetNextDocTemplate(pos);
  332.  
  333.     // Check for cancel on login dialog box or other error.
  334.     if ((pDocFast = (CMFCPDocFast*) pDocTemplate->OpenDocumentFile(NULL))
  335.         == NULL)
  336.         {
  337.         return;
  338.         }
  339.  
  340.     // Check for login to correct database.
  341.     if (!pDocFast->m_Database.IsOpen())
  342.         {
  343.         pDocFast->OnCloseDocument();
  344.         return;
  345.         }
  346.  
  347.     // Get the view (child contains handling for CRecordset notifications).
  348.     pChild = ((CMDIFrameWnd*) m_pMainWnd)->MDIGetActive();
  349.     pViewFast = (CMFCPViewFast*) pChild->GetActiveView();
  350.     pViewFast->SetRedraw(FALSE);
  351.  
  352.     // Start performance data logging.
  353.     PerfStart(pDocFast->m_Database.m_hdbc, &pperf);
  354.  
  355.     // Animate the child.
  356.     for (i = 0; i < PERF_ITERATIONS; i++)
  357.         {
  358.         pViewFast->OnMove(ID_RECORD_NEXT);
  359.         }
  360.  
  361.     // Snapshot the data, record it in the statistics doc, and then close the
  362.     //  stats source doc.
  363.     PerfSnapshot(pDocFast->m_Database.m_hdbc, NULL);
  364.     PerfStop(pDocFast->m_Database.m_hdbc);
  365.     memcpy((void*) &(pDocStats->m_perfFast), (void*) (pperf), sizeof(SQLPERF));
  366.     pDocFast->OnCloseDocument();
  367.  
  368.     // Display the stats document...
  369.     pDocStats->SetModifiedFlag();
  370.     pDocStats->UpdateAllViews(NULL);
  371.     }
  372.  
  373. void CMFCPerfApp::OnPerftestSetlogfilename() 
  374.     {
  375.     CLogFileDlg dlg;
  376.  
  377.     dlg.m_editLogFN = g_strLogFN;
  378.  
  379.     if (dlg.DoModal() == IDOK)
  380.         {
  381.         g_strLogFN = dlg.m_editLogFN;
  382.         }
  383.     }
  384.  
  385. /////////////////////////////////////////////////////////////////////////////
  386. // Global Functions
  387. void SetForm
  388.     (
  389.     CWnd*       wndFrame,
  390.     CEdit&      editCompany,
  391.     CEdit&      editCountry,
  392.     CMSFlexGrid& gridOrders,
  393.     CMSFlexGrid& gridDetails,
  394.     CStatic&    staticClick,
  395.     CStatic&    staticToSee
  396.     )
  397.     {
  398.     RECT        rectGrid;
  399.     RECT        rectFrame;
  400.  
  401.     gridDetails.GetWindowRect(&rectGrid);
  402.     wndFrame->GetWindowRect(&rectFrame);
  403.  
  404.     rectFrame.bottom = (rectGrid.bottom - rectFrame.top) + 16;
  405.     rectFrame.right = rectFrame.right - rectFrame.left;
  406.     rectFrame.top = 0;
  407.     rectFrame.left = 0;
  408.  
  409.     wndFrame->MoveWindow(&rectFrame);
  410.  
  411.     editCompany.SetFont((CFont*) &g_fontEdits);
  412.     editCountry.SetFont((CFont*) &g_fontEdits);
  413.  
  414.     staticClick.SetFont((CFont*) &g_fontStatics);
  415.     staticToSee.SetFont((CFont*) &g_fontStatics);
  416.  
  417.     gridOrders.SetColWidth(0, 300);
  418.     gridDetails.SetColWidth(0, 300);
  419.  
  420.     gridOrders.SetColWidth(1, 1215);
  421.     gridOrders.SetColWidth(2, 2040);
  422.     gridOrders.SetColWidth(3, 2040);
  423.     gridOrders.SetColWidth(4, 2040);
  424.  
  425.     gridDetails.SetColWidth(1, 2880);
  426.     gridDetails.SetColWidth(2, 1170);
  427.     gridDetails.SetColWidth(3, 1170);
  428.     gridDetails.SetColWidth(4, 1170);
  429.     gridDetails.SetColWidth(5, 1170);
  430.     }
  431.  
  432. // Take a snapshot of the performance data. MFC is an ODBC 2.x consumer; use
  433. //  ODBC 2.x SQLSetConnectOption, not 3.x SQLSetConnectAttr.
  434. void PerfSnapshot
  435.     (
  436.     SQLHDBC hdbc,
  437.     LPCTSTR szComment
  438.     )
  439.     {
  440.     SQLSetConnectOption(hdbc, SQL_COPT_SS_PERF_DATA_LOG_NOW,
  441.         (UDWORD) ((void*) szComment));
  442.     }
  443.  
  444. // Take a snapshot of the performance data.
  445. void PerfStart
  446.     (
  447.     SQLHDBC hdbc,
  448.     SQLPERF** ppSQLPerf
  449.     )
  450.     {
  451.     if (!g_strLogFN.GetLength())
  452.         {
  453.         g_strLogFN = DEFAULT_LOGFILE;
  454.         }
  455.  
  456.     // Check to see if logging is already enabled on the connection. If
  457.     //  logging is on, disable and enable logging to clear log buffers
  458.     //  for our use.
  459.     SQLGetConnectOption(hdbc, SQL_COPT_SS_PERF_DATA, 
  460.         (SQLPOINTER) ppSQLPerf);
  461.     if (*ppSQLPerf != NULL)
  462.         {
  463.         SQLSetConnectOption(hdbc, SQL_COPT_SS_PERF_DATA,
  464.             SQL_PERF_STOP);
  465.         }
  466.     else
  467.         {
  468.         SQLSetConnectOption(hdbc, SQL_COPT_SS_PERF_DATA_LOG,
  469.             (UDWORD) ((LPCTSTR) g_strLogFN));
  470.         }
  471.  
  472.     SQLSetConnectOption(hdbc, SQL_COPT_SS_PERF_DATA,
  473.         SQL_PERF_START);
  474.     SQLGetConnectOption(hdbc, SQL_COPT_SS_PERF_DATA, 
  475.         (SQLPOINTER) ppSQLPerf);
  476.     }
  477.  
  478. void PerfStop
  479.     (
  480.     SQLHDBC hdbc
  481.     )
  482.     {
  483.     SQLSetConnectOption(hdbc, SQL_COPT_SS_PERF_DATA, SQL_PERF_STOP);
  484.     }