Show
Ignore:
Timestamp:
14/07/10 11:15:19 (23 months ago)
Author:
westram
Message:
  • measure time used for tests
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/UNIT_TESTER/UnitTester.cxx

    r6451 r6677  
    1818#include <cstdlib> 
    1919#include <cstdio> 
     20 
     21#include <sys/time.h> 
    2022 
    2123#define SIMPLE_ARB_ASSERT 
     
    7274} 
    7375 
    74 static UnitTestResult execute_guarded(UnitTest_function fun) { 
     76#define SECOND 1000000 
     77 
     78static UnitTestResult execute_guarded(UnitTest_function fun, long *duration_usec) { 
    7579    SigHandler     old_handler = signal(SIGSEGV, UT_sigsegv_handler); 
     80    UnitTestResult result      = TEST_OK; 
     81    timeval        t1, t2; 
    7682    int            trapped     = setjmp(UT_return_after_segv); 
    77     UnitTestResult result      = TEST_OK; 
    7883 
    7984    if (trapped) {                                  // trapped 
     
    8388    else {                                          // normal execution 
    8489        inside_test = true; 
     90        gettimeofday(&t1, NULL); 
    8591        fun(); 
    8692    } 
     93 
     94    gettimeofday(&t2, NULL); 
     95    *duration_usec = (t2.tv_sec - t1.tv_sec) * SECOND + (t2.tv_usec - t1.tv_usec); 
     96     
    8797    inside_test = false; 
    8898    signal(SIGSEGV, old_handler); 
     
    96106    const UnitTest_simple *tests; 
    97107    size_t                 count; 
     108    double                 duration_ms; 
    98109 
    99110    bool perform(size_t which); 
    100      
     111 
    101112public: 
    102113    SimpleTester(const UnitTest_simple *tests_) 
     
    108119    size_t perform_all(); 
    109120    size_t get_test_count() const { return count; } 
     121 
     122    double overall_duration_ms() const { return duration_ms; } 
    110123}; 
    111124 
     
    126139    ut_assert(which<count); 
    127140 
    128     const UnitTest_simple& test   = tests[which]; 
    129     UnitTest_function      fun    = test.fun; 
    130     UnitTestResult         result = execute_guarded(fun); 
     141    const UnitTest_simple& test = tests[which]; 
     142    UnitTest_function      fun  = test.fun; 
    131143 
    132     trace("* %s = %s\n", test.name, readable_result[result]); 
     144    long           duration_usec; 
     145    UnitTestResult result           = execute_guarded(fun, &duration_usec); 
     146    double         duration_ms_this = duration_usec/1000.0; 
     147 
     148    duration_ms += duration_ms_this; 
     149    trace("* %s = %s (%.1f ms)\n", test.name, readable_result[result], duration_ms_this); 
    133150    if (result != TEST_OK) { 
    134151        fprintf(stderr, "%s: Error: %s failed (details above)\n", test.location, test.name); 
     
    144161    size_t tests  = 0; 
    145162    size_t passed = 0; 
     163    double duration_ms; 
    146164 
    147165    { 
    148166        SimpleTester simple_tester(simple_tests); 
    149167 
    150         tests = simple_tester.get_test_count(); 
     168        tests             = simple_tester.get_test_count(); 
    151169        if (tests) passed = simple_tester.perform_all(); 
     170        duration_ms       = simple_tester.overall_duration_ms(); 
    152171    } 
    153172 
    154173    if (tests>0) { 
    155174        if (passed == tests) { 
    156             trace("all unit tests for '%s' passed OK\n", libname); 
     175            trace("all unit tests for '%s' passed OK (%.0f ms)\n\n", libname, duration_ms); 
    157176        } 
    158177        else { 
    159             trace("unit tests for '%s': %zu OK / %zu FAILED\n", libname, passed, tests-passed); 
     178            trace("unit tests for '%s': %zu OK / %zu FAILED (%.0f ms)\n\n", libname, passed, tests-passed, duration_ms); 
    160179        } 
    161180    } 
    162181    else { 
    163         trace("No tests defined for '%s'\n", libname); 
     182        trace("No tests defined for '%s'\n\n", libname); 
    164183    } 
    165184