discard.cc revision 726c52da
1726c52daSShuo Chen#include "thread/Atomic.h" 2726c52daSShuo Chen#include "datetime/Timestamp.h" 3d42543f4SShuo Chen#include "Acceptor.h" 4d42543f4SShuo Chen#include "InetAddress.h" 5d42543f4SShuo Chen#include "TcpStream.h" 6d42543f4SShuo Chen 7d42543f4SShuo Chen#include <string.h> 8d42543f4SShuo Chen 9d42543f4SShuo Chen#include <thread> 10d42543f4SShuo Chen 11d42543f4SShuo Chenmuduo::AtomicInt64 g_bytes; 12d42543f4SShuo Chen 13d42543f4SShuo Chenvoid measure() 14d42543f4SShuo Chen{ 15d42543f4SShuo Chen muduo::Timestamp start = muduo::Timestamp::now(); 16d42543f4SShuo Chen while (true) 17d42543f4SShuo Chen { 18d42543f4SShuo Chen struct timespec ts = { 1, 0 }; 19d42543f4SShuo Chen ::nanosleep(&ts, NULL); 20d42543f4SShuo Chen // unfortunately, those two assignments are not atomic 21d42543f4SShuo Chen int64_t bytes = g_bytes.getAndSet(0); 22d42543f4SShuo Chen muduo::Timestamp end = muduo::Timestamp::now(); 23d42543f4SShuo Chen double elapsed = timeDifference(end, start); 24d42543f4SShuo Chen start = end; 25d42543f4SShuo Chen if (bytes) 26d42543f4SShuo Chen { 27d42543f4SShuo Chen printf("%.3f MiB/s\n", bytes / (1024.0 * 1024) / elapsed); 28d42543f4SShuo Chen } 29d42543f4SShuo Chen } 30d42543f4SShuo Chen} 31d42543f4SShuo Chen 32d42543f4SShuo Chenvoid discard(TcpStreamPtr stream) 33d42543f4SShuo Chen{ 34d42543f4SShuo Chen char buf[65536]; 35d42543f4SShuo Chen while (true) 36d42543f4SShuo Chen { 37d42543f4SShuo Chen int nr = stream->receiveSome(buf, sizeof buf); 38d42543f4SShuo Chen if (nr <= 0) 39d42543f4SShuo Chen break; 40d42543f4SShuo Chen g_bytes.add(nr); 41d42543f4SShuo Chen } 42d42543f4SShuo Chen} 43d42543f4SShuo Chen 44d42543f4SShuo Chen// a thread-per-connection current discard server and client 45d42543f4SShuo Chenint main(int argc, char* argv[]) 46d42543f4SShuo Chen{ 47d42543f4SShuo Chen if (argc < 3) 48d42543f4SShuo Chen { 49d42543f4SShuo Chen printf("Usage:\n %s hostname port\n %s -l port\n", argv[0], argv[0]); 50d42543f4SShuo Chen return 0; 51d42543f4SShuo Chen } 52d42543f4SShuo Chen 53d42543f4SShuo Chen std::thread(measure).detach(); 54d42543f4SShuo Chen 55d42543f4SShuo Chen int port = atoi(argv[2]); 56d42543f4SShuo Chen if (strcmp(argv[1], "-l") == 0) 57d42543f4SShuo Chen { 58d42543f4SShuo Chen Acceptor acceptor((InetAddress(port))); 59d42543f4SShuo Chen printf("Accepting... Ctrl-C to exit\n"); 60d42543f4SShuo Chen int count = 0; 61d42543f4SShuo Chen while (true) 62d42543f4SShuo Chen { 63d42543f4SShuo Chen TcpStreamPtr tcpStream = acceptor.accept(); 64d42543f4SShuo Chen printf("accepted no. %d client\n", ++count); 65d42543f4SShuo Chen 66d42543f4SShuo Chen std::thread thr(discard, std::move(tcpStream)); 67d42543f4SShuo Chen thr.detach(); 68d42543f4SShuo Chen } 69d42543f4SShuo Chen } 70d42543f4SShuo Chen else 71d42543f4SShuo Chen { 72d42543f4SShuo Chen InetAddress addr(port); 73d42543f4SShuo Chen const char* hostname = argv[1]; 74d42543f4SShuo Chen if (InetAddress::resolve(hostname, &addr)) 75d42543f4SShuo Chen { 76d42543f4SShuo Chen TcpStreamPtr stream(TcpStream::connect(addr)); 77d42543f4SShuo Chen if (stream) 78d42543f4SShuo Chen { 79d42543f4SShuo Chen discard(std::move(stream)); 80d42543f4SShuo Chen } 81d42543f4SShuo Chen else 82d42543f4SShuo Chen { 83d42543f4SShuo Chen printf("Unable to connect %s\n", addr.toIpPort().c_str()); 84d42543f4SShuo Chen perror(""); 85d42543f4SShuo Chen } 86d42543f4SShuo Chen } 87d42543f4SShuo Chen else 88d42543f4SShuo Chen { 89d42543f4SShuo Chen printf("Unable to resolve %s\n", hostname); 90d42543f4SShuo Chen } 91d42543f4SShuo Chen } 92d42543f4SShuo Chen} 93