// // Copyright (C) 2015 Aaron Ball // // Cnetbench is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Cnetbench is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with cnetbench. If not, see . // #include "test.h" #ifndef cnetbench_test #define cnetbench_test #define test_default_amount 64 #define test_default_streams 1 void new_test(t_test *test) { test->amount = test_default_amount; test->streams = test_default_streams; } // // Performs a benchmark test on the specified destination/port, sending the // specified amount of data. // // @param amount Megabytes to send // @param dest Destination host to send data to // @param port Destination port to send the data on // // @return int Succcess (0) or failure (1) of data send // int test_execute(t_test* test) { int socket_desc; // This is 1 megabyte long size = 1048576; char *message = calloc(size, 1); // AF_INET: ipv4 (AF_INET6 would be ipv6) // SOCK_STREAM: TCP // 0: Protocol 0 (IP) socket_desc = socket(AF_INET, SOCK_STREAM, 0); if(socket_desc == -1) { printf("Error creating socket\n"); return 1; } // Set socket values struct sockaddr_in server; server.sin_addr.s_addr = inet_addr(test->server); server.sin_family = AF_INET; server.sin_port = htons(test->port); if(connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0) { printf("Connection error\n"); return -1; } printf("Connected\n"); // Send some data long i = 0; while(i < test->amount) { if(send(socket_desc, message, size, 0) < 0) { return -1; } i++; } free(message); return 0; } // // Forks sub-processes off stream_count times, to test bandwidth to the // specified destination host/port, sending the specified amount of data. // // Note that start_times and exit_times are two arrays that will receive start // and exit timestamps for each pid for timing purposes. // // @param amount Amount of data in megabytes to send // @param stream_count Number of data streams to test concurrently // @param dest Destination host to send data to // @param port Destination port to send data over // @param start_times Output array of timeval start time timestamps // @param exit_times Output array of timeval exit time timestamps // // @return int Success (0) or failure(1) // //int spawn_tests(int amount, // int stream_count, // char* dest, // int port, // struct timeval *start_times, // struct timeval *exit_times) { int test_start_all(t_test *test_def, struct timeval *start_times, struct timeval *exit_times) { pid_t pids[test_def->streams]; pid_t pid; int i = 0; while(i < test_def->streams) { // Fork pid = fork(); if(pid == 0) { // Execute the test printf("Fork %d starting\n", i); test_execute(test_def); exit(0); } else if(pid > 0) { // parent pids[i] = pid; // Set start time gettimeofday(&start_times[i], NULL); i++; } else { // failure printf("%d: Failure\n", i); return 1; } } // Wait for all children to exit, and clock their exit time time_pids(pids, test_def->streams, exit_times); printf("Fin!\n"); return 0; } #endif