Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
curl easy接口串行取数据
#include <sys/time.h>
#include <string>
#include "curl/curl.h"
const char* url = "http://www.baidu.com";
const int count = 1000;
size_t write_data(void* buffer, size_t size, size_t count, void* stream) {
(void)buffer;
(void)stream;
return size * count;
}
int64_t time_delta(const struct timeval& start_time, const struct timeval& end_time) {
return (end_time.tv_sec - start_time.tv_sec) * 1e6 + (end_time.tv_usec - start_time.tv_usec);
}
void curl_easy() {
for (int i = 0; i < count; ++i) {
CURL* curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_perform(curl);
}
}
int main() {
struct timeval t1;
struct timeval t2;
gettimeofday(&t1, NULL);
curl_easy();
gettimeofday(&t2, NULL);
printf("%lu\n", time_delta(t1, t2));
return 0;
}
/* vim: set ts=4 sw=4 sts=4 tw=100 */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment