如何在Google基准中强制黑白输出

How to force black and white output in google benchmark

本文关键字:黑白 输出 基准 Google      更新时间:2023-10-16

我正在Xcode中使用Google基准测试,并且由于某种原因而生产彩色输出。由于Xcode似乎不支持彩色输出,因此我看到了不希望的符号。我想知道是否有可能在Google基准中强制黑白输出。我更喜欢使用他们的API的答案,但我对其他替代方案开放。

读书中提到了Google基准的颜色输出:https://github.com/google/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/a271c36af93c7a3b19dfeb2aefaefa9ca77a58e52e4/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建议(使用包装板脚本)的建议使用--benchmark_color=false,或尝试使用"无用的CAT模式"传递控制台输出:

 ./program | cat

它应该设置stdout,以使isatty()在自动彩色默认模式下检查(此技巧可以禁用颜色grep)。

或使用export TERM=ansi更改您的术语Env变量,以指示您的绿色和黑色CRT显示器:https://github.com/google/benchmark/blob/09b93ccc6a9aeed84c269b6f5b8130c878e518ebbb/src/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继续生成颜色。它必须是一个错误(!!!)GetOutputOptions附近,使IsColorTerminal()逻辑在"自动"的情况下为NOOP,因此"自动"模式并不是真正的自动,它始终是启用的:

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认为默认值"自动"为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一起使用,并且可能与某些正确设置术语的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;