Changeset 34798

Show
Ignore:
Timestamp:
08/06/09 16:53:32 (4 years ago)
Author:
saturday06
Message:

bencher

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/objective-cplusplus/i3/trunk/src/mil/src/Test11.cpp

    r34796 r34798  
    1414namespace Test11 { 
    1515 
    16 int global = 0; 
     16atomic<int> global = 0; 
    1717atomic<int> spin_status; 
    1818 
     
    3737 
    3838// bench class 
    39 struct BenchReceiver : mil::Module<BenchReceiver> { 
     39const unsigned int POST_MAX = 50000; 
     40 
     41struct Bencher : mil::Module<Bencher> { 
     42    static Bencher benchers[20]; 
     43    int index; 
    4044    int begin; 
     45    int posted; 
     46    void setIndex(int index_) { 
     47        posted = 0; 
     48        index = index_; 
     49    } 
     50    void a() { 
     51        posted++; 
     52        if (posted == POST_MAX) { 
     53            AllPosted e; 
     54            benchers[index].post(e, *this); 
     55        } 
     56    } 
     57    void execute(TinyEvent& e) { 
     58        global.fetch_and_add(1); 
     59        benchers[index].post(e, *this); 
     60        a(); 
     61    } 
     62    void execute(SmallEvent& e) { 
     63        global.fetch_and_add(e.data * 2); 
     64        benchers[index].post(e, *this); 
     65        a(); 
     66    } 
     67    void execute(MiddleEvent& e) { 
     68        global.fetch_and_add(e.data * 2); 
     69        benchers[index].post(e, *this); 
     70        a(); 
     71    } 
     72    void execute(BigEvent& e) { 
     73        global.fetch_and_add(e.data * 2); 
     74        benchers[index].post(e, *this); 
     75        a(); 
     76    } 
     77    void execute(AllPosted& e) { 
     78        printf("AllPosted Received : %lf, global = %d, index = %d\n", 
     79               (double)(clock() - begin) / CLOCKS_PER_SEC, global.load(), index); 
     80        spin_status.fetch_and_add(1); 
     81    } 
    4182    void run() { 
    4283        while (spin_status.load()) {} 
    4384        begin = clock(); 
     85        for (int i = 0; i < 5; i++) { 
     86            TinyEvent t; 
     87            benchers[index].post(t, *this); 
     88            SmallEvent s; 
     89            s.data = 8; 
     90            benchers[index].post(s, *this); 
     91            //MiddleEvent m; 
     92            //m.data = 16; 
     93            //r.post(m, *this); 
     94            //BigEvent b; 
     95            //b.data = 32; 
     96            //r.post(b, *this); 
     97        } 
    4498        this->loop(); 
    45     } 
    46     void execute(TinyEvent& e) { 
    47         global += 2; 
    48     } 
    49     void execute(SmallEvent& e) { 
    50         global += e.data * 2; 
    51     } 
    52     void execute(MiddleEvent& e) { 
    53         global += e.data * 2; 
    54     } 
    55     void execute(BigEvent& e) { 
    56         global += e.data * 2; 
    57     } 
    58     void execute(AllPosted& e) { 
    59         printf("AllPosted Received : %lf, global = %d\n", 
    60                (double)(clock() - begin) / CLOCKS_PER_SEC, global); 
    61         spin_status.store(2); 
    6299    } 
    63100}; 
    64101 
    65 struct BenchSender : mil::Module<BenchSender> { 
    66     int begin; 
    67     BenchReceiver& r; 
    68     BenchSender(BenchReceiver& r) : r(r) {} 
    69     void run() { 
    70         while (spin_status.load()) {} 
    71         begin = clock(); 
    72         for (int i = 0; i < 5000000; i++) { 
    73             TinyEvent t; 
    74             r.post(t, *this); 
    75             SmallEvent s; 
    76             s.data = 8; 
    77             r.post(s, *this); 
    78             MiddleEvent m; 
    79             m.data = 16; 
    80             //r.post(m, *this); 
    81             BigEvent b; 
    82             b.data = 32; 
    83             //r.post(b, *this); 
    84         } 
    85         AllPosted a; 
    86         r.post(a, *this); 
    87  
    88         while (spin_status.load() != 2) { 
    89             while (this->execute_front()) { 
    90             } 
    91         } 
    92         printf("run end : %lf, global = %d\n", 
    93                (double)(clock() - begin) / CLOCKS_PER_SEC, global); 
    94         spin_status.store(3); 
    95     } 
    96 }; 
     102Bencher Bencher::benchers[20]; 
    97103 
    98104} 
     
    101107 
    102108QT_TEST(test_iaodfioasdfadfaadfasdfadfiiiii) { 
    103     BenchReceiver r; 
    104     BenchSender s(r); 
    105109    spin_status.store(1); 
    106     r.start(); 
    107     s.start(); 
    108     sleep(1); 
     110 
     111    for (size_t i = 0; i < _countof(Bencher::benchers); i++) { 
     112        Bencher::benchers[i].setIndex((i + 1) % _countof(Bencher::benchers)); 
     113        Bencher::benchers[i].start(); 
     114    } 
     115 
    109116    spin_status.store(0); 
    110117 
    111     while (spin_status.load() != 3) {} 
    112  
     118    while (spin_status.load() != _countof(Bencher::benchers)) { 
     119        Sleep(10); 
     120    } 
    113121} 
    114122