Skip to content

Instantly share code, notes, and snippets.

@bagder
Created November 17, 2023 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bagder/f61dd7a9a0f568851b94fb8f75cdd26c to your computer and use it in GitHub Desktop.
Save bagder/f61dd7a9a0f568851b94fb8f75cdd26c to your computer and use it in GitHub Desktop.
measure libcurl URL parsing speed
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <curl/curl.h>
#define FILENAME "URLs"
void *mem;
char **urls;
size_t url_num;
size_t url_alloc;
static void load_urls()
{
struct stat st;
size_t memsize;
size_t left;
FILE *fp;
char *ptr;
stat(FILENAME, &st);
memsize = st.st_size;
mem = malloc(memsize + 1);
fp = fopen(FILENAME, "rb");
fread(mem, memsize, 1, fp);
for(ptr = mem, left = memsize; left; ) {
char *nl;
if(url_alloc == url_num) {
if(!url_alloc) {
url_alloc = 10000;
}
url_alloc *= 2;
urls = realloc(urls, url_alloc * sizeof(char *));
}
urls[url_num++] = ptr;
nl = memchr(ptr, '\n', left);
if(!nl)
break;
*nl++ = 0;
left -= nl - ptr;
ptr = nl;
}
printf("Loaded %u URLs from %s\n", url_num, FILENAME);
}
#define END_OF_LIST 0xffffff
const static int options[] = {
CURLU_DEFAULT_PORT,
CURLU_NO_DEFAULT_PORT,
CURLU_DEFAULT_SCHEME,
CURLU_NON_SUPPORT_SCHEME,
CURLU_ALLOW_SPACE,
CURLU_GUESS_SCHEME,
CURLU_PATH_AS_IS,
CURLU_DISALLOW_USER,
END_OF_LIST
};
int main(int argc, char **argv)
{
int l, o = 0, u;
int count = 0;
struct timeval start;
struct timeval end;
time_t diff;
long us;
int laps = 100;
if(argc > 1)
laps = atoi(argv[1]);
load_urls();
gettimeofday(&start, NULL);
#if DEBUG
if(argc > 2)
curl_dbg_memdebug("dump");
#endif
for(l = 0; l < laps; l++) {
// for(o = 0; options[o] != END_OF_LIST; o++) {
for(u = 0; u < url_num; u++) {
CURLU *p = curl_url();
curl_url_set(p, CURLUPART_URL, urls[u], options[o]);
curl_url_cleanup(p);
count++;
}
// }
}
gettimeofday(&end, NULL);
diff = end.tv_sec-start.tv_sec;
us = diff * 1000000 + end.tv_usec-start.tv_usec;
printf("%d URLs in %.5f secs, %.1f ns/URL, %.3f URLs/sec\n",
count, (double)us/1000000.0, (double)us*1000/count, count / (us/1000000.0));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment