diff --git a/CMakeLists.txt b/CMakeLists.txt index 4bf145c..bc008ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,15 @@ if(NOT WIN32 AND NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS Debug Release MinSizeRel RelWithDebInfo) endif() +include(FetchContent) +FetchContent_Declare( + glaze + GIT_REPOSITORY https://github.com/stephenberry/glaze.git + GIT_TAG main + GIT_SHALLOW TRUE +) +FetchContent_MakeAvailable(glaze) + set(MASTER_PROJECT OFF) if(${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR}) set(MASTER_PROJECT ON) diff --git a/libtermbench/CMakeLists.txt b/libtermbench/CMakeLists.txt index f34c81e..f0ddc75 100644 --- a/libtermbench/CMakeLists.txt +++ b/libtermbench/CMakeLists.txt @@ -25,6 +25,7 @@ set_target_properties(termbench PROPERTIES target_include_directories(termbench PUBLIC $ $) +target_link_libraries(termbench PUBLIC glaze::glaze) install(TARGETS termbench EXPORT termbench-targets diff --git a/libtermbench/termbench.cpp b/libtermbench/termbench.cpp index 7adc434..9f30fdc 100644 --- a/libtermbench/termbench.cpp +++ b/libtermbench/termbench.cpp @@ -32,20 +32,6 @@ namespace termbench #define leadingZeroBits(x) __builtin_clz(v) #endif -namespace -{ - std::string sizeStr(double _value) - { - if ((long double) (_value) >= (1024ull * 1024ull * 1024ull)) // GB - return std::format("{:7.3f} GB", _value / 1024.0 / 1024.0 / 1024.0); - if (_value >= (1024 * 1024)) // MB - return std::format("{:7.3f} MB", _value / 1024.0 / 1024.0); - if (_value >= 1024) // KB - return std::format("{:7.3f} KB", _value / 1024.0); - return std::format("{:7.3f} bytes", _value); - } -} // namespace - using u16 = unsigned short; Benchmark::Benchmark(std::function _writer, @@ -117,6 +103,13 @@ void Benchmark::runAll() } } // + +void Benchmark::summarizeToJson(std::ostream& os) +{ + std::string buffer = glz::write_json(results_); + os << buffer; +} + void Benchmark::summarize(std::ostream& os) { os << std::format("All {} tests finished.\n", results_.size()); diff --git a/libtermbench/termbench.h b/libtermbench/termbench.h index ba7506e..1af9fdd 100644 --- a/libtermbench/termbench.h +++ b/libtermbench/termbench.h @@ -21,6 +21,8 @@ #include #include +#include + namespace termbench { @@ -95,6 +97,7 @@ class Benchmark void runAll(); void summarize(std::ostream& os); + void summarizeToJson(std::ostream& os); std::vector const& results() const noexcept { return results_; } @@ -114,8 +117,41 @@ class Benchmark std::vector results_; }; +inline std::string sizeStr(double _value) +{ + if ((long double) (_value) >= (1024ull * 1024ull * 1024ull)) // GB + return std::format("{:7.3f} GB", _value / 1024.0 / 1024.0 / 1024.0); + if (_value >= (1024 * 1024)) // MB + return std::format("{:7.3f} MB", _value / 1024.0 / 1024.0); + if (_value >= 1024) // KB + return std::format("{:7.3f} KB", _value / 1024.0); + return std::format("{:7.3f} bytes", _value); +} + } // namespace termbench +namespace glz +{ + +template <> +struct meta +{ + using T = termbench::Result; + static constexpr auto value = glz::object( + "name", + [](T const& result) { return result.test.get().name; }, + "bytes written", + &T::bytesWritten, + "time", + [](T const& result) { return result.time.count(); }, + "MB/s", + [](T const& result) { + double bytesPerSecond = double(result.bytesWritten) / (double(result.time.count()) / 1000.0); + return bytesPerSecond / 1024.0 / 1024.0; + }); +}; +} // namespace glz + // Holds a set of pre-defined terminal benchmark tests. namespace termbench::tests { diff --git a/scripts/plot_results.jl b/scripts/plot_results.jl index 134ce42..6088719 100644 --- a/scripts/plot_results.jl +++ b/scripts/plot_results.jl @@ -1,6 +1,10 @@ using Pkg Pkg.add("CairoMakie") +Pkg.add("JSON") using CairoMakie +using JSON + + function parse_line(line) if count("MB",line) > 0 @@ -10,41 +14,45 @@ function parse_line(line) end end -function get_data(data, data_type) +function get_data(file_name) + return JSON.parsefile(file_name) +end + +function get_values(data, data_type) if data_type == :ascii - name = "chars per line:" + name = "chars per line" elseif data_type == :sgr - name = "chars with sgr per line:" + name = "chars with sgr per line" elseif data_type == :sgr_bg - name = "chars with sgr and bg per line:" + name = "chars with sgr and bg per line" elseif data_type == :unicode - name = "unicode simple:" + name = "unicode simple" elseif data_type == :diacritic - name = "unicode diacritic:" + name = "unicode diacritic" elseif data_type == :diacritic_double - name = "unicode double diacritic:" + name = "unicode double diacritic" elseif data_type == :fire - name = "unicode fire:" + name = "unicode fire" elseif data_type == :fire_text - name = "unicode fire as text:" + name = "unicode fire as text" elseif data_type == :flag - name = "unicode flag:" + name = "unicode flag" end - lines = [] + lines = Vector{Float64}() for el in data - if occursin(name, el) - push!(lines, el) + if occursin(name, el["name"]) + push!(lines, el["MB/s"]) end end - return [ parse_line(val) for val in lines] + return lines end function insert_from_data(ax, file_name, marker_style, type) - data = split(open(io->read(io, String), file_name), '\n') + data = get_data(file_name) terminal_name = split(file_name,"_")[1] - data_speed = get_data(data,type) + data_speed = get_values(data,type) scatter!(ax, data_speed, label= terminal_name * "_ascii", marker = marker_style, markersize = 8) end @@ -57,13 +65,12 @@ function generate_for_terminal(file_name, prefix="results_") fig = Figure() ax = Axis(fig[1,1], title = "Results for "*terminal_name ,xlabel = "Length of line", ylabel = "throughput, MB/s") - data = split(open(io->read(io, String), file_name), '\n') - terminal_name = split(file_name,"_")[1] + data = get_data(file_name) markers = [:circle :rect :cross :star4 :start5 :star6 :diamond] - get_data_l = (type) -> get_data(data,type) - speed = [ get_data_l(t) for t in types] + get_values_l = (type) -> get_values(data,type) + speed = [ get_values_l(t) for t in types] marker_size = 8 for (ind,dat) in enumerate(speed) @@ -94,6 +101,7 @@ function generate_comparison(type) end end + types = [:ascii :sgr :sgr_bg ] generate_for_terminal("contour_results") generate_for_terminal("alacritty_results") diff --git a/tb/main.cpp b/tb/main.cpp index c8b1184..46c7f75 100644 --- a/tb/main.cpp +++ b/tb/main.cpp @@ -334,7 +334,7 @@ int main(int argc, char const* argv[]) cout << "Writing summary into " << settings.fileout << std::endl; std::ofstream writerToFile; writerToFile.open(settings.fileout); - tb.summarize(writerToFile); + tb.summarizeToJson(writerToFile); } #if defined(_WIN32)