https получить с помощью libcurl против Голанга - PullRequest
0 голосов
/ 23 февраля 2019

Скажем, у меня есть список URL:

$ for i in `seq 1 90`; do echo "$RANDOM$RANDOM.blogspot.com" ; done >> /tmp/urls.txt

Мои GET-ы в C занимают гораздо больше времени, чем при использовании моего кода go.

Вот код C:

n_memory.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

struct MemoryStruct {
  char *memory;
  size_t size;
};

static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
  size_t realsize = size * nmemb;
  struct MemoryStruct *mem = (struct MemoryStruct *)userp;
  char *ptr = realloc(mem->memory, mem->size + realsize + 1);
  if(ptr == NULL) {
    printf("not enough memory (realloc returned NULL)\n");
    return 0;
  }

  mem->memory = ptr;
  memcpy(&(mem->memory[mem->size]), contents, realsize);
  mem->size += realsize;
  mem->memory[mem->size] = 0;
  return realsize;
}

int try_url(char *url);

int main(int argc, char **argv){

        if (argc < 2){
                fprintf(stderr, "error, argc\n");
                return 1;
        }
        FILE *fp = fopen(argv[1],"r");
        if (!fp){
                fprintf(stderr, "fopen, argc\n");
                return 1;
        }
        int count = 1;
        char _line[2048];
        char url[8192];
        while ( fgets(_line, 1024, fp) ){
               _line[strcspn(_line, "\r\n")] = 0;
                char *part1 = "https://dns.google.com/resolve?name=";
                char *part3 = "&type=A";
                snprintf(url, 4096, "%s%s%s", part1, _line, part3);
                printf("%d %s\n", count, url);
                try_url(url);
                if (count > 80){
                        break;
                }
                count++;
        }

        //try_url(argv[1]);
        puts("Done");
        return 0;
}



int try_url(char *url)
{
  CURL *hnd;
  CURLcode res;
  struct curl_slist *slist1;
  struct MemoryStruct chunk;

  chunk.memory = malloc(1);  /* will be grown as needed by the realloc above */
  chunk.size = 0;    /* no data at this point */

  curl_global_init(CURL_GLOBAL_ALL);
  hnd = curl_easy_init();
  curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
  curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
  curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2TLS);
  curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
  curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
  curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);
  curl_easy_setopt(hnd, CURLOPT_RESOLVE, slist1);

  slist1 = NULL;
  slist1 = curl_slist_append(slist1, "dns.google.com:443:172.217.5.110");

  curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0);
  curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0);
  curl_easy_setopt(hnd, CURLOPT_URL, url);
  curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  curl_easy_setopt(hnd, CURLOPT_WRITEDATA, (void *)&chunk);
  curl_easy_setopt(hnd, CURLOPT_USERAGENT, "libcurl-agent/1.0");
  res = curl_easy_perform(hnd);
  if(res != CURLE_OK) {
    fprintf(stderr, "curl_easy_perform() failed: %s\n",
            curl_easy_strerror(res));
  }
  else {
    printf("%lu bytes retrieved\n", (unsigned long)chunk.size);
  }

  curl_easy_cleanup(hnd);
  free(chunk.memory);
  curl_global_cleanup();
  return 0;
}

и вот код:

n_get.go

package main

import (
        "bufio"
        "fmt"
        "log"
        "net/http"
        "os"
        "time"
)

func main() {

        if len(os.Args) < 2 {
                fmt.Println("Invalid usage")
                os.Exit(1)
        }

        filename := os.Args[1]

        f, err := os.Open(filename)
        checkerr(err)
        defer f.Close()
        fscanner := bufio.NewScanner(f)
        i := 1
        for fscanner.Scan() {
                text := fscanner.Text()
                // https://dns.google.com/resolve?name=1.bp.blogspot.com&type=A
                url := "https://dns.google.com/resolve?name=" + text + "&type=A"
                //fmt.Println(i, url);
                get_url(url)
                if i == 80 {
                        break
                }
                i = i + 1
        }

        fmt.Println("Hello!")
}

func checkerr(err error) {
        if err != nil {
                fmt.Println(err)
                log.Fatal(err)
        }
}

func get_url(url string) int {
        fmt.Println(url)
        t1 := time.Now()
        resp, err := http.Get(url)
        t2 := time.Now()
        checkerr(err)
        fmt.Println(resp.Status)
        diff := t2.Sub(t1)
        fmt.Println(url, "Took us", diff)
        if resp.StatusCode == 200 {
                fmt.Println("OK")
                return 0
        } else {
                fmt.Println("Failed")
                return 1
        }

}

Я даже пытался помочь libcurl с опцией --resolve передать IP-адрес, который он может использовать, что избавило его от необходимости выполнять поиск по имени.Однако это не очень помогает.

Даже пробовал вариант --insecure с curl, все еще не делает много вмятины.

Вот время, необходимое для выполнения 80 HTTPS GET:

+------------------+-----------------+
|      golang      |   c             |
+------------------------------------+
| real    0m2.670s |real    0m20.024s|
| user    0m0.555s |user    0m13.393s|
| sys     0m0.086s |sys     0m0.242s |
+------------------------------------+

Это немного однобокий, и я ищу указатели, чтобы закрыть пробел.Как я могу улучшить скорость моего кода C?Любые пункты будут высоко оценены.

1 Ответ

0 голосов
/ 23 февраля 2019

Прежде всего, не запускайте весь curl init для каждой попытки.Сделайте это один раз.

Я не думаю, что вам также нужно каждый раз делать все опции.

И не делайте это malloc из 1 байта.Просто оставьте это NULL.Realloc знает, как с этим справиться.

...