[5adf4f4] | 1 | #include <string> |
---|
| 2 | using namespace std; |
---|
| 3 | #include <uActor.h> |
---|
| 4 | #include <chrono> |
---|
| 5 | using namespace chrono; |
---|
| 6 | #include <iostream> |
---|
| 7 | |
---|
| 8 | struct IntMsg : public uActor::SenderMsg { int val; }; |
---|
| 9 | struct CharMsg : public uActor::SenderMsg { char val; }; |
---|
| 10 | |
---|
| 11 | size_t Messages = 100'000, Processors = 4, QScale = 256, Times = 10; |
---|
| 12 | time_point<steady_clock> starttime; |
---|
| 13 | |
---|
| 14 | _Actor Server { |
---|
| 15 | Allocation receive( uActor::Message & msg ) { |
---|
| 16 | Case( IntMsg, msg ) { msg_d->val = 7; *msg_d->sender() | msg; } |
---|
| 17 | else Case( CharMsg, msg ) { msg_d->val = 'x'; *msg_d->sender() | msg; } |
---|
| 18 | else Case( StopMsg, msg ) { return Finished; } |
---|
| 19 | return Nodelete; // reuse actor |
---|
| 20 | } |
---|
| 21 | }; |
---|
| 22 | |
---|
| 23 | Server * servers; |
---|
| 24 | |
---|
| 25 | _Actor Client { |
---|
| 26 | IntMsg * intmsg; |
---|
| 27 | CharMsg * charmsg; |
---|
| 28 | size_t results = 0, times = 0; |
---|
| 29 | |
---|
| 30 | Allocation reset() { |
---|
| 31 | times += 1; |
---|
| 32 | if ( times == Times ) { |
---|
| 33 | for ( unsigned int i = 0; i < Messages; i += 1 ) { |
---|
| 34 | servers[i] | uActor::stopMsg; |
---|
| 35 | } // for |
---|
| 36 | return Finished; |
---|
| 37 | } |
---|
| 38 | results = 0; |
---|
| 39 | *this | uActor::startMsg; // restart experiment |
---|
| 40 | return Nodelete; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | Allocation receive( uActor::Message & msg ) { // receive callback messages |
---|
| 44 | Case( IntMsg, msg ) results += 1; |
---|
| 45 | else Case( CharMsg, msg ) results += 1; |
---|
| 46 | else Case( StartMsg, msg ) { |
---|
| 47 | for ( size_t i = 0; i < Messages; i += 1 ) { // send out work |
---|
| 48 | servers[i] | intmsg[i]; // tell send |
---|
| 49 | servers[i] | charmsg[i]; |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | if ( results == 2 * Messages ) return reset(); // all messages handled ? |
---|
| 53 | return Nodelete; // reuse actor |
---|
| 54 | } |
---|
| 55 | public: |
---|
| 56 | Client() { |
---|
| 57 | intmsg = new IntMsg[Messages]; |
---|
| 58 | charmsg = new CharMsg[Messages]; |
---|
| 59 | } |
---|
| 60 | ~Client() { |
---|
| 61 | delete [] charmsg; |
---|
| 62 | delete [] intmsg; |
---|
| 63 | } |
---|
| 64 | }; |
---|
| 65 | |
---|
| 66 | int main( int argc, char * argv[] ) { |
---|
| 67 | switch ( argc ) { |
---|
| 68 | case 5: |
---|
| 69 | if ( strcmp( argv[4], "d" ) != 0 ) { // default ? |
---|
| 70 | QScale = stoi( argv[4] ); |
---|
| 71 | if ( QScale < 1 ) goto Usage; |
---|
| 72 | } // if |
---|
| 73 | case 4: |
---|
| 74 | if ( strcmp( argv[3], "d" ) != 0 ) { // default ? |
---|
| 75 | Times = stoi( argv[3] ); |
---|
| 76 | if ( Times < 1 ) goto Usage; |
---|
| 77 | } // if |
---|
| 78 | case 3: |
---|
| 79 | if ( strcmp( argv[2], "d" ) != 0 ) { // default ? |
---|
| 80 | Processors = stoi( argv[2] ); |
---|
| 81 | if ( Processors < 1 ) goto Usage; |
---|
| 82 | } // if |
---|
| 83 | case 2: |
---|
| 84 | if ( strcmp( argv[1], "d" ) != 0 ) { // default ? |
---|
| 85 | Messages = stoi( argv[1] ); |
---|
| 86 | if ( Messages < 1 ) goto Usage; |
---|
| 87 | } // if |
---|
| 88 | case 1: // use defaults |
---|
| 89 | break; |
---|
| 90 | default: |
---|
| 91 | Usage: |
---|
| 92 | cerr << "Usage: " << argv[0] |
---|
| 93 | << ") ] [ messages (> 0) | 'd' (default " << Messages |
---|
| 94 | << ") ] [ processors (> 0) | 'd' (default " << Processors |
---|
| 95 | << ") ] [ Times (> 0) | 'd' (default " << Times |
---|
| 96 | << ") ] [ qscale (> 0) | 'd' (default " << QScale |
---|
| 97 | << ") ]" << endl; |
---|
| 98 | exit( EXIT_FAILURE ); |
---|
| 99 | } // switch |
---|
| 100 | |
---|
| 101 | uExecutor executor( Processors, Processors, Processors == 1 ? 1 : Processors * QScale, true, -1 ); |
---|
| 102 | time_point<steady_clock> starttime = steady_clock::now(); |
---|
| 103 | uActor::start( &executor ); // start actor system |
---|
| 104 | servers = new Server[Messages]; |
---|
| 105 | Client client; |
---|
| 106 | client | uActor::startMsg; // start actors |
---|
| 107 | uActor::stop(); // wait for all actors to terminate |
---|
| 108 | cout << (steady_clock::now() - starttime).count() / 1'000'000'000.0 << endl; |
---|
| 109 | delete [] servers; |
---|
| 110 | } |
---|