clean-code/VectorFiller/atlas/bootstrap/bootstrap.h

69 lines
2.3 KiB
C++

#pragma once
#include "../collection/decorator/MeasuredVectorFactory.h"
#include "../collection/impl/AutoThreadedVectorFactory.h"
#include "../collection/impl/MultiThreadedVectorFactory.h"
#include "../collection/impl/SingleThreadedVectorFactory.h"
#include "../generator/impl/RandomIntGenerator.h"
#include "../time/impl/HighResolutionTimer.h"
#include <charconv>
#include <limits>
namespace atlas::bootstrap {
class Bootstrap {
public:
auto run(int argc, char* argv[]) const -> int
{
constexpr size_t size { std::numeric_limits<int>::max() / 2 };
if (argc > 1) {
std::size_t i;
std::string_view arg { argv[1] };
if (auto const [ptr, ec] = std::from_chars(arg.data(), arg.data() + arg.size(), i); ec == std::errc {}) {
return benchmark(size, { i });
}
}
return benchmark(size, { 0, 1, 2, 4, 8, 16 });
}
private:
auto benchmark(size_t const size, std::vector<size_t> const& threads) const -> int
{
for (auto const i : threads) {
run(size, i);
}
return 0;
}
auto run(size_t const size, size_t const nthreads) const -> int
{
std::cout << "Running with " << nthreads << " threads\n";
auto factory { make_measured(make_factory(nthreads)) };
auto result { factory->createAndFillVector(size) };
return 0;
}
std::unique_ptr<collection::VectorFactory<int>> make_factory(size_t nthreads) const
{
switch (nthreads) {
case 0:
return std::make_unique<collection::AutoThreadedVectorFactory<int>>([this] { return make_generator(); });
case 1:
return std::make_unique<collection::SingleThreadedVectorFactory<int>>(make_generator());
default:
return std::make_unique<collection::MultiThreadedVectorFactory<int>>([this] { return make_generator(); }, nthreads);
}
}
std::unique_ptr<collection::VectorFactory<int>> make_measured(std::unique_ptr<collection::VectorFactory<int>> other) const
{
return std::make_unique<collection::decorator::MeasuredVectorFactory<int>>(std::make_unique<time::HighResolutionTimer>(), std::move(other));
}
std::unique_ptr<generator::Generator<int>> make_generator() const
{
return std::make_unique<generator::RandomIntGenerator>();
}
};
}