cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re : Speed performances of curl library

From: <gonzague.debus_at_centraliens-lille.org>
Date: Fri, 2 Jul 2010 08:11:37 +0200 (CEST)

I tried it on win XP only. Protocol is http.
I am using version 7.20.0.0 of libcurl with ssl support

Considering your answers, the problem must be specific to my OS
installation, i will try this week end on a clean new one.

I am not using the multi interface but i guess it is ok since i am using
multiple threads ?

Anyway, here are the code sources:

#python 2.5

        file=open(path+name,"wb")
        f=urllib2.urlopen(request)
        ctn=1
        while ctn:
                try:
                        buffer=f.read(50000)
                except:
                        buffer=""
                if len(buffer)<50000:
                        ctn=0
                file.write(buffer)
        file.close()

// libcurl

typedef struct DL_ARG{
FILE * F_OUT;
char * TAMPON;
unsigned int SIZE;
} DL_ARG;

//TotalBytes2 is a global unsigned int that allows me to count how much
data has been received at any time
//TotalBytes1 is used for request that do not write data to the disk
unsigned int TotalBytes2;

//write file function
size_t write_data( void * ptr, size_t size, size_t nmemb, void * stream){
    DL_ARG * stt = (DL_ARG *) stream;
    if(stt->SIZE+size*nmemb>TAMPON_SIZE)
    {
        fwrite(stt->TAMPON,1,stt->SIZE,stt->F_OUT);
        fwrite(ptr,size,nmemb,stt->F_OUT);
        stt->SIZE=0;
    }
    else
    {
        memcpy(stt->TAMPON+stt->SIZE,ptr,size*nmemb);
        stt->SIZE+=size*nmemb;
    }
    TotalBytes2+=size*nmemb;
    return nmemb*size;
}

//call

// curl pointer comes from a "ConnectionManager::getCurl()" method
// this class manages all the curls pointers which allows suspending new
requests (and waiting for pending requests to finish) if needed (in order
to shutdown and restart your internet connection without getting errors)
// since there is many threads eventually performing requests , this is
quite useful

char ConnectionManager::download_file(std::string directory, std::string
str_file_name, std::string str_url, std::string post, std::string cookie){
{
.
.
.
    curl_easy_reset(curl);
    curl_easy_setopt(curl, CURLOPT_URL, str_url.data());
    curl_easy_setopt(curl, CURLOPT_USERAGENT, user_agent.data());
    if(Proxy.size())
        curl_easy_setopt(curl, CURLOPT_PROXY ,Proxy.data());
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION ,write_data );
    if(post.size())
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS , post.data() );
    if(cookie.size())
        curl_easy_setopt(curl, CURLOPT_COOKIE , cookie.data());

    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION , 1);

    DL_ARG arg;
    arg.F_OUT = fopen(path.data(),"wb");
    if(arg.F_OUT)
    {
        arg.SIZE=0;
        arg.TAMPON = (char *) malloc(TAMPON_SIZE);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA , &arg );

        res = curl_easy_perform(curl);
        if(arg.SIZE)
        {
            fwrite(arg.TAMPON,1,arg.SIZE,arg.F_OUT);
            arg.SIZE=0;
        }
        free(arg.TAMPON);
        fclose(arg.F_OUT);
    }
.
.
.
}

Most of the time, and for the comparison I made, only one thread is
downloading and no other requests are performed meanwile.

curl pointers are initialized this way in the constructor:

for(unsigned int i=0;i<nb;i++)
{
        tab[i]=curl_easy_init();
}

Are they some OS preferences to run libcurl ?

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2010-07-02