Simple text for Tx bandwidth of ethernet
main.cpp@0:f1fc262e9984, 2011-02-24 (annotated)
- Committer:
- simon
- Date:
- Thu Feb 24 18:37:28 2011 +0000
- Revision:
- 0:f1fc262e9984
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 0:f1fc262e9984 | 1 | #include "mbed.h" |
simon | 0:f1fc262e9984 | 2 | |
simon | 0:f1fc262e9984 | 3 | Ethernet eth; |
simon | 0:f1fc262e9984 | 4 | |
simon | 0:f1fc262e9984 | 5 | #define NPACKETS 100000 |
simon | 0:f1fc262e9984 | 6 | #define NBYTES 1000 |
simon | 0:f1fc262e9984 | 7 | |
simon | 0:f1fc262e9984 | 8 | __packed struct EthPkt { |
simon | 0:f1fc262e9984 | 9 | char dest[6]; |
simon | 0:f1fc262e9984 | 10 | char src[6]; |
simon | 0:f1fc262e9984 | 11 | unsigned short type; |
simon | 0:f1fc262e9984 | 12 | }; |
simon | 0:f1fc262e9984 | 13 | |
simon | 0:f1fc262e9984 | 14 | unsigned short htons(unsigned short n) { // Host short to network short byte swapping |
simon | 0:f1fc262e9984 | 15 | return ((n & 0xff) << 8) | ((n & 0xff00) >> 8); |
simon | 0:f1fc262e9984 | 16 | } |
simon | 0:f1fc262e9984 | 17 | |
simon | 0:f1fc262e9984 | 18 | char data[NBYTES]; |
simon | 0:f1fc262e9984 | 19 | |
simon | 0:f1fc262e9984 | 20 | |
simon | 0:f1fc262e9984 | 21 | |
simon | 0:f1fc262e9984 | 22 | |
simon | 0:f1fc262e9984 | 23 | |
simon | 0:f1fc262e9984 | 24 | int main() { |
simon | 0:f1fc262e9984 | 25 | printf("Hello Ethernet Tx Benchmark!\n"); |
simon | 0:f1fc262e9984 | 26 | |
simon | 0:f1fc262e9984 | 27 | // fill the data with ascending pattern, inc packet header |
simon | 0:f1fc262e9984 | 28 | for(int i=0; i<NBYTES; i++) { |
simon | 0:f1fc262e9984 | 29 | data[i] = i; |
simon | 0:f1fc262e9984 | 30 | } |
simon | 0:f1fc262e9984 | 31 | EthPkt *pkt = (EthPkt*)&data[0]; |
simon | 0:f1fc262e9984 | 32 | eth.address(pkt->src); // note, this call goes to the mbed interface, so takes a while |
simon | 0:f1fc262e9984 | 33 | for(int i = 0; i<6; i++) { |
simon | 0:f1fc262e9984 | 34 | pkt->dest[i] = 0xFF; |
simon | 0:f1fc262e9984 | 35 | } |
simon | 0:f1fc262e9984 | 36 | |
simon | 0:f1fc262e9984 | 37 | while(!eth.link()); |
simon | 0:f1fc262e9984 | 38 | |
simon | 0:f1fc262e9984 | 39 | printf("Sending %d packets of %d bytes...\n", NPACKETS, NBYTES); |
simon | 0:f1fc262e9984 | 40 | |
simon | 0:f1fc262e9984 | 41 | Timer t; |
simon | 0:f1fc262e9984 | 42 | t.start(); |
simon | 0:f1fc262e9984 | 43 | for(int i = 0; i < NPACKETS; i++) { |
simon | 0:f1fc262e9984 | 44 | eth.write(data, NBYTES); |
simon | 0:f1fc262e9984 | 45 | eth.send(); |
simon | 0:f1fc262e9984 | 46 | } |
simon | 0:f1fc262e9984 | 47 | t.stop(); |
simon | 0:f1fc262e9984 | 48 | |
simon | 0:f1fc262e9984 | 49 | printf("Time = %.2f seconds\n", t.read()); |
simon | 0:f1fc262e9984 | 50 | |
simon | 0:f1fc262e9984 | 51 | float bw = NPACKETS * NBYTES * 8 / t.read(); |
simon | 0:f1fc262e9984 | 52 | printf("bandwidth = %.2f kbps\n", bw / 1024.0); |
simon | 0:f1fc262e9984 | 53 | |
simon | 0:f1fc262e9984 | 54 | } |