如何在Google Benchmark中强制输出黑白?

6

我正在Xcode中使用Google基准测试,并出现了彩色输出。由于Xcode似乎不支持彩色输出,我看到了一些不想要的符号。我想知道是否可以在Google基准测试中强制使用黑白输出。我更喜欢使用他们的API的答案,但也接受其他替代方案。


3
你尝试过使用参数--benchmark_color={auto|true|false}吗? - Artemy Vysotsky
太好了!这就解决了问题。你知道是否有办法从CMake中指定吗?@ArtemyVysotsky - German Capuano
1
为了应用@Artemy Vysotsky的正确建议,您可以创建一个脚本,使用正确的参数调用基准可执行文件。您甚至可以将该文件提交到您的版本控制中,然后让cmake将该文件复制到您的构建目录中。请参见cmake命令configure_file - snow_abstraction
1
好的,如果有人在回答中写出来,我会接受它并分配赏金。 - German Capuano
1个回答

3
Google Benchmark的输出颜色在他们的Readme中有提到:https://github.com/google/benchmark#output-formats 引用如下:
输出格式
该库支持多种输出格式。使用--benchmark_format=<console|json|csv>标志设置格式类型。控制台是默认格式。
控制台格式旨在成为人类可读格式。默认情况下,该格式生成彩色输出。上下文输出在stderr上,表格数据输出在stdout上。
还有一个选项可以将无颜色副本的输出保存到文件中:--benchmark_out=file --benchmark_out_format=console(默认为json)
该库支持将基准测试的输出写入由--benchmark_out=<filename>指定的文件。可以使用--benchmark_out_format={json|console|csv}指定输出格式。指定--benchmark_out不会抑制控制台输出。
在实现中,他们有禁用控制台着色的标志:

https://github.com/google/benchmark/blob/a271c36af93c7a3b19dfeb2aefa9ca77a58e52e4/src/benchmark.cc#L87

 DEFINE_string(benchmark_color, "auto",
          "Whether to use colors in the output.  Valid values: "
          "'true'/'yes'/1, 'false'/'no'/0, and 'auto'. 'auto' means to use "
          "colors if the output is being sent to a terminal and the TERM "
          "environment variable is set to a terminal type that supports "
          "colors.");

所以,你可以使用Artemy建议的包装shell脚本 --benchmark_color=false,或者尝试通过“无用的cat模式”传递控制台输出:
 ./program | cat

在自动颜色默认模式下,它应该将stdout设置为失败的isatty()检查(这个技巧可用于禁用彩色grep)。

或者使用export TERM=ansi更改您的TERM环境变量,以表明您有绿黑CRT监视器: https://github.com/google/benchmark/blob/09b93ccc6a9aed84c269b6f5b8130c878e518ebb/src/colorprint.cc#L167

  //                 ... This list of
  // supported TERM values is copied from Google Test:
  // <https://github.com/google/googletest/blob/master/googletest/src/gtest.cc#L2925>.
  const char* const SUPPORTED_TERM_VALUES[] = {
      "xterm",         "xterm-color",     "xterm-256color",
      "screen",        "screen-256color", "tmux",
      "tmux-256color", "rxvt-unicode",    "rxvt-unicode-256color",
      "linux",         "cygwin",
};

但是使用|catexport TERM=ansi,gbench继续生成彩色输出。这一定是一个bug (!!!)GetOutputOptions附近,使得IsColorTerminal()逻辑在"auto"模式下无效,因此"auto"模式并不真正自动启用:
ConsoleReporter::OutputOptions GetOutputOptions(bool force_no_color) {
  int output_opts = ConsoleReporter::OO_Defaults;
  if ((FLAGS_benchmark_color == "auto" && IsColorTerminal()) ||
      IsTruthyFlagValue(FLAGS_benchmark_color)) {
    output_opts |= ConsoleReporter::OO_Color;
  } else {
    output_opts &= ~ConsoleReporter::OO_Color;
}

IsTruthyFlagValue将默认值"auto"视为true,并始终启用颜色输出,即使终端不支持它!
bool IsTruthyFlagValue(const std::string& value) {
  if (value.empty()) return true;
  char ch = value[0];
  return isalnum(ch) &&
         !(ch == '0' || ch == 'f' || ch == 'F' || ch == 'n' || ch == 'N');
}   

这是启用自动模式的补丁(可与| catTERM = ansi ./ program 一起使用,并可能适用于正确设置TERM的某些IDE)。随时进行拉取请求:
--- a/src/benchmark.cc
+++ b/src/benchmark.cc
@@ -555,7 +555,8 @@ bool IsZero(double n) {
 ConsoleReporter::OutputOptions GetOutputOptions(bool force_no_color) {
   int output_opts = ConsoleReporter::OO_Defaults;
   if ((FLAGS_benchmark_color == "auto" && IsColorTerminal()) ||
-      IsTruthyFlagValue(FLAGS_benchmark_color)) {
+      (FLAGS_benchmark_color != "auto" &&
+       IsTruthyFlagValue(FLAGS_benchmark_color))) {
     output_opts |= ConsoleReporter::OO_Color;
   } else {
     output_opts &= ~ConsoleReporter::OO_Color;

1
1.2.0版本中由0de985ae9d4ea1582125a5414a99ca90c368ca10引入的错误 https://github.com/google/benchmark/commit/0de985ae9d4ea1582125a5414a99ca90c368ca10提交(biojppm Joao Paulo Magalhaes于2017年3月2日提交)-旧逻辑是仅在颜色选项不为auto时使用IsTruthyFlagValue。https://github.com/google/benchmark/commit/0de985ae9d4ea1582125a5414a99ca90c368ca10#diff-5cd986c2288650e5c5d336b92dcfafd1L549 - osgx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接