File: json_perftest_decodebench.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (100 lines) | stat: -rw-r--r-- 3,349 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This program measures the time taken to decode the given JSON files (the
// command line arguments). It is for manual benchmarking.
//
// Usage:
// $ ninja -C out/foobar json_perftest_decodebench
// $ out/foobar/json_perftest_decodebench -a -n=10 the/path/to/your/*.json
//
// The -n=10 switch controls the number of iterations. It defaults to 1.
//
// The -a switch means to print 1 non-comment line per input file (the average
// iteration time). Without this switch (the default), it prints n non-comment
// lines per input file (individual iteration times). For a single input file,
// building and running this program before and after a particular commit can
// work well with the 'ministat' tool: https://github.com/thorduri/ministat

#include <inttypes.h>
#include <iomanip>
#include <iostream>

#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/time/time.h"

int main(int argc, char* argv[]) {
  if (!base::ThreadTicks::IsSupported()) {
    std::cout << "# base::ThreadTicks is not supported\n";
    return EXIT_FAILURE;
  }
  base::ThreadTicks::WaitUntilInitialized();

  base::CommandLine::Init(argc, argv);
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  bool average = command_line->HasSwitch("a");
  int iterations = 1;
  std::string iterations_str = command_line->GetSwitchValueASCII("n");
  if (!iterations_str.empty()) {
    iterations = atoi(iterations_str.c_str());
    if (iterations < 1) {
      std::cout << "# invalid -n command line switch\n";
      return EXIT_FAILURE;
    }
  }

  if (average) {
    std::cout << "# Microseconds (μs), n=" << iterations << ", averaged"
              << std::endl;
  } else {
    std::cout << "# Microseconds (μs), n=" << iterations << std::endl;
  }
  for (const auto& filename : command_line->GetArgs()) {
    std::string src;
    if (!base::ReadFileToString(base::FilePath(filename), &src)) {
      std::cout << "# could not read " << filename << std::endl;
      return EXIT_FAILURE;
    }

    int64_t total_time = 0;
    std::string error_message;
    for (int i = 0; i < iterations; ++i) {
      auto start = base::ThreadTicks::Now();
      auto v = base::JSONReader::ReadAndReturnValueWithError(src);
      auto end = base::ThreadTicks::Now();
      int64_t iteration_time = (end - start).InMicroseconds();
      total_time += iteration_time;

      if (i == 0) {
        if (average) {
          error_message =
              !v.has_value() ? std::move(v.error().message) : std::string();
        } else {
          std::cout << "# " << filename;
          if (!v.has_value() && !v.error().message.empty()) {
            std::cout << ": " << v.error().message;
          }
          std::cout << std::endl;
        }
      }

      if (!average) {
        std::cout << iteration_time << std::endl;
      }
    }

    if (average) {
      int64_t average_time = total_time / iterations;
      std::cout << std::setw(12) << average_time << "\t# " << filename;
      if (!error_message.empty()) {
        std::cout << ": " << error_message;
      }
      std::cout << std::endl;
    }
  }
  return EXIT_SUCCESS;
}