From e5829a6b7f846bf25f5600efb5089c2488e5a5a4 Mon Sep 17 00:00:00 2001 From: Marcin Copik Date: Sun, 10 Nov 2024 14:50:03 +0000 Subject: [PATCH] Add sticky benchmarker --- benchmarks/CMakeLists.txt | 6 + .../benchmarker_editing_sticky.cpp | 230 ++++++++++++++++++ 2 files changed, 236 insertions(+) create mode 100644 benchmarks/latex-service/benchmarker_editing_sticky.cpp diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt index 5fe94bb..92aef31 100644 --- a/benchmarks/CMakeLists.txt +++ b/benchmarks/CMakeLists.txt @@ -7,6 +7,12 @@ target_link_libraries(benchmarker_latex_editing PUBLIC cereal::cereal) target_link_libraries(benchmarker_latex_editing PRIVATE praas_sdk) set_target_properties(benchmarker_latex_editing PROPERTIES RUNTIME_OUTPUT_DIRECTORY latex-service) +add_executable(benchmarker_latex_editing_sticky latex-service/benchmarker_editing_sticky.cpp) +target_link_libraries(benchmarker_latex_editing_sticky PUBLIC spdlog::spdlog) +target_link_libraries(benchmarker_latex_editing_sticky PUBLIC cereal::cereal) +target_link_libraries(benchmarker_latex_editing_sticky PRIVATE praas_sdk) +set_target_properties(benchmarker_latex_editing_sticky PROPERTIES RUNTIME_OUTPUT_DIRECTORY latex-service) + add_executable(benchmarker_latex_compiling latex-service/benchmarker_compiling.cpp) target_link_libraries(benchmarker_latex_compiling PUBLIC spdlog::spdlog) target_link_libraries(benchmarker_latex_compiling PUBLIC cereal::cereal) diff --git a/benchmarks/latex-service/benchmarker_editing_sticky.cpp b/benchmarks/latex-service/benchmarker_editing_sticky.cpp new file mode 100644 index 0000000..aa4783d --- /dev/null +++ b/benchmarks/latex-service/benchmarker_editing_sticky.cpp @@ -0,0 +1,230 @@ +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Base64.h" + +struct Config { + std::string inputs_directory; + std::vector input_files; + int repetitions; + + std::string resources_cpu; + std::string resources_memory; + + std::string control_plane_address; + + std::string output_file; + std::string cloud_resource_name; + + template + void serialize(Ar& ar) + { + ar(CEREAL_NVP(inputs_directory)); + ar(CEREAL_NVP(input_files)); + ar(CEREAL_NVP(repetitions)); + ar(CEREAL_NVP(resources_cpu)); + ar(CEREAL_NVP(resources_memory)); + + ar(CEREAL_NVP(control_plane_address)); + ar(CEREAL_NVP(output_file)); + ar(CEREAL_NVP(cloud_resource_name)); + } +}; + +struct File { + + std::string path; + std::string file; + std::string data; + + template + void save(Ar& archive) const + { + archive(CEREAL_NVP(path)); + archive(CEREAL_NVP(file)); + archive(CEREAL_NVP(data)); + } + + template + void load(Ar& archive) + { + archive(CEREAL_NVP(path)); + archive(CEREAL_NVP(file)); + archive(CEREAL_NVP(data)); + } +}; + +struct Result { + std::string message; + + template + void save(Ar& archive) const + { + archive(CEREAL_NVP(message)); + } + + template + void load(Ar& archive) + { + archive(CEREAL_NVP(message)); + } +}; + +std::string generate_input_json(File& file) +{ + std::stringstream output; + { + cereal::JSONOutputArchive archive_out{output}; + file.save(archive_out); + if(!output.good()) { + abort(); + } + } + return output.str(); +} + +int main(int argc, char** argv) +{ + std::string config_file{argv[1]}; + std::ifstream in_stream{config_file}; + if (!in_stream.is_open()) { + spdlog::error("Could not open config file {}", config_file); + exit(1); + } + + Config cfg; + cereal::JSONInputArchive archive_in(in_stream); + cfg.serialize(archive_in); + + spdlog::set_pattern("[%H:%M:%S:%f] [P %P] [T %t] [%l] %v "); + spdlog::info("Executing PraaS benchmarker!"); + + praas::sdk::PraaS praas{fmt::format("http://{}:{}", cfg.control_plane_address, 8000)}; + + praas.create_application("test-latex-editing", cfg.cloud_resource_name); + + //auto proc = praas.create_process( + // "test-latex-editing", "alloc_invoc_process", cfg.resources_cpu, cfg.resources_memory + //); + //if (!proc.has_value() || !proc->connect()) { + // abort(); + //} + + std::vector> measurements; + + std::optional process_id = std::nullopt; + + int j = 0; + for (std::string file : cfg.input_files) { + + spdlog::info("Begin file {}", file); + + File input_file; + input_file.file = std::filesystem::path{file}.filename(); + input_file.path = std::filesystem::path{file}.parent_path(); + + std::ifstream input{std::filesystem::path{cfg.inputs_directory} / file}; + if(input.fail()) { + spdlog::error("Failed to open the file!"); + abort(); + } + std::stringstream buffer; + buffer << input.rdbuf(); + input_file.data = buffer.str(); + + if (std::filesystem::path{file}.extension() == ".pdf" || + std::filesystem::path{file}.extension() == ".png") { + input_file.data = macaron::Base64::Encode(input_file.data); + } + auto data = generate_input_json(input_file); + + for (int i = 0; i < cfg.repetitions + 1; ++i) { + + auto begin = std::chrono::high_resolution_clock::now(); + auto invoc = praas.invoke("test-latex-editing", "update-file", data, process_id, cfg.resources_cpu, cfg.resources_memory); + auto end = std::chrono::high_resolution_clock::now(); + if (invoc.return_code != 0) { + std::cerr << invoc.return_code << " " << invoc.error_message << " " << file << " " << i << std::endl; + abort(); + } + if (i > 0) { + measurements.emplace_back( + "update-file", file, i, data.length(), invoc.response.length(), + std::chrono::duration_cast(end - begin).count() + ); + } + { + std::ofstream output{fmt::format("output_update_file_{}_praas_{}", j, i)}; + output << invoc.response; + } + + if(!process_id.has_value()) + process_id = invoc.process_name; + } + + ++j; + } + + j = 0; + for (std::string file : cfg.input_files) { + + spdlog::info("Begin file {}", file); + + File input_file; + input_file.file = std::filesystem::path{file}.filename(); + input_file.path = std::filesystem::path{file}.parent_path(); + input_file.data = ""; + auto data = generate_input_json(input_file); + + for (int i = 0; i < cfg.repetitions + 1; ++i) { + + auto begin = std::chrono::high_resolution_clock::now(); + auto invoc = praas.invoke("test-latex-editing", "get-file", data, process_id, cfg.resources_cpu, cfg.resources_memory); + auto end = std::chrono::high_resolution_clock::now(); + if (invoc.return_code != 0) { + std::cerr << invoc.return_code << " " << file << " " << i << std::endl; + abort(); + } + if (i > 0) { + measurements.emplace_back( + "get-file", file, i, data.length(), invoc.response.length(), + std::chrono::duration_cast(end - begin).count() + ); + } + { + std::ofstream output{fmt::format("output_get_file_{}_praas_{}", j, i)}; + output << invoc.response; + } + } + + ++j; + } + //std::cerr << measurements.size() << std::endl; + + std::ofstream out_file{cfg.output_file, std::ios::out}; + out_file << "type,input,repetition,input-size,output-size,time" << '\n'; + + for (auto& [type, input, rep, input_size, output, time] : measurements) { + out_file << type << "," << input << "," << rep << "," << input_size << "," << output << "," + << time << '\n'; + } + + out_file.close(); + + return 0; +}