Solved

libcurl performance

Posted on 2011-09-21
3
484 Views
Last Modified: 2012-05-12
Hi

I am using libcurl in C++  to call a webservice api and get back a json/xml response.

PLease see the attached code
I want to make sure i can do the following things

1) if the response takes too long and i am waiting on it, i want to time out
2) show i worry about anyothet failure cases, for example, if the called url does not exist?

any other problem areas i should be aware of?

size_t writer( void *ptr, size_t size, size_t nmemb, void *stream)
{
    buffer.append((char*)ptr, size*nmemb);
    return size*nmemb;
}

std::string mdi_body::DownloadWS(std::string URL, ext_api mode)
{
    CURL *curl;
    CURLcode res;
    std::ostringstream oss;
    curl = curl_easy_init();
    stringstream data;

    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());

        if (mode == CORE_IMAP) {
            char *data="{ \"groups\" : [\"F\"], \"content\" : true }";
            curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
        }

        curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, writer);

        res = curl_easy_perform(curl);
        if(CURLE_OK == res) {
            char *ct;
            res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
            if((CURLE_OK == res) && ct)
                return buffer;
            }
      }

}

Open in new window

0
Comment
Question by:Vlearns
  • 2
3 Comments
 

Author Comment

by:Vlearns
ID: 36577023
added these

 46         /* Performance check: DNS timeouts*/
 47         curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 50);
 48
 49         /* Performance check: Response timeouts */
 50         curl_easy_setopt(curl,CURLOPT_TIMEOUT_MS, 50);

what else should i worry about?
0
 

Author Comment

by:Vlearns
ID: 36577028
i am using

ports/curl-7.16.3_1
0
 
LVL 7

Accepted Solution

by:
tampnic earned 500 total points
ID: 36579789
One thing I notice is after calls to the library you are only checking for success - you might want to write a function similar to below to log the performance of your code - call it whenever result of a call to the library is not CURLE_OK.

You may want to retry on timeouts, up to a maximum number of retries. Sometimes if a network request is timing out its better to retry from scratch again, rather than setting a longer timeout. Don't ask me why! Its just something I tried in the past that got me out of a hole :-) When making 3 network data requests at 5s intervals with short timeout the third one got through, but making one request with a timeout interval of 60s timed out. That wasn't using libcurl but the general principle might still apply (or not ... if you have timeout issues its a possible workaround).

N.B. I knocked the code up in two minutes - untested - you might want to check out the pages at http://curl.haxx.se/libcurl/c/ to make sure I'm setting up the CURLOPT_ERRORBUFFER option correctly.

Cheers,
  Chris
void OnCURLError(CURL *handle, CURLcode errornum)
{
  char sError[CURL_ERROR_SIZE];
  curl_easy_setopt(handle,CURLOPT_ERRORBUFFER,sError);
  cerr << curl_easy_strerror(errornum);
}

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question