test: add test case for performance

This commit is contained in:
Nick Peng
2023-03-19 14:26:46 +08:00
parent 455dca9ae4
commit aa6f6fd519
10 changed files with 307 additions and 55 deletions

58
test/utils.cc Normal file
View File

@@ -0,0 +1,58 @@
#include "include/utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
namespace smartdns
{
bool IsCommandExists(const std::string &cmd)
{
char *copy_path = NULL;
char cmd_path[4096];
const char *env_path = getenv("PATH");
char *save_ptr = NULL;
if (env_path == NULL) {
env_path = "/bin:/usr/bin:/usr/local/bin";
}
copy_path = strdup(env_path);
if (copy_path == NULL) {
return false;
}
Defer
{
free(copy_path);
};
for (char *tok = strtok_r(copy_path, ":", &save_ptr); tok; tok = strtok_r(NULL, ":", &save_ptr)) {
snprintf(cmd_path, sizeof(cmd_path), "%s/%s", tok, cmd.c_str());
if (access(cmd_path, X_OK) != 0) {
continue;
}
return true;
}
return false;
}
std::string GenerateRandomString(int len)
{
std::string result;
static const char alphanum[] = "0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
result.resize(len);
for (int i = 0; i < len; ++i) {
result[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
}
return result;
}
} // namespace smartdns