Solved

libcurl performance

Posted on 2011-09-21
3
524 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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…

734 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