Benchmark refactoring, zero delay submit and unified HTTP layer.

This commit is contained in:
XMRig 2020-11-16 16:22:34 +07:00
parent be8245fc92
commit c1d99bfa09
No known key found for this signature in database
GPG key ID: 446A53638BE94409
21 changed files with 558 additions and 380 deletions

View file

@ -176,9 +176,7 @@ void xmrig::Job::copy(const Job &other)
# endif
# ifdef XMRIG_FEATURE_BENCHMARK
m_benchSize = other.m_benchSize;
m_benchHash = other.m_benchHash;
m_benchToken = other.m_benchToken;
m_benchSize = other.m_benchSize;
# endif
}
@ -213,8 +211,6 @@ void xmrig::Job::move(Job &&other)
# endif
# ifdef XMRIG_FEATURE_BENCHMARK
m_benchSize = other.m_benchSize;
m_benchHash = other.m_benchHash;
m_benchToken = std::move(other.m_benchToken);
m_benchSize = other.m_benchSize;
# endif
}

View file

@ -112,12 +112,8 @@ public:
inline Job &operator=(Job &&other) noexcept { move(std::move(other)); return *this; }
# ifdef XMRIG_FEATURE_BENCHMARK
inline const String &benchToken() const { return m_benchToken; }
inline uint32_t benchSize() const { return m_benchSize; }
inline uint64_t benchHash() const { return m_benchHash; }
inline void setBenchHash(uint64_t benchHash) { m_benchHash = benchHash; }
inline void setBenchSize(uint32_t size) { m_benchSize = size; }
inline void setBenchToken(const String &token) { m_benchToken = token; }
# endif
private:
@ -146,9 +142,7 @@ private:
# endif
# ifdef XMRIG_FEATURE_BENCHMARK
String m_benchToken;
uint32_t m_benchSize = 0;
uint64_t m_benchHash = 0;
# endif
};

View file

@ -19,6 +19,8 @@
#include "base/net/stratum/benchmark/BenchClient.h"
#include "3rdparty/fmt/core.h"
#include "3rdparty/rapidjson/document.h"
#include "backend/common/benchmark/BenchState.h"
#include "backend/common/interfaces/IBackend.h"
#include "backend/cpu/Cpu.h"
#include "base/io/json/Json.h"
#include "base/io/log/Log.h"
@ -33,7 +35,8 @@
xmrig::BenchClient::BenchClient(const std::shared_ptr<BenchConfig> &benchmark, IClientListener* listener) :
m_listener(listener),
m_benchmark(benchmark)
m_benchmark(benchmark),
m_hash(benchmark->hash())
{
std::vector<char> blob(112 * 2 + 1, '0');
blob.back() = '\0';
@ -43,7 +46,8 @@ xmrig::BenchClient::BenchClient(const std::shared_ptr<BenchConfig> &benchmark, I
m_job.setDiff(std::numeric_limits<uint64_t>::max());
m_job.setHeight(1);
m_job.setBenchSize(m_benchmark->size());
m_job.setBenchHash(m_benchmark->hash());
BenchState::setListener(this);
# ifdef XMRIG_FEATURE_HTTP
if (m_benchmark->isSubmit()) {
@ -54,8 +58,8 @@ xmrig::BenchClient::BenchClient(const std::shared_ptr<BenchConfig> &benchmark, I
if (!m_benchmark->id().isEmpty()) {
m_job.setId(m_benchmark->id());
m_job.setBenchToken(m_benchmark->token());
m_mode = ONLINE_VERIFY;
m_token = m_benchmark->token();
m_mode = ONLINE_VERIFY;
return;
}
@ -63,7 +67,7 @@ xmrig::BenchClient::BenchClient(const std::shared_ptr<BenchConfig> &benchmark, I
m_job.setId("00000000");
if (m_job.benchHash() && m_job.setSeedHash(m_benchmark->seed())) {
if (m_hash && m_job.setSeedHash(m_benchmark->seed())) {
m_mode = STATIC_VERIFY;
return;
@ -74,6 +78,12 @@ xmrig::BenchClient::BenchClient(const std::shared_ptr<BenchConfig> &benchmark, I
}
xmrig::BenchClient::~BenchClient()
{
BenchState::destroy();
}
void xmrig::BenchClient::connect()
{
# ifdef XMRIG_FEATURE_HTTP
@ -100,6 +110,54 @@ void xmrig::BenchClient::setPool(const Pool &pool)
}
void xmrig::BenchClient::onBenchDone(uint64_t result, uint64_t ts)
{
# ifdef XMRIG_FEATURE_HTTP
if (m_mode == ONLINE_BENCH) {
m_doneTime = ts;
rapidjson::Document doc(rapidjson::kObjectType);
auto &allocator = doc.GetAllocator();
doc.AddMember("steady_done_ts", m_doneTime, allocator);
doc.AddMember("hash", rapidjson::Value(fmt::format("{:016X}", result).c_str(), allocator), allocator);
doc.AddMember("backend", m_backend->toJSON(doc), allocator);
update(doc);
}
# endif
const uint64_t ref = referenceHash();
const char *color = ref ? ((result == ref) ? GREEN_BOLD_S : RED_BOLD_S) : BLACK_BOLD_S;
LOG_NOTICE("%s " WHITE_BOLD("benchmark finished in ") CYAN_BOLD("%.3f seconds") WHITE_BOLD_S " hash sum = " CLEAR "%s%016" PRIX64 CLEAR, Tags::bench(), static_cast<double>(ts - m_startTime) / 1000.0, color, result);
if (m_mode != ONLINE_BENCH) {
printExit();
}
}
void xmrig::BenchClient::onBenchStart(uint64_t ts, uint32_t threads, const IBackend *backend)
{
m_startTime = ts;
m_threads = threads;
m_backend = backend;
# ifdef XMRIG_FEATURE_HTTP
if (m_mode == ONLINE_BENCH) {
rapidjson::Document doc(rapidjson::kObjectType);
auto &allocator = doc.GetAllocator();
doc.AddMember("threads", threads, allocator);
doc.AddMember("steady_start_ts", m_startTime, allocator);
update(doc);
}
# endif
}
void xmrig::BenchClient::onHttpData(const HttpData &data)
{
# ifdef XMRIG_FEATURE_HTTP
@ -116,15 +174,39 @@ void xmrig::BenchClient::onHttpData(const HttpData &data)
}
if (m_mode == ONLINE_BENCH) {
startBench(doc);
}
else {
startVerify(doc);
if (!m_startTime) {
return startBench(doc);
}
if (m_doneTime) {
LOG_NOTICE("%s " WHITE_BOLD("benchmark submitted ") CYAN_BOLD("https://xmrig.com/benchmark/%s"), Tags::bench(), m_job.id().data());
printExit();
}
return;
}
startVerify(doc);
# endif
}
uint64_t xmrig::BenchClient::referenceHash() const
{
if (m_hash || m_mode == ONLINE_BENCH) {
return m_hash;
}
return BenchState::referenceHash(m_job.algorithm(), m_job.benchSize(), m_threads);
}
void xmrig::BenchClient::printExit()
{
LOG_INFO("%s " WHITE_BOLD("press ") MAGENTA_BOLD("Ctrl+C") WHITE_BOLD(" to exit"), Tags::bench());
}
void xmrig::BenchClient::start()
{
m_listener->onLoginSuccess(this);
@ -180,7 +262,8 @@ void xmrig::BenchClient::startBench(const rapidjson::Value &value)
{
m_job.setId(Json::getString(value, BenchConfig::kId));
m_job.setSeedHash(Json::getString(value, BenchConfig::kSeed));
m_job.setBenchToken(Json::getString(value, BenchConfig::kToken));
m_token = Json::getString(value, BenchConfig::kToken);
start();
}
@ -190,7 +273,7 @@ void xmrig::BenchClient::startVerify(const rapidjson::Value &value)
{
const char *hash = Json::getString(value, BenchConfig::kHash);
if (hash) {
m_job.setBenchHash(strtoull(hash, nullptr, 16));
m_hash = strtoull(hash, nullptr, 16);
}
m_job.setAlgorithm(Json::getString(value, BenchConfig::kAlgo));
@ -199,4 +282,15 @@ void xmrig::BenchClient::startVerify(const rapidjson::Value &value)
start();
}
void xmrig::BenchClient::update(const rapidjson::Value &body)
{
assert(!m_token.isEmpty());
FetchRequest req(HTTP_PATCH, BenchConfig::kApiHost, BenchConfig::kApiPort, fmt::format("/1/benchmark/{}", m_job.id()).c_str(), body, BenchConfig::kApiTLS, true);
req.headers.insert({ "Authorization", fmt::format("Bearer {}", m_token)});
fetch(std::move(req), m_httpListener);
}
#endif

View file

@ -20,20 +20,21 @@
#define XMRIG_BENCHCLIENT_H
#include "base/net/stratum/Client.h"
#include "backend/common/interfaces/IBenchListener.h"
#include "base/kernel/interfaces/IHttpListener.h"
#include "base/net/stratum/Client.h"
namespace xmrig {
class BenchClient : public IClient, public IHttpListener
class BenchClient : public IClient, public IHttpListener, public IBenchListener
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(BenchClient)
BenchClient(const std::shared_ptr<BenchConfig> &benchmark, IClientListener* listener);
~BenchClient() override = default;
~BenchClient() override;
inline bool disconnect() override { return true; }
inline bool hasExtension(Extension) const noexcept override { return false; }
@ -52,7 +53,7 @@ public:
inline int64_t sequence() const override { return 0; }
inline int64_t submit(const JobResult &) override { return 0; }
inline void connect(const Pool &pool) override { setPool(pool); }
inline void deleteLater() override {}
inline void deleteLater() override { delete this; }
inline void setAlgo(const Algorithm &algo) override {}
inline void setEnabled(bool enabled) override {}
inline void setProxy(const ProxyUrl &proxy) override {}
@ -65,6 +66,8 @@ public:
void setPool(const Pool &pool) override;
protected:
void onBenchDone(uint64_t result, uint64_t ts) override;
void onBenchStart(uint64_t ts, uint32_t threads, const IBackend *backend) override;
void onHttpData(const HttpData &data) override;
private:
@ -75,6 +78,8 @@ private:
ONLINE_VERIFY
};
uint64_t referenceHash() const;
void printExit();
void start();
# ifdef XMRIG_FEATURE_HTTP
@ -84,15 +89,22 @@ private:
void setError(const char *message);
void startBench(const rapidjson::Value &value);
void startVerify(const rapidjson::Value &value);
void update(const rapidjson::Value &body);
# endif
const IBackend *m_backend = nullptr;
IClientListener* m_listener;
Job m_job;
Mode m_mode = STATIC_BENCH;
Pool m_pool;
std::shared_ptr<BenchConfig> m_benchmark;
std::shared_ptr<IHttpListener> m_httpListener;
String m_ip;
Mode m_mode = STATIC_BENCH;
String m_token;
uint32_t m_threads = 0;
uint64_t m_doneTime = 0;
uint64_t m_hash = 0;
uint64_t m_startTime = 0;
};