Solved

libcurl performance

Posted on 2011-09-21
3
459 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

937 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

Need Help in Real-Time?

Connect with top rated Experts

1 Experts available now in Live!

Get 1:1 Help Now