Write displayed fps values to disk.

This commit is contained in:
Pentarctagon 2021-04-27 23:51:12 -05:00 committed by Pentarctagon
parent afded0db32
commit bc5878f153
4 changed files with 23 additions and 5 deletions

View file

@ -21,6 +21,7 @@
#include "cursor.hpp"
#include "display.hpp"
#include "fake_unit_manager.hpp"
#include "filesystem.hpp"
#include "font/sdl_ttf_compat.hpp"
#include "font/text.hpp"
#include "preferences/game.hpp"
@ -79,6 +80,7 @@ static lg::log_domain log_display("display");
#define MaxZoom (zoom_levels.back())
namespace {
// if this is enabled with :benchmark, then everything is marked as invalid and redrawn each time
bool benchmark = false;
bool debug_foreground = false;
@ -1368,14 +1370,26 @@ void display::update_display()
static int frames = 0;
++frames;
const int sample_freq = 10;
if(frames == sample_freq) {
const auto minmax_it = std::minmax_element(frametimes_.begin(), frametimes_.end());
const unsigned render_avg = std::accumulate(frametimes_.begin(), frametimes_.end(), 0) / frametimes_.size();
const int avg_fps = calculate_fps(render_avg);
const int max_fps = calculate_fps(*minmax_it.first);
const int min_fps = calculate_fps(*minmax_it.second);
fps_history_.emplace_back(min_fps, avg_fps, max_fps);
frames = 0;
// flush out the stored fps values every so often
if(fps_history_.size() == 1000) {
std::string filename = filesystem::get_user_data_dir()+"/fps_log.csv";
filesystem::scoped_ostream fps_log = filesystem::ostream_file(filename, std::ios_base::binary | std::ios_base::app);
for(const auto& fps : fps_history_) {
*fps_log << std::get<0>(fps) << "," << std::get<1>(fps) << "," << std::get<2>(fps) << "\n";
}
fps_history_.clear();
}
if(fps_handle_ != 0) {
font::remove_floating_label(fps_handle_);
fps_handle_ = 0;

View file

@ -51,6 +51,7 @@ namespace wb {
#include "animated.hpp"
#include "display_context.hpp"
#include "filesystem.hpp"
#include "font/standard_colors.hpp"
#include "game_config.hpp"
#include "picture.hpp" //only needed for enums (!)
@ -1071,6 +1072,8 @@ private:
bool dirty_;
std::vector<std::tuple<int, int, int>> fps_history_;
protected:
static display * singleton_;
};

View file

@ -1038,25 +1038,25 @@ filesystem::scoped_istream istream_file(const std::string& fname, bool treat_fai
}
}
filesystem::scoped_ostream ostream_file(const std::string& fname, bool create_directory)
filesystem::scoped_ostream ostream_file(const std::string& fname, std::ios_base::openmode mode, bool create_directory)
{
LOG_FS << "streaming " << fname << " for writing.\n";
#if 1
try {
boost::iostreams::file_descriptor_sink fd(bfs::path(fname), std::ios_base::binary);
boost::iostreams::file_descriptor_sink fd(bfs::path(fname), mode);
return std::make_unique<boost::iostreams::stream<boost::iostreams::file_descriptor_sink>>(fd, 4096, 0);
} catch(const BOOST_IOSTREAMS_FAILURE& e) {
// If this operation failed because the parent directory didn't exist, create the parent directory and
// retry.
error_code ec_unused;
if(create_directory && bfs::create_directories(bfs::path(fname).parent_path(), ec_unused)) {
return ostream_file(fname, false);
return ostream_file(fname, mode, false);
}
throw filesystem::io_exception(e.what());
}
#else
return new bfs::ofstream(bfs::path(fname), std::ios_base::binary);
return new bfs::ofstream(bfs::path(fname), mode);
#endif
}

View file

@ -20,6 +20,7 @@
#pragma once
#include <ctime>
#include <fstream>
#include <iosfwd>
#include <memory>
#include <string>
@ -204,7 +205,7 @@ bool looks_like_pbl(const std::string& file);
/** Basic disk I/O - read file. */
std::string read_file(const std::string& fname);
filesystem::scoped_istream istream_file(const std::string& fname, bool treat_failure_as_error = true);
filesystem::scoped_ostream ostream_file(const std::string& fname, bool create_directory = true);
filesystem::scoped_ostream ostream_file(const std::string& fname, std::ios_base::openmode mode = std::ios_base::binary, bool create_directory = true);
/** Throws io_exception if an error occurs. */
void write_file(const std::string& fname, const std::string& data);