#include #include #include #include #include #include #include #include #include #include #include #ifndef AF_SMC #define AF_SMC 43 #endif #define NET_PROTOCAL AF_INET #define SERV_IP "11.213.5.33" #define SERV_PORT 10012 #define BUF_SIZE (5 * 128 * 1024) int stream_send(int fd, char *buf, int msgsize) { int n = msgsize; while (n) { int i = write(fd, buf, n); if (i < 0) return i; buf += i; n -= i; if (i == 0) break; } return msgsize-n; } int net_clnt(char *ip, int port, int msgsize, int msgcnt) { struct timespec start, end; double elapsed_us = 0.0; double elapsed_us_sum = 0.0; int sent; long num_bytes = 0; double gb_s; if (!ip) ip = SERV_IP; if (!port) port = SERV_PORT; int sock = socket(NET_PROTOCAL, SOCK_STREAM, 0); struct sockaddr_in s_addr; memset(&s_addr, 0, sizeof(s_addr)); s_addr.sin_family = NET_PROTOCAL; s_addr.sin_addr.s_addr = inet_addr(ip); s_addr.sin_port = htons(port); if (connect(sock, (struct sockaddr*)&s_addr, sizeof(s_addr))){ printf("connect fail\n"); return 0; } char *buf = (char *)malloc(sizeof(char) * BUF_SIZE); while (--msgcnt) { if (msgsize > BUF_SIZE) break; printf("Send msgsize: %d\n", msgsize); clock_gettime(CLOCK_MONOTONIC, &start); sent = stream_send(sock, buf, msgsize); clock_gettime(CLOCK_MONOTONIC, &end); if (send <= 0) { printf("Error send %d\n", sent); break; } elapsed_us = (end.tv_sec - start.tv_sec)*1000000.0; elapsed_us += (end.tv_nsec - start.tv_nsec) / 1000.0; elapsed_us_sum += elapsed_us; num_bytes += sent; } close(sock); gb_s = (num_bytes/1000) / elapsed_us_sum; printf("Sent %ld bytes in %f us [%f GB/s]\n", num_bytes, elapsed_us_sum, gb_s); return 0; } int main(int argc, char **argv){ int msgsize = BUF_SIZE, msgcnt = 10; char *ip = NULL; bool wrong_param = false; int port = 0; int c; while(!wrong_param && (-1 != (c = getopt(argc, argv, "i:p:m:c:")))) { switch (c) { case 'i': ip = optarg; break; case 'p': port = atoi(optarg); break; case 'm': msgsize = atoi(optarg); break; case 'c': msgcnt = atoi(optarg); break; case '?': printf("usage: ./client -i -p -m -c \n"); wrong_param = true; break; } } if (!wrong_param) net_clnt(ip, port, msgsize, msgcnt); return 0; }