Solved

libcurl performance

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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.

706 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

19 Experts available now in Live!

Get 1:1 Help Now