Skip to content

Instantly share code, notes, and snippets.

@aleks-f
Created February 23, 2017 17:56
Show Gist options
  • Save aleks-f/e37688d4cc4a7829d421978074cbe0e3 to your computer and use it in GitHub Desktop.
Save aleks-f/e37688d4cc4a7829d421978074cbe0e3 to your computer and use it in GitHub Desktop.
// $ g++ -std=c++11 -o curl_connect curl_connect.cpp -lcurl
// $ ./curl_connect
#include <stdio.h>
#include <curl/curl.h>
#include <chrono>
#include <iostream>
using namespace std;
using namespace std::chrono;
class stopwatch
{
public:
void start() { record(m_start); }
void stop() { record(m_stop); }
void reset()
{
m_start = high_resolution_clock::time_point::min();
m_stop = m_start;
}
template<typename T>
typename T::rep elapsed() const
{
return duration_cast<T>(m_stop - m_start).count();
}
private:
void record(high_resolution_clock::time_point& tp)
{
tp = high_resolution_clock::now();
}
high_resolution_clock::time_point m_start;
high_resolution_clock::time_point m_stop;
};
int main(void)
{
CURL *curl;
CURLcode res;
stopwatch sw;
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1");
while(true)
{
sw.start();
res = curl_easy_perform(curl);
sw.stop();
if(res != CURLE_OK) cerr << curl_easy_strerror(res) << endl;
cout << "Elapsed: " << sw.elapsed<microseconds>() << " us" << endl;
}
curl_easy_cleanup(curl);
}
else
{
cerr << "Can't create curl." << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment