]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/benchmarks/bench.cpp
Ensure that detached menus don't keep focus grab in wxGTK.
[wxWidgets.git] / tests / benchmarks / bench.cpp
index ea9f5c9bbd5d325c588f1893bebb097e388426f9..2662554ef897284458088239a651ec79b6ab3bd7 100644 (file)
@@ -5,7 +5,7 @@
 // Created:     2008-07-19
 // RCS-ID:      $Id$
 // Copyright:   (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
-// Licence:     wxWindows license
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 // ============================================================================
 // ----------------------------------------------------------------------------
 
 static const char OPTION_LIST = 'l';
+static const char OPTION_SINGLE = '1';
 
 static const char OPTION_AVG_COUNT = 'a';
 static const char OPTION_NUM_RUNS = 'n';
+static const char OPTION_NUMERIC_PARAM = 'p';
+static const char OPTION_STRING_PARAM = 's';
 
 // ----------------------------------------------------------------------------
 // BenchApp declaration
@@ -53,6 +56,10 @@ public:
     virtual int  OnRun();
     virtual int  OnExit();
 
+    // accessors
+    int GetNumericParameter() const { return m_numParam; }
+    const wxString& GetStringParameter() const { return m_strParam; }
+
 private:
     // list all registered benchmarks
     void ListBenchmarks();
@@ -60,25 +67,38 @@ private:
     // command lines options/parameters
     wxSortedArrayString m_toRun;
     long m_numRuns,
-         m_avgCount;
+         m_avgCount,
+         m_numParam;
+    wxString m_strParam;
 };
 
+IMPLEMENT_APP_CONSOLE(BenchApp)
+
 // ============================================================================
-// Bench::Function implementation
+// Bench namespace symbols implementation
 // ============================================================================
 
 Bench::Function *Bench::Function::ms_head = NULL;
 
+long Bench::GetNumericParameter()
+{
+    return wxGetApp().GetNumericParameter();
+}
+
+wxString Bench::GetStringParameter()
+{
+    return wxGetApp().GetStringParameter();
+}
+
 // ============================================================================
 // BenchApp implementation
 // ============================================================================
 
-IMPLEMENT_APP_CONSOLE(BenchApp)
-
 BenchApp::BenchApp()
 {
     m_avgCount = 10;
     m_numRuns = 10000; // just some default (TODO: switch to time-based one)
+    m_numParam = 0;
 }
 
 bool BenchApp::OnInit()
@@ -105,14 +125,42 @@ void BenchApp::OnInitCmdLine(wxCmdLineParser& parser)
                      "list",
                      "list all the existing benchmarks");
 
+    parser.AddSwitch(OPTION_SINGLE,
+                     "single",
+                     "run the benchmark once only");
+
     parser.AddOption(OPTION_AVG_COUNT,
                      "avg-count",
-                     "number of times to run benchmarking loop",
+                     wxString::Format
+                     (
+                        "number of times to run benchmarking loop (default: %ld)",
+                        m_avgCount
+                     ),
                      wxCMD_LINE_VAL_NUMBER);
     parser.AddOption(OPTION_NUM_RUNS,
                      "num-runs",
-                     "number of times to run each benchmark in a loop",
+                     wxString::Format
+                     (
+                         "number of times to run each benchmark in a loop "
+                         "(default: %ld)",
+                         m_numRuns
+                     ),
+                     wxCMD_LINE_VAL_NUMBER);
+    parser.AddOption(OPTION_NUMERIC_PARAM,
+                     "num-param",
+                     wxString::Format
+                     (
+                         "numeric parameter used by some benchmark functions "
+                         "(default: %ld)",
+                         m_numParam
+                     ),
                      wxCMD_LINE_VAL_NUMBER);
+    parser.AddOption(OPTION_STRING_PARAM,
+                     "str-param",
+                     "string parameter used by some benchmark functions "
+                     "(default: empty)",
+                     wxCMD_LINE_VAL_STRING);
+
     parser.AddParam("benchmark name",
                     wxCMD_LINE_VAL_STRING,
                     wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE);
@@ -137,8 +185,25 @@ bool BenchApp::OnCmdLineParsed(wxCmdLineParser& parser)
         return false;
     }
 
-    parser.Found(OPTION_AVG_COUNT, &m_avgCount);
-    parser.Found(OPTION_NUM_RUNS, &m_numRuns);
+    bool numRunsSpecified = false;
+    if ( parser.Found(OPTION_AVG_COUNT, &m_avgCount) )
+        numRunsSpecified = true;
+    if ( parser.Found(OPTION_NUM_RUNS, &m_numRuns) )
+        numRunsSpecified = true;
+    parser.Found(OPTION_NUMERIC_PARAM, &m_numParam);
+    parser.Found(OPTION_STRING_PARAM, &m_strParam);
+    if ( parser.Found(OPTION_SINGLE) )
+    {
+        if ( numRunsSpecified )
+        {
+            wxFprintf(stderr, "Incompatible options specified.\n");
+
+            return false;
+        }
+
+        m_avgCount =
+        m_numRuns = 1;
+    }
 
     // construct sorted array for quick verification of benchmark names
     wxSortedArrayString benchmarks;
@@ -174,13 +239,23 @@ int BenchApp::OnRun()
         if ( m_toRun.Index(func->GetName()) == wxNOT_FOUND )
             continue;
 
-        wxPrintf("Benchmarking %s: ", func->GetName());
+        wxString params;
+        if ( m_numParam )
+            params += wxString::Format(" with N=%ld", m_numParam);
+        if ( !m_strParam.empty() )
+        {
+            if ( !params.empty() )
+                params += " and";
+            params += wxString::Format(" with s=\"%s\"", m_strParam);
+        }
+
+        wxPrintf("Benchmarking %s%s: ", func->GetName(), params);
 
         long timeMin = LONG_MAX,
              timeMax = 0,
              timeTotal = 0;
-        bool ok = true;
-        for ( long a = 0; a < m_avgCount; a++ )
+        bool ok = func->Init();
+        for ( long a = 0; ok && a < m_avgCount; a++ )
         {
             wxStopWatch sw;
             for ( long n = 0; n < m_numRuns && ok; n++ )
@@ -190,9 +265,6 @@ int BenchApp::OnRun()
 
             sw.Pause();
 
-            if ( !ok )
-                break;
-
             const long t = sw.Time();
             if ( t < timeMin )
                 timeMin = t;
@@ -201,6 +273,8 @@ int BenchApp::OnRun()
             timeTotal += t;
         }
 
+        func->Done();
+
         if ( !ok )
         {
             wxPrintf("ERROR\n");
@@ -220,6 +294,8 @@ int BenchApp::OnRun()
             wxPrintf("%.2f avg (min=%ld, max=%ld)\n",
                      (float)timeTotal / times, timeMin, timeMax);
         }
+
+        fflush(stdout);
     }
 
     return rc;