Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,35 @@ cmake-build-debug/
cmake-build-linux/

# Visual Studio 2015/2017 cache/options directory
.vs/
.vs/
cmake_install.cmake
CMakeCache.txt
install_manifest.txt
Makefile
SpeedTest
SpeedTestConfig.h
CMakeFiles/cmake.check_cache
CMakeFiles/CMakeDirectoryInformation.cmake
CMakeFiles/CMakeOutput.log
CMakeFiles/Makefile.cmake
CMakeFiles/Makefile2
CMakeFiles/progress.marks
CMakeFiles/TargetDirectories.txt
CMakeFiles/3.16.3/CMakeCCompiler.cmake
CMakeFiles/3.16.3/CMakeCXXCompiler.cmake
CMakeFiles/3.16.3/CMakeDetermineCompilerABI_C.bin
CMakeFiles/3.16.3/CMakeDetermineCompilerABI_CXX.bin
CMakeFiles/3.16.3/CMakeSystem.cmake
CMakeFiles/3.16.3/CompilerIdC/CMakeCCompilerId.c
CMakeFiles/3.16.3/CompilerIdCXX/CMakeCXXCompilerId.cpp
CMakeFiles/SpeedTest.dir/build.make
CMakeFiles/SpeedTest.dir/cmake_clean.cmake
CMakeFiles/SpeedTest.dir/CXX.includecache
CMakeFiles/SpeedTest.dir/depend.internal
CMakeFiles/SpeedTest.dir/depend.make
CMakeFiles/SpeedTest.dir/DependInfo.cmake
CMakeFiles/SpeedTest.dir/flags.make
CMakeFiles/SpeedTest.dir/link.txt
CMakeFiles/SpeedTest.dir/progress.make
CMakeFiles/Progress/1
CMakeFiles/Progress/count.txt
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.7)
project(SpeedTest)

set (SpeedTest_VERSION_MAJOR 1)
set (SpeedTest_VERSION_MINOR 14)
set (SpeedTest_VERSION_MINOR 15)
set (SpeedTest_SYSTEM_PROCESSOR ${CMAKE_SYSTEM_PROCESSOR})
set (SpeedTest_SYSTEM ${CMAKE_SYSTEM})

Expand Down
23 changes: 23 additions & 0 deletions CmdOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <getopt.h>

enum OutputType { verbose, text, json };
enum LineType { automatic, slow, narrow, broad, fiber, gigasym };


typedef struct program_options_t {
Expand All @@ -17,6 +18,7 @@ typedef struct program_options_t {
bool share = false;
std::string selected_server = "";
OutputType output_type = OutputType::verbose;
LineType line_type = LineType::automatic;
} ProgramOptions;

static struct option CmdLongOptions[] = {
Expand All @@ -27,6 +29,7 @@ static struct option CmdLongOptions[] = {
{"share", no_argument, 0, 's' },
{"test-server", required_argument, 0, 't' },
{"output", required_argument, 0, 'o' },
{"line-type", required_argument, 0, 'i' },
{0, 0, 0, 0 }
};

Expand Down Expand Up @@ -68,6 +71,26 @@ bool ParseOptions(const int argc, const char **argv, ProgramOptions& options){
return false;
}

break;
case 'i':
if (strcmp(optarg, "auto") == 0)
options.line_type = LineType::automatic;
else if (strcmp(optarg, "slow") == 0)
options.line_type = LineType::slow;
else if (strcmp(optarg, "narrow") == 0)
options.line_type = LineType::narrow;
else if (strcmp(optarg, "broad") == 0)
options.line_type = LineType::broad;
else if (strcmp(optarg, "fiber") == 0)
options.line_type = LineType::fiber;
else if (strcmp(optarg, "gigasym") == 0)
options.line_type = LineType::gigasym;
else {
std::cerr << "Unsupported line type " << optarg << std::endl;
std::cerr << "Supported line type: auto, slow, narrow, broad, fiber" <<std::endl;
return false;
}

break;
default:
return false;
Expand Down
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 taganaka

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
39 changes: 20 additions & 19 deletions SpeedTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <iomanip>
#include "SpeedTest.h"
#include "MD5Util.h"
#include <cfloat>
#include <netdb.h>

SpeedTest::SpeedTest(float minServerVersion):
Expand Down Expand Up @@ -97,19 +98,19 @@ bool SpeedTest::uploadSpeed(const ServerInfo &server, const TestConfig &config,
return true;
}

const long &SpeedTest::latency() {
const double &SpeedTest::latency() {
return mLatency;
}

bool SpeedTest::jitter(const ServerInfo &server, long& result, const int sample) {
bool SpeedTest::jitter(const ServerInfo &server, double& result, const int sample) {
auto client = SpeedTestClient(server);
double current_jitter = 0;
long previous_ms = LONG_MAX;
double previous_ms = DBL_MAX;
if (client.connect()){
for (int i = 0; i < sample; i++){
long ms = 0;
double ms = 0;
if (client.ping(ms)){
if (previous_ms == LONG_MAX) {
if (previous_ms == DBL_MAX) {
previous_ms = ms;
} else {
current_jitter += std::abs(previous_ms - ms);
Expand All @@ -121,21 +122,21 @@ bool SpeedTest::jitter(const ServerInfo &server, long& result, const int sample)
return false;
}

result = (long) std::floor(current_jitter / sample);
result = current_jitter / sample;
return true;
}


bool SpeedTest::share(const ServerInfo& server, std::string& image_url) {
std::stringstream hash;
hash << std::setprecision(0) << std::fixed << mLatency
hash << std::setprecision(2) << std::fixed << (mLatency / 1000)
<< "-" << std::setprecision(2) << std::fixed << (mUploadSpeed * 1000)
<< "-" << std::setprecision(2) << std::fixed << (mDownloadSpeed * 1000)
<< "-" << SPEED_TEST_API_KEY;
std::string hex_digest = MD5Util::hexDigest(hash.str());
std::stringstream post_data;
post_data << "download=" << std::setprecision(2) << std::fixed << (mDownloadSpeed * 1000) << "&";
post_data << "ping=" << std::setprecision(0) << std::fixed << mLatency << "&";
post_data << "ping=" << std::setprecision(2) << std::fixed << (mLatency / 1000) << "&";
post_data << "upload=" << std::setprecision(2) << std::fixed << (mUploadSpeed * 1000) << "&";
post_data << "pingselect=1&";
post_data << "recommendedserverid=" << server.id << "&";
Expand Down Expand Up @@ -186,11 +187,11 @@ double SpeedTest::execute(const ServerInfo &server, const TestConfig &config, co
auto start = std::chrono::steady_clock::now();
std::vector<double> partial_results;
while (curr_size < max_size){
long op_time = 0;
double op_time = 0;
if ((spClient.*pfunc)(curr_size, config.buff_size, op_time)) {
total_size += curr_size;
total_time += op_time;
double metric = (curr_size * 8) / (static_cast<double>(op_time) / 1000);
double metric = (curr_size * 8) / (op_time / 1000);
partial_results.push_back(metric);
if (cb)
cb(true);
Expand Down Expand Up @@ -236,7 +237,7 @@ double SpeedTest::execute(const ServerInfo &server, const TestConfig &config, co

workers.clear();

return overall_speed / 1000 / 1000;
return overall_speed / 1024 / 1024;
}

template<typename T>
Expand Down Expand Up @@ -297,7 +298,8 @@ CURL *SpeedTest::curl_setup(CURL *handler) {
if (curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeFunc) == CURLE_OK
&& curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L) == CURLE_OK
&& curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L) == CURLE_OK
&& curl_easy_setopt(curl, CURLOPT_USERAGENT, SPEED_TEST_USER_AGENT) == CURLE_OK){
&& curl_easy_setopt(curl, CURLOPT_USERAGENT, SPEED_TEST_USER_AGENT) == CURLE_OK
&& curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "br, gzip, deflate") == CURLE_OK) {
return curl;
} else {
curl_easy_cleanup(handler);
Expand Down Expand Up @@ -492,12 +494,12 @@ bool SpeedTest::fetchServers(const std::string& url, std::vector<ServerInfo>& ta
return true;
}

const ServerInfo SpeedTest::findBestServerWithin(const std::vector<ServerInfo> &serverList, long &latency,
const ServerInfo SpeedTest::findBestServerWithin(const std::vector<ServerInfo> &serverList, double &latency,
const int sample_size, std::function<void(bool)> cb) {
int i = sample_size;
ServerInfo bestServer = serverList[0];

latency = INT_MAX;
latency = DBL_MAX;

for (auto &server : serverList){
auto client = SpeedTestClient(server);
Expand All @@ -513,7 +515,7 @@ const ServerInfo SpeedTest::findBestServerWithin(const std::vector<ServerInfo> &
continue;
}

long current_latency = LONG_MAX;
double current_latency = DBL_MAX;
if (testLatency(client, 20, current_latency)){
if (current_latency < latency){
latency = current_latency;
Expand All @@ -532,12 +534,12 @@ const ServerInfo SpeedTest::findBestServerWithin(const std::vector<ServerInfo> &
return bestServer;
}

bool SpeedTest::testLatency(SpeedTestClient &client, const int sample_size, long &latency) {
bool SpeedTest::testLatency(SpeedTestClient &client, const int sample_size, double &latency) {
if (!client.connect()){
return false;
}
latency = INT_MAX;
long temp_latency = 0;
latency = DBL_MAX;
double temp_latency = 0;
for (int i = 0; i < sample_size; i++){
if (client.ping(temp_latency)){
if (temp_latency < latency){
Expand All @@ -549,4 +551,3 @@ bool SpeedTest::testLatency(SpeedTestClient &client, const int sample_size, long
}
return true;
}

12 changes: 6 additions & 6 deletions SpeedTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "DataTypes.h"

class SpeedTestClient;
typedef bool (SpeedTestClient::*opFn)(const long size, const long chunk_size, long &millisec);
typedef bool (SpeedTestClient::*opFn)(const long size, const long chunk_size, double &millisec);
typedef void (*progressFn)(bool success);


Expand All @@ -38,15 +38,15 @@ class SpeedTest {
const std::vector<ServerInfo>& serverList();
const ServerInfo bestServer(int sample_size = 5, std::function<void(bool)> cb = nullptr);
bool setServer(ServerInfo& server);
const long &latency();
const double &latency();
bool downloadSpeed(const ServerInfo& server, const TestConfig& config, double& result, std::function<void(bool)> cb = nullptr);
bool uploadSpeed(const ServerInfo& server, const TestConfig& config, double& result, std::function<void(bool)> cb = nullptr);
bool jitter(const ServerInfo& server, long& result, int sample = 40);
bool jitter(const ServerInfo& server, double& result, int sample = 40);
bool share(const ServerInfo& server, std::string& image_url);
private:
bool fetchServers(const std::string& url, std::vector<ServerInfo>& target, int &http_code);
bool testLatency(SpeedTestClient& client, int sample_size, long& latency);
const ServerInfo findBestServerWithin(const std::vector<ServerInfo>& serverList, long& latency, int sample_size = 5, std::function<void(bool)> cb = nullptr);
bool testLatency(SpeedTestClient& client, int sample_size, double& latency);
const ServerInfo findBestServerWithin(const std::vector<ServerInfo>& serverList, double& latency, int sample_size = 5, std::function<void(bool)> cb = nullptr);
static CURL* curl_setup(CURL* curl = nullptr);
static size_t writeFunc(void* buf, size_t size, size_t nmemb, void* userp);
static ServerInfo processServerXMLNode(xmlTextReaderPtr reader);
Expand All @@ -58,7 +58,7 @@ class SpeedTest {

IPInfo mIpInfo;
std::vector<ServerInfo> mServerList;
long mLatency;
double mLatency;
double mUploadSpeed;
double mDownloadSpeed;
float mMinSupportedServer;
Expand Down
15 changes: 9 additions & 6 deletions SpeedTestClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void SpeedTestClient::close() {
}

// It executes PING command
bool SpeedTestClient::ping(long &millisec) {
bool SpeedTestClient::ping(double &millisec) {
if (!mSocketFd)
return false;

Expand All @@ -80,7 +80,8 @@ bool SpeedTestClient::ping(long &millisec) {
if (SpeedTestClient::readLine(mSocketFd, reply)){
if (reply.substr(0, 5) == "PONG "){
auto stop = std::chrono::steady_clock::now();
millisec = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count();
double microsec = std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count();
millisec = microsec / 1000;
return true;
}
}
Expand All @@ -90,7 +91,7 @@ bool SpeedTestClient::ping(long &millisec) {
}

// It executes DOWNLOAD command
bool SpeedTestClient::download(const long size, const long chunk_size, long &millisec) {
bool SpeedTestClient::download(const long size, const long chunk_size, double &millisec) {
std::stringstream cmd;
cmd << "DOWNLOAD " << size;

Expand All @@ -116,13 +117,14 @@ bool SpeedTestClient::download(const long size, const long chunk_size, long &mil
}

auto stop = std::chrono::steady_clock::now();
millisec = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count();
double microsec = std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count();
millisec = microsec / 1000;
delete[] buff;
return true;
}

// It executes UPLOAD command
bool SpeedTestClient::upload(const long size, const long chunk_size, long &millisec) {
bool SpeedTestClient::upload(const long size, const long chunk_size, double &millisec) {
std::stringstream cmd;
cmd << "UPLOAD " << size << "\n";
auto cmd_len = cmd.str().length();
Expand Down Expand Up @@ -170,7 +172,8 @@ bool SpeedTestClient::upload(const long size, const long chunk_size, long &milli

std::stringstream ss;
ss << "OK " << size << " ";
millisec = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count();
double microsec = std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count();
millisec = microsec / 1000;
delete[] buff;
return reply.substr(0, ss.str().length()) == ss.str();

Expand Down
8 changes: 4 additions & 4 deletions SpeedTestClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class SpeedTestClient {
~SpeedTestClient();

bool connect();
bool ping(long &millisec);
bool upload(long size, long chunk_size, long &millisec);
bool download(long size, long chunk_size, long &millisec);
bool ping(double &millisec);
bool upload(long size, long chunk_size, double &millisec);
bool download(long size, long chunk_size, double &millisec);
float version();
const std::pair<std::string, int> hostport();
void close();
Expand All @@ -39,5 +39,5 @@ class SpeedTestClient {
static bool writeLine(int& fd, const std::string& buffer);
};

typedef bool (SpeedTestClient::*opFn)(const long size, const long chunk_size, long &millisec);
typedef bool (SpeedTestClient::*opFn)(const long size, const long chunk_size, double &millisec);
#endif //SPEEDTEST_SPEEDTESTCLIENT_H
Loading