int main(int argc, const char *argv[]) { char *data[] = { "apple", "red", /* array of key-value pairs */ "banana", "yellow" }; int data_size = sizeof(data)/sizeof(char*); /* recommended way to get size of array */ req_t req; /* declare struct used to store data */ CURL *curl = requests_init(&req); /* setup */ /* returns apple%3Dred%26banana%3Dyellow */ char *body = requests_url_encode(curl, data, data_size); requests_post(curl, &req, "http://www.posttestserver.com/post.php", body); /* submit POST request */ printf("Request URL: %s\n", req.url); printf("Response Code: %lu\n", req.code); printf("Response Size: %zu\n", req.size); printf("Response Body:\n%s", req.text); curl_free(body); /* necessary, due to internal curl allocation */ requests_close(&req); /* clean up */ return 0; }
int main(int argc, char **argv) { if (argc <= 1) { usage(argv[0]); exit(-1); } char ch; while ((ch = getopt(argc, argv, "t:b:d:s:l:w:o:rh")) != -1) { switch (ch) { // case 't': num_threads = atoi(optarg); break; case 't': num_threads = 1; break; //case 'b': num_mget = atoi(optarg); break; case 'd': duration = atof(optarg); break; case 's': serverip = optarg; break; case 'l': inputfile = optarg; break; case 'o': outputfolder = optarg; break; case 'w': workingset = optarg; break; case 'r': isreverse = true; break; case 'h': usage(argv[0]); exit(0); break; default: usage(argv[0]); exit(-1); } } if (serverip == NULL || inputfile == NULL || workingset == NULL) { usage(argv[0]); exit(-1); } request *requests = requests_init(inputfile); pthread_t threads[num_threads]; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);; pthread_mutex_init(&printmutex, NULL); size_t t; thread_param tp[num_threads]; for (t = 0; t < num_threads; t++) { tp[t].requests = requests + t * (num_requests / num_threads); tp[t].tid = t; tp[t].num_ops = num_requests / num_threads; tp[t].num_puts = tp[t].num_gets = tp[t].num_miss = tp[t].num_hits = 0; tp[t].time = tp[t].tput = 0.0; int rc = pthread_create(&threads[t], &attr, requests_exec, (void *) &tp[t]); if (rc) { perror("failed: pthread_create\n"); exit(-1); } } result_t result; result.total_time = 0.0;; result.total_tput = 0.0; result.total_hits = 0; result.total_miss = 0; result.total_gets = 0; result.total_puts = 0; result.num_threads = num_threads; for (t = 0; t < num_threads; t++) { void *status; int rc = pthread_join(threads[t], &status); if (rc) { perror("error, pthread_join\n"); exit(-1); } result.total_time = (result.total_time > tp[t].time) ? result.total_time : tp[t].time; result.total_tput += tp[t].tput; result.total_hits += tp[t].num_hits; result.total_miss += tp[t].num_miss; result.total_gets += tp[t].num_gets; result.total_puts += tp[t].num_puts; } printf("total_time = %.2f\n", result.total_time); printf("total_tput = %.2f\n", result.total_tput); printf("total_hitratio = %.4f\n", (float) result.total_hits / result.total_gets); pthread_attr_destroy(&attr); printf("bye\n"); return 0; }