Created
November 5, 2014 18:05
-
-
Save kanazux/7383fbdf98ef394607be to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <stdlib.h> | |
#include <curl/curl.h> | |
#include <unistd.h> | |
#define TIMEOUT 20 | |
// Function prototypes | |
void upload_test(const char *, const char *); | |
void download_test(const char *); | |
void usage(void); | |
void upload_test(const char *url, const char *filename) { | |
double speed; | |
CURL *curl; | |
FILE *fd; | |
fd = fopen(filename,"r"); | |
if (!fd) { | |
fprintf(stderr, "Error trying to read %s\n", filename); | |
exit(1); | |
} | |
curl = curl_easy_init(); | |
if (curl) { | |
curl_easy_setopt(curl, CURLOPT_TIMEOUT, TIMEOUT); | |
curl_easy_setopt(curl, CURLOPT_URL, url); | |
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); | |
curl_easy_setopt(curl, CURLOPT_READDATA, fd); | |
curl_easy_setopt(curl, CURLOPT_NOBODY, 1); | |
curl_easy_perform(curl); | |
curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed); | |
printf("Upload Speed: %.2f Kb/s\n", speed / 1024); | |
curl_easy_cleanup(curl); | |
} | |
fclose(fd); | |
return; | |
} | |
void download_test(const char *url) { | |
double speed; | |
CURL *curl; | |
FILE *fd; | |
fd = fopen("/dev/null","a"); | |
if (!fd) { | |
fprintf(stderr, "Error trying to write to /dev/null\n"); | |
exit(1); | |
} | |
curl = curl_easy_init(); | |
if (curl) { | |
curl_easy_setopt(curl, CURLOPT_TIMEOUT, TIMEOUT); | |
curl_easy_setopt(curl, CURLOPT_URL, url); | |
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fd); | |
curl_easy_perform(curl); | |
curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &speed); | |
printf("Download Speed: %.2f Kb/s\n", speed / 1024); | |
printf("Nominal Speed: %.2f MB/s\n", speed / 1024 / 1024 * 10); | |
curl_easy_cleanup(curl); | |
} | |
fclose(fd); | |
return; | |
} | |
void usage() { | |
printf("Usage: consumer -d download_url -u upload_url -f upload_filename\n"); | |
exit(1); | |
} | |
int main(int argc, char *argv[]) { | |
char *filename = NULL; | |
char *durl = NULL; | |
char *uurl = NULL; | |
int c; | |
while ((c = getopt (argc, argv, "d:u:f:")) != -1) | |
switch (c) | |
{ | |
case 'd': | |
durl = optarg; | |
break; | |
case 'u': | |
uurl = optarg; | |
break; | |
case 'f': | |
filename = optarg; | |
break; | |
default: | |
usage(); | |
} | |
if ((durl == NULL) && (uurl == NULL)) | |
usage(); | |
if ((uurl != NULL) && (filename == NULL)) | |
usage(); | |
if (durl != NULL) | |
download_test(durl); | |
if (uurl != NULL) | |
upload_test(uurl, filename); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment