Skip to content

Instantly share code, notes, and snippets.

@jglenn9k
Last active July 5, 2018 20:49
Show Gist options
  • Save jglenn9k/1183fe7eab534be3d83c71c615e8208c to your computer and use it in GitHub Desktop.
Save jglenn9k/1183fe7eab534be3d83c71c615e8208c to your computer and use it in GitHub Desktop.
Checks page speed
#include <iostream>
#include <string>
#include <cstdio>
#include <curl/curl.h>
// James Glenn <thedonkdonk@gmail.com>
// 2018-03-30
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
int main(int argc, char **argv) {
if (argc != 2) {
std::cout << "ERROR - Must provide a URL." << std::endl;
return 2;
}
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_HEADER, false);
curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, false);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Page Speed Checker v1.0");
curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
if(CURLE_OK == res) {
char *url = NULL;
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
double dlsize;
curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &dlsize);
double dlspeed;
curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &dlspeed);
double namelookup;
curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME, &namelookup);
double connect;
curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &connect);
double pretransfer;
curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME, &pretransfer);
double start;
curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME, &start);
double redirect;
curl_easy_getinfo(curl, CURLINFO_REDIRECT_TIME, &redirect);
double total;
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total);
long redirects;
curl_easy_getinfo(curl, CURLINFO_REDIRECT_COUNT, &redirects);
if((CURLE_OK == res) && response_code) {
printf("Redirected to: %s\n", url);
printf("Response Code: %ld\n", response_code);
printf("Total Redirects: %ld\n", redirects);
printf("Downloaded: %.0f bytes\n", dlsize);
printf("Download speed %.0f bytes/sec\n", dlspeed);
printf("Name Lookup Time: %.2f\n", namelookup);
printf("Connect Time: %.2f\n", connect);
printf("Pretransfer Time: %.2f\n", pretransfer);
printf("Start Time: %.2f\n", start);
printf("Redirect Time: %.2f\n", redirect);
printf("Total Time: %.2f\n", total);
}
}
curl_easy_cleanup(curl);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment