Created
June 9, 2014 10:53
-
-
Save fgodino/603ba228a97a3325cf31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#include <sys/types.h> | |
#include <errno.h> | |
#include <stdio.h> | |
#include <sys/wait.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <curl/curl.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <pthread.h> /* POSIX Threads */ | |
#include <time.h> | |
#include <sys/resource.h> | |
int num_reqs = 1000; /* A global variable*/ | |
void * request_function (); | |
void sleepMs(int milisec); | |
int getMemory(); | |
void * getData ( void *ptr ); | |
int requests = 0; | |
int responses = 0; | |
pthread_mutex_t mutexres; | |
void sleepMs(int milisec){ | |
struct timespec req = {0}; | |
req.tv_sec = 0; | |
req.tv_nsec = milisec * 1000000L; | |
nanosleep(&req, (struct timespec *)NULL); | |
} | |
int main(void) | |
{ | |
pthread_t * threads = (pthread_t*) malloc(sizeof(pthread_t) * num_reqs); | |
pthread_t dataThread; | |
pthread_attr_t attr; | |
int status; | |
int i; | |
pthread_mutex_init(&mutexres, NULL); | |
pthread_attr_init(&attr); | |
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); | |
pthread_create (&dataThread, NULL, (void *) getData, NULL); | |
for(i = 0; i<num_reqs;i++){ | |
sleepMs(500); | |
requests++; | |
//pthread_create (&threads[i], &attr, (void *) request_function, NULL); | |
request_function(); | |
} | |
pthread_attr_destroy(&attr); | |
/*for(i = 0; i<num_reqs;i++){ | |
pthread_join(threads[i], NULL); | |
}*/ | |
free(threads); | |
pthread_mutex_destroy(&mutexres); | |
pthread_exit(NULL); | |
} | |
void * request_function (){ | |
CURL *curl; | |
CURLcode res; | |
struct stat file_info; | |
double speed_upload, total_time; | |
FILE *fd; | |
fd = fopen("img1.jpg", "rb"); /* open file to upload */ | |
/* to get the file size */ | |
fstat(fileno(fd), &file_info); | |
curl = curl_easy_init(); | |
if(curl) { | |
/* upload to this place */ | |
curl_easy_setopt(curl, CURLOPT_URL, | |
"http://ec2-54-199-108-216.ap-northeast-1.compute.amazonaws.com:80/"); | |
/* tell it to "upload" to the URL */ | |
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); | |
/* set where to read from (on Windows you need to use READFUNCTION too) */ | |
curl_easy_setopt(curl, CURLOPT_READDATA, fd); | |
/* and give the size of the upload (optional) */ | |
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, | |
(curl_off_t)file_info.st_size); | |
/* enable verbose for easier tracing */ | |
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); | |
res = curl_easy_perform(curl); | |
printf("fdsfdsf\n"); | |
/* Check for errors */ | |
if(res != CURLE_OK) { | |
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); | |
} | |
else { | |
/* now extract transfer info */ | |
curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload); | |
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time); | |
//fprintf(stderr, "Speed: %.3f bytes/sec during %.3f seconds\n",speed_upload, total_time); | |
fclose(fd); | |
} | |
/* always cleanup */ | |
curl_easy_cleanup(curl); | |
pthread_mutex_lock (&mutexres); | |
responses++; | |
pthread_mutex_unlock (&mutexres); | |
} | |
pthread_exit(0); /* exit */ | |
} | |
int getMemory() { | |
long double res; | |
struct rusage* memory = malloc(sizeof(struct rusage)); | |
getrusage(RUSAGE_SELF, memory); | |
res = memory->ru_maxrss; | |
free(memory); | |
return res; | |
} | |
void * getData (void * ptr){ | |
FILE * pFile = fopen("runData.csv", "a"); | |
int mem; | |
while(1){ | |
sleep(2); | |
mem = getMemory(); | |
fprintf(pFile, "%d, %d, %d\n", requests, responses, mem); | |
fprintf(stdout, "%d, %d, %d\n", requests, responses, mem); | |
} | |
fclose(pFile); | |
pthread_exit(0); /* exit */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment