Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: PicoTCP lpc1768-picotcp-eth mbed-rtos mbed
main.cpp@1:81060ee2ac04, 2013-10-03 (annotated)
- Committer:
- tass
- Date:
- Thu Oct 03 07:05:52 2013 +0000
- Revision:
- 1:81060ee2ac04
- Parent:
- 0:28a7ae57d114
UDP Rx Official Benchmark
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tass | 0:28a7ae57d114 | 1 | #include "mbed.h" |
| tass | 0:28a7ae57d114 | 2 | #include "EthernetInterface.h" |
| tass | 0:28a7ae57d114 | 3 | |
| tass | 1:81060ee2ac04 | 4 | #define MEGA (1024*1024) |
| tass | 0:28a7ae57d114 | 5 | #define BUFFER_SIZE 1024 |
| tass | 0:28a7ae57d114 | 6 | #define NUMBER_OF_SECONDS (20*1000u) // 20 seconds |
| tass | 0:28a7ae57d114 | 7 | |
| tass | 0:28a7ae57d114 | 8 | #define REMOTE_IP "192.168.100.2" |
| tass | 0:28a7ae57d114 | 9 | #define REMOTE_PORT 2327 |
| tass | 0:28a7ae57d114 | 10 | #define NUMBER_OF_FRAMES 1000 |
| tass | 0:28a7ae57d114 | 11 | |
| tass | 0:28a7ae57d114 | 12 | struct UDPStat |
| tass | 0:28a7ae57d114 | 13 | { |
| tass | 0:28a7ae57d114 | 14 | int BytesReceived; |
| tass | 0:28a7ae57d114 | 15 | int FramesReceived; |
| tass | 1:81060ee2ac04 | 16 | int LostPackets; |
| tass | 0:28a7ae57d114 | 17 | }; |
| tass | 0:28a7ae57d114 | 18 | |
| tass | 0:28a7ae57d114 | 19 | struct UDPStat UDP_Statistics; |
| tass | 0:28a7ae57d114 | 20 | |
| tass | 0:28a7ae57d114 | 21 | int main() { |
| tass | 0:28a7ae57d114 | 22 | |
| tass | 0:28a7ae57d114 | 23 | EthernetInterface eth; |
| tass | 0:28a7ae57d114 | 24 | eth.init(); |
| tass | 0:28a7ae57d114 | 25 | eth.connect(); |
| tass | 0:28a7ae57d114 | 26 | |
| tass | 0:28a7ae57d114 | 27 | printf("Started UDP Fast Test...\n"); |
| tass | 0:28a7ae57d114 | 28 | |
| tass | 0:28a7ae57d114 | 29 | UDPSocket server; |
| tass | 0:28a7ae57d114 | 30 | Endpoint endp; |
| tass | 0:28a7ae57d114 | 31 | |
| tass | 0:28a7ae57d114 | 32 | printf("Remote endpoint @ %s:%d\n",REMOTE_IP,REMOTE_PORT); |
| tass | 0:28a7ae57d114 | 33 | endp.set_address(REMOTE_IP,REMOTE_PORT); |
| tass | 0:28a7ae57d114 | 34 | |
| tass | 0:28a7ae57d114 | 35 | printf("Binding result :%d\n", server.bind(REMOTE_PORT)); |
| tass | 0:28a7ae57d114 | 36 | |
| tass | 0:28a7ae57d114 | 37 | server.set_blocking(false,1000); |
| tass | 0:28a7ae57d114 | 38 | char buffer[BUFFER_SIZE]; |
| tass | 0:28a7ae57d114 | 39 | |
| tass | 0:28a7ae57d114 | 40 | while(true) |
| tass | 0:28a7ae57d114 | 41 | { |
| tass | 0:28a7ae57d114 | 42 | unsigned int time = PICO_TIME_MS(); |
| tass | 1:81060ee2ac04 | 43 | int index = -1; |
| tass | 0:28a7ae57d114 | 44 | |
| tass | 0:28a7ae57d114 | 45 | memset(&UDP_Statistics,0x0,sizeof(struct UDPStat)); |
| tass | 0:28a7ae57d114 | 46 | while( (time + NUMBER_OF_SECONDS) >= (unsigned int)PICO_TIME_MS()) |
| tass | 0:28a7ae57d114 | 47 | { |
| tass | 0:28a7ae57d114 | 48 | int size; |
| tass | 0:28a7ae57d114 | 49 | size = server.receiveFrom(endp,buffer,sizeof(buffer)); |
| tass | 1:81060ee2ac04 | 50 | if(size > 0) |
| tass | 0:28a7ae57d114 | 51 | { |
| tass | 1:81060ee2ac04 | 52 | int _index; |
| tass | 0:28a7ae57d114 | 53 | UDP_Statistics.BytesReceived += size; |
| tass | 0:28a7ae57d114 | 54 | UDP_Statistics.FramesReceived++; |
| tass | 1:81060ee2ac04 | 55 | _index = *((int *)buffer); |
| tass | 1:81060ee2ac04 | 56 | |
| tass | 1:81060ee2ac04 | 57 | if(index>0) |
| tass | 1:81060ee2ac04 | 58 | { |
| tass | 1:81060ee2ac04 | 59 | UDP_Statistics.LostPackets+= _index-index-1; |
| tass | 1:81060ee2ac04 | 60 | } |
| tass | 1:81060ee2ac04 | 61 | index = _index; |
| tass | 0:28a7ae57d114 | 62 | } |
| tass | 0:28a7ae57d114 | 63 | } |
| tass | 0:28a7ae57d114 | 64 | time = PICO_TIME_MS() - time; |
| tass | 0:28a7ae57d114 | 65 | time = time/1000; |
| tass | 0:28a7ae57d114 | 66 | |
| tass | 1:81060ee2ac04 | 67 | printf("20 seconds Rx statistics\n"); |
| tass | 0:28a7ae57d114 | 68 | printf("Total bytes received : %d\n",UDP_Statistics.BytesReceived); |
| tass | 1:81060ee2ac04 | 69 | printf("UDP Speed :%.3f Mbit/s\n",(UDP_Statistics.BytesReceived *8.0)/(20.0*MEGA)); |
| tass | 1:81060ee2ac04 | 70 | printf("Loss percentage: %.2f %%\n\n\n",(UDP_Statistics.LostPackets*100.0)/(UDP_Statistics.LostPackets+UDP_Statistics.FramesReceived)); |
| tass | 0:28a7ae57d114 | 71 | } |
| tass | 0:28a7ae57d114 | 72 | |
| tass | 0:28a7ae57d114 | 73 | server.close(); |
| tass | 0:28a7ae57d114 | 74 | } |