Skip to content

Commit

Permalink
Add: DiskBenchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
dnzbk committed Sep 2, 2024
1 parent cc8ff58 commit 92df53b
Show file tree
Hide file tree
Showing 13 changed files with 743 additions and 33 deletions.
3 changes: 2 additions & 1 deletion daemon/main/nzbget.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/


Expand Down Expand Up @@ -224,6 +224,7 @@ compiled */
#include <variant>
#include <limits>
#include <type_traits>
#include <random>

#include <libxml/parser.h>
#include <libxml/xmlreader.h>
Expand Down
118 changes: 118 additions & 0 deletions daemon/remote/XmlRpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#include "UrlCoordinator.h"
#include "ExtensionManager.h"
#include "SystemInfo.h"
#include "Benchmark.h"
#include "Xml.h"

extern void ExitProc();
extern void Reload();
Expand Down Expand Up @@ -361,6 +363,42 @@ class TestServerSpeedXmlCommand: public SafeXmlCommand
virtual void Execute();
};

class TestDiskSpeedXmlCommand: public SafeXmlCommand
{
public:
virtual void Execute();

std::string ToJsonStr(const std::pair<uint64_t, double>& res)
{
Json::JsonObject json;

json["SizeMB"] = res.first / 1024ull / 1024ull;
json["DurationMS"] = res.second;

return Json::Serialize(json);
}

std::string ToXmlStr(const std::pair<uint64_t, double>& res)
{
xmlNodePtr rootNode = xmlNewNode(nullptr, BAD_CAST "value");
xmlNodePtr structNode = xmlNewNode(nullptr, BAD_CAST "struct");

std::string sizeMB = std::to_string(res.first / 1024ull / 1024ull);
std::string durationMS = std::to_string(res.second);

Xml::AddNewNode(structNode, "SizeMB", "i4", sizeMB.c_str());
Xml::AddNewNode(structNode, "DurationMS", "double", durationMS.c_str());

xmlAddChild(rootNode, structNode);

std::string result = Xml::Serialize(rootNode);

xmlFreeNode(rootNode);

return result;
}
};

class StartScriptXmlCommand : public XmlCommand
{
public:
Expand Down Expand Up @@ -816,6 +854,10 @@ std::unique_ptr<XmlCommand> XmlRpcProcessor::CreateCommand(const char* methodNam
else if (!strcasecmp(methodName, "testserverspeed"))
{
command = std::make_unique<TestServerSpeedXmlCommand>();
}
else if (!strcasecmp(methodName, "testdiskspeed"))
{
command = std::make_unique<TestDiskSpeedXmlCommand>();
}
else if (!strcasecmp(methodName, "startscript"))
{
Expand Down Expand Up @@ -3696,6 +3738,82 @@ void TestServerSpeedXmlCommand::Execute()
BuildBoolResponse(true);
}



void TestDiskSpeedXmlCommand::Execute()
{
char* dirPath;
int writeBufferKiB;
int maxFileSizeGiB;
int timeoutSec;

if (!NextParamAsStr(&dirPath))
{
BuildErrorResponse(2, "Invalid argument (Path)");
return;
}

if (!NextParamAsInt(&writeBufferKiB))
{
BuildErrorResponse(2, "Invalid argument (Write Buffer)");
return;
}

if (writeBufferKiB < 0)
{
BuildErrorResponse(2, "Write Buffer cannot be negative");
return;
}

if (!NextParamAsInt(&maxFileSizeGiB))
{
BuildErrorResponse(2, "Invalid argument (Max file size)");
return;
}

if (maxFileSizeGiB < 1)
{
BuildErrorResponse(2, "Max file size must be positive");
return;
}

if (!NextParamAsInt(&timeoutSec))
{
BuildErrorResponse(2, "Invalid argument (Timeout)");
return;
}

if (timeoutSec < 1)
{
BuildErrorResponse(2, "Timeout must be positive");
return;
}

try
{
size_t bufferSizeBytes = writeBufferKiB * 1024;
uint64_t maxFileSizeBytes = maxFileSizeGiB * 1024ull * 1024ull * 1024ull;

Benchmark::DiskBenchmark db;
auto res = db.Run(
dirPath,
bufferSizeBytes,
maxFileSizeBytes,
std::chrono::seconds(timeoutSec)
);

std::string jsonStr = IsJson() ?
ToJsonStr(res) :
ToXmlStr(res);

AppendResponse(jsonStr.c_str());
}
catch (const std::exception& e)
{
BuildErrorResponse(2, e.what());
}
}

// bool startscript(string script, string command, string context, struct[] options);
void StartScriptXmlCommand::Execute()
{
Expand Down
1 change: 1 addition & 0 deletions daemon/sources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ set(SRC
${CMAKE_SOURCE_DIR}/daemon/util/Util.cpp
${CMAKE_SOURCE_DIR}/daemon/util/Json.cpp
${CMAKE_SOURCE_DIR}/daemon/util/Xml.cpp
${CMAKE_SOURCE_DIR}/daemon/util/Benchmark.cpp

${CMAKE_SOURCE_DIR}/daemon/system/SystemInfo.cpp
${CMAKE_SOURCE_DIR}/daemon/system/OS.cpp
Expand Down
173 changes: 173 additions & 0 deletions daemon/util/Benchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
* This file is part of nzbget. See <https://nzbget.com>.
*
* Copyright (C) 2024 Denis <denis@nzbget.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "nzbget.h"

#include <exception>
#include <random>
#include "FileSystem.h"
#include "Benchmark.h"

namespace Benchmark
{
using namespace std::chrono;

std::pair<uint64_t, double> DiskBenchmark::Run(
const std::string& dir,
size_t bufferSizeBytes,
uint64_t maxFileSizeBytes,
seconds timeout) const noexcept(false)
{
ValidateBufferSize(bufferSizeBytes);

const std::string filename = dir + PATH_SEPARATOR + GetUniqueFilename();
std::ofstream file = OpenFile(filename);

std::vector<char> buffer;
UseBuffer(file, buffer, bufferSizeBytes);

size_t dataSize = bufferSizeBytes > 0 ? bufferSizeBytes : 1024;
std::vector<char> data = GenerateRandomCharsVec(dataSize);

return RunBench(file, filename, data, maxFileSizeBytes, timeout);
}

std::pair<uint64_t, double> DiskBenchmark::RunBench(
std::ofstream& file,
const std::string& filename,
const std::vector<char>& data,
uint64_t maxFileSizeBytes,
std::chrono::seconds timeout) const noexcept(false)
{
const char* dataToWrite = data.data();
size_t dataSize = data.size();
uint64_t totalWritten = 0;

auto start = high_resolution_clock::now();
try
{
while (
totalWritten < maxFileSizeBytes &&
duration_cast<seconds>(high_resolution_clock::now() - start) < timeout)
{
file.write(dataToWrite, dataSize);
totalWritten += dataSize;
}
}
catch (const std::exception& e)
{
CleanUp(file, filename);

std::string errMsg = "Failed to write data to file " + filename + ". " + e.what();
throw std::runtime_error(errMsg);
}
auto finish = high_resolution_clock::now();

CleanUp(file, filename);

double elapsed = duration<double, std::milli>(finish - start).count();

return { totalWritten, elapsed };
}

void DiskBenchmark::DeleteFile(const std::string& filename) const noexcept(false)
{
if (!FileSystem::DeleteFile(filename.c_str()))
{
std::string errMsg = "Failed to delete " + filename + " test file";
throw std::runtime_error(errMsg);
}
}

void DiskBenchmark::CleanUp(
std::ofstream& file,
const std::string& filename) const noexcept(false)
{
file.close();
DeleteFile(filename);
}

std::string DiskBenchmark::GetUniqueFilename() const noexcept(false)
{
return std::to_string(
high_resolution_clock::now().time_since_epoch().count()
) + ".bin";
}

std::ofstream DiskBenchmark::OpenFile(const std::string& filename) const noexcept(false)
{
std::ofstream file(filename, std::ios::binary);

if (!file.is_open())
{
std::string errMsg = std::string("Failed to create test file: ") + strerror(errno);
throw std::runtime_error(errMsg);
}

file.exceptions(std::ofstream::badbit | std::ofstream::failbit);
file.sync_with_stdio(false);

return file;
}

void DiskBenchmark::ValidateBufferSize(size_t bufferSz) const noexcept(false)
{
if (bufferSz > m_maxBufferSize)
{
throw std::invalid_argument("The buffer size is too big");
}
}

void DiskBenchmark::UseBuffer(
std::ofstream& file,
std::vector<char>& buffer,
size_t buffSize
) const noexcept(false)
{
if (buffSize > 0)
{
buffer.resize(buffSize);
file.rdbuf()->pubsetbuf(buffer.data(), buffSize);
}
else
{
file.rdbuf()->pubsetbuf(nullptr, 0);
}
}

std::vector<char> DiskBenchmark::GenerateRandomCharsVec(size_t size) const noexcept(false)
{
if (size == 0)
{
return {};
}

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distrib(0, 127);

std::vector<char> v(size);
std::generate(begin(v), end(v), [&distrib, &gen]()
{
return static_cast<char>(distrib(gen));
});

return v;
}
}
Loading

0 comments on commit 92df53b

Please sign in to comment.