Skip to content

Instantly share code, notes, and snippets.

@kreshikhin
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kreshikhin/d398608cb827aa703b9c to your computer and use it in GitHub Desktop.
Save kreshikhin/d398608cb827aa703b9c to your computer and use it in GitHub Desktop.
/* http-client.c
*
* Copyright (c) 2000 Sean Walton and Macmillan Publishers. Use may be in
* whole or in part in accordance to the General Public License (GPL).
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#include <errno.h>
#include <time.h>
#include <pthread.h>
#define MAXBUF 1024
void PANIC(char *msg);
#define PANIC(msg) { perror(msg); abort() ;}
struct sockaddr_in dest;
char request[1024];
size_t request_count = 0;
const size_t request_limit = 100000;
pthread_mutex_t count_mutex;
static size_t make_request(void* p) {
int sockfd, bytes_read;
char buffer[MAXBUF];
size_t response_length = 0;
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
PANIC("Socket");
/*---Connect to server---*/
if ( connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0 )
PANIC("Connect");
send(sockfd, request, strlen(request), 0);
/*---While there's data, read and print it---*/
do
{
bzero(buffer, sizeof(buffer));
bytes_read = recv(sockfd, buffer, sizeof(buffer), 0);
response_length += bytes_read;
//if ( bytes_read > 0 )
// printf("%s %i", buffer, bytes_read);
}
while ( bytes_read > 0 );
/*---Clean up---*/
close(sockfd);
return response_length;
}
static void* request_loop(void* p) {
printf("%s", "a request loop has been created\r\n");
do {
make_request(NULL);
pthread_mutex_lock(&count_mutex);
request_count ++;
if(!(request_count % 1000)) {
printf(" %lu \r\n ", 100 * request_count / request_limit);
}
pthread_mutex_unlock(&count_mutex);
} while (request_count < request_limit);
}
int main(int Count, char *Strings[])
{
pthread_mutex_init(&count_mutex, NULL);
const size_t thread_count = 32;
pthread_t threads[thread_count];
/*---Make sure we have the right number of parameters---*/
if ( Count < 4 )
PANIC("usage: testport <IP-addr> <port> <send-msg>\n");
/*---Initialize server address/port struct---*/
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(atoi(Strings[2])); /* HTTP Server port */
if ( inet_addr(Strings[1], &dest.sin_addr.s_addr) == 0 )
PANIC(Strings[1]);
bzero(&request, sizeof(request));
sprintf(request,
"GET %s HTTP/1.1\r\n"
"Host: %s:%s\r\n"
"User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:32.0) Gecko/20100101 Firefox/32.0\r\n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
"Accept-Language: en-US,en;q=0.5\r\n"
"Cache-Control: max-age=0\r\n"
"Connection: close\r\n\r\n"
, Strings[3], Strings[1], Strings[2]);
printf("%s", request);
size_t response_length = make_request(NULL);
//pthread_mutex_destroy(&count_mutex);
//return 0;
time_t timer0;
time(&timer0);
int i;
for(i = 0; i < thread_count; ++ i) {
if(pthread_create(&threads[i], NULL, request_loop, NULL) != 0)
{
return EXIT_FAILURE;
}
}
for(i = 0; i < thread_count; ++ i) {
if(pthread_join(threads[i], NULL) != 0)
{
return EXIT_FAILURE;
}
}
time_t timer1;
time(&timer1);
float exec_time = timer1 - timer0;
float us_per_request = 1e6 * exec_time / request_limit;
float request_per_second = request_limit / exec_time;
float request1000 = 1000 * exec_time / request_limit;
printf ("Executed in %li threads\r\n", thread_count);
printf ("Size of response in bytes %li \r\n", response_length);
printf ("Total request count %li \r\n", request_limit);
printf ("Total request time %f in seconds \r\n", exec_time);
printf ("Requests per second %f \r\n", request_per_second);
printf ("Time per request %f (us) \r\n", us_per_request);
printf ("Time of 1K request %f (s)\r\n", request1000);
pthread_mutex_destroy(&count_mutex);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment