Skip to content

Commit

Permalink
Usage added
Browse files Browse the repository at this point in the history
  • Loading branch information
taganaka committed Jun 4, 2016
1 parent e6e987c commit 7178111
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 24 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set (SpeedTest_VERSION_MINOR 0)
set (SpeedTest_SYSTEM_PROCESSOR ${CMAKE_SYSTEM_PROCESSOR})
set (SpeedTest_SYSTEM ${CMAKE_SYSTEM})

set (SpeedTest_AUTHOR "Francesco Laurita <francesco.laurita@gmail.com")
set (SpeedTest_AUTHOR "Francesco Laurita <francesco.laurita@gmail.com>")
set (SpeedTest_USER_AGENT "Mozilla/5.0 ${CMAKE_SYSTEM}; U; ${CMAKE_SYSTEM_PROCESSOR}; en-us (KHTML, like Gecko) SpeedTest++/${SpeedTest_VERSION_MAJOR}.${SpeedTest_VERSION_MINOR}")
set (SpeedTest_SERVER_LIST_URL "http://www.speedtest.net/speedtest-servers-static.php")
set (SpeedTest_IP_INFO_API_URL "http://speedtest.ookla.com/api/ipaddress.php")
Expand All @@ -22,7 +22,7 @@ set(SOURCE_FILES
SpeedTestClient.cpp
SpeedTestClient.h
TestConfigTemplate.h
)
MD5Util.cpp MD5Util.h)

configure_file (
"${PROJECT_SOURCE_DIR}/SpeedTestConfig.h.in"
Expand Down
22 changes: 22 additions & 0 deletions MD5Util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Created by Francesco Laurita on 6/3/16.
//

#include <sstream>
#include "MD5Util.h"

std::string MD5Util::hexDigest(const std::string &str) {
unsigned char digest[MD5_DIGEST_LENGTH];

MD5_CTX ctx;
MD5_Init(&ctx);
MD5_Update(&ctx, str.c_str(), str.size());
MD5_Final(digest, &ctx);

char hexDigest[33] = {'\0'};
for (int i = 0; i < 16; i++)
std::sprintf(&hexDigest[i*2], "%02x", (unsigned int)digest[i]);

return std::string(hexDigest);
}

23 changes: 23 additions & 0 deletions MD5Util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Created by Francesco Laurita on 6/3/16.
//

#ifndef SPEEDTEST_MD5UTIL_H
#define SPEEDTEST_MD5UTIL_H
#if defined(__APPLE__)
# define COMMON_DIGEST_FOR_OPENSSL
# include <CommonCrypto/CommonDigest.h>
#include <string>

# define SHA1 CC_SHA1
#else
# include <openssl/md5.h>
#endif

class MD5Util {
public:
static std::string hexDigest(const std::string &str);
};


#endif //SPEEDTEST_MD5UTIL_H
29 changes: 13 additions & 16 deletions SpeedTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@
#include <cmath>
#include "SpeedTest.h"
#include "SpeedTestClient.h"
#if defined(__APPLE__)
# define COMMON_DIGEST_FOR_OPENSSL
# include <CommonCrypto/CommonDigest.h>
# define SHA1 CC_SHA1
#else
# include <openssl/md5.h>
#endif

SpeedTest::SpeedTest() {
curl_global_init(CURL_GLOBAL_DEFAULT);
mIpInfo = IPInfo();
mServerList = std::vector<ServerInfo>();
mLatency = -1;
mLatency = 0;
mUploadSpeed = 0;
mDownloadSpeed = 0;
}

SpeedTest::~SpeedTest() {
Expand Down Expand Up @@ -139,14 +134,16 @@ const ServerInfo SpeedTest::bestServer(const int sample_size) {
return bestServer;
}

const float SpeedTest::downloadSpeed(const ServerInfo &server, const TestConfig &config) {
const double SpeedTest::downloadSpeed(const ServerInfo &server, const TestConfig &config) {
opFn pfunc = &SpeedTestClient::download;
return execute(server, config, pfunc);
mDownloadSpeed = execute(server, config, pfunc);
return mDownloadSpeed;
}

const float SpeedTest::uploadSpeed(const ServerInfo &server, const TestConfig &config) {
const double SpeedTest::uploadSpeed(const ServerInfo &server, const TestConfig &config) {
opFn pfunc = &SpeedTestClient::upload;
return execute(server, config, pfunc);
mUploadSpeed = execute(server, config, pfunc);
return mUploadSpeed;
}

const double &SpeedTest::latency() {
Expand All @@ -157,7 +154,7 @@ const double &SpeedTest::latency() {

float SpeedTest::execute(const ServerInfo &server, const TestConfig &config, const opFn &pfunc) {
std::vector<std::thread> workers;
float overall_speed = 0;
double overall_speed = 0;
std::mutex mtx;
for (int i = 0; i < config.concurrency; i++) {
workers.push_back(std::thread([&server, &overall_speed, &pfunc, &config, &mtx](){
Expand All @@ -172,13 +169,13 @@ float SpeedTest::execute(const ServerInfo &server, const TestConfig &config, con
long total_size = 0;
long total_time = 0;
auto start = SpeedTestClient::now();
std::vector<float> partial_results;
std::vector<double> partial_results;
while (curr_size < max_size){
long op_time = 0;
if ((spClient.*pfunc)(curr_size, config.buff_size, op_time)) {
total_size += curr_size;
total_time += op_time;
float metric = (curr_size * 8) / (static_cast<float>(op_time) / 1000);
double metric = (curr_size * 8) / (static_cast<double>(op_time) / 1000);
partial_results.push_back(metric);
std::cout << "." << std::flush;
} else {
Expand All @@ -200,7 +197,7 @@ float SpeedTest::execute(const ServerInfo &server, const TestConfig &config, con
}

size_t iter = 0;
float real_sum = 0;
double real_sum = 0;
for (auto it = partial_results.begin() + skip; it != partial_results.end() - drop; ++it ){
iter++;
real_sum += (*it);
Expand Down
6 changes: 4 additions & 2 deletions SpeedTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ class SpeedTest {
const std::vector<ServerInfo>& serverList();
const ServerInfo bestServer(const int sample_size = 5);
const double &latency();
const float downloadSpeed(const ServerInfo& server, const TestConfig& config);
const float uploadSpeed(const ServerInfo& server, const TestConfig& config);
const double downloadSpeed(const ServerInfo& server, const TestConfig& config);
const double uploadSpeed(const ServerInfo& server, const TestConfig& config);
private:
static CURL* curl_setup(CURL* curl = nullptr);
static size_t writeFunc(void* buf, size_t size, size_t nmemb, void* userp);
Expand All @@ -80,6 +80,8 @@ class SpeedTest {
IPInfo mIpInfo;
std::vector<ServerInfo> mServerList;
double mLatency;
double mUploadSpeed;
double mDownloadSpeed;

};

Expand Down
23 changes: 19 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@
#include "SpeedTest.h"
#include "TestConfigTemplate.h"


void banner(){
std::cout << "SpeedTest++ version " << SpeedTest_VERSION_MAJOR << "." << SpeedTest_VERSION_MINOR << std::endl;
std::cout << "speedtest.net command line interface" << std::endl;
std::cout << "Author: " << SpeedTest_AUTHOR << std::endl;
}

void usage(const char* name){
std::cout << "usage: " << name << " ";
std::cout << "[--latency] [--download] [--upload] [--help]" << std::endl;
std::cout << "optional arguments:" << std::endl;
std::cout << "\t--help Show this message and exit\n";
std::cout << "\t--latency Perform latency test only\n";
std::cout << "\t--download Perform download test only. It includes latency test\n";
std::cout << "\t--upload Perform upload test only. It includes latency test\n";
}

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

bool download_only = false;
Expand All @@ -20,6 +30,14 @@ int main(const int argc, const char **argv) {
for (int i = 0; i < argc; i++)
options.push_back(std::string(argv[i]));

banner();
std::cout << std::endl;

if (std::find(options.begin(), options.end(), "--help") != options.end()) {
usage(argv[0]);
return EXIT_SUCCESS;
}

if (std::find(options.begin(), options.end(), "--latency") != options.end())
latency_only = true;

Expand All @@ -29,9 +47,6 @@ int main(const int argc, const char **argv) {
if (std::find(options.begin(), options.end(), "--upload") != options.end())
upload_only = true;

std::cout << std::endl;
banner();
std::cout << std::endl;

auto sp = SpeedTest();
IPInfo info;
Expand Down

0 comments on commit 7178111

Please sign in to comment.