Ninja
manifest_parser_perftest.cc
Go to the documentation of this file.
1 // Copyright 2014 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Tests manifest parser performance. Expects to be run in ninja's root
16 // directory.
17 
18 #include <numeric>
19 
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #ifdef _WIN32
26 #include "getopt.h"
27 #include <direct.h>
28 #elif defined(_AIX)
29 #include "getopt.h"
30 #include <unistd.h>
31 #else
32 #include <getopt.h>
33 #include <unistd.h>
34 #endif
35 
36 #include "disk_interface.h"
37 #include "graph.h"
38 #include "manifest_parser.h"
39 #include "metrics.h"
40 #include "state.h"
41 #include "util.h"
42 
43 using namespace std;
44 
45 bool WriteFakeManifests(const string& dir, string* err) {
46  RealDiskInterface disk_interface;
47  TimeStamp mtime = disk_interface.Stat(dir + "/build.ninja", err);
48  if (mtime != 0) // 0 means that the file doesn't exist yet.
49  return mtime != -1;
50 
51  string command = "python misc/write_fake_manifests.py " + dir;
52  printf("Creating manifest data..."); fflush(stdout);
53  int exit_code = system(command.c_str());
54  printf("done.\n");
55  if (exit_code != 0)
56  *err = "Failed to run " + command;
57  return exit_code == 0;
58 }
59 
60 int LoadManifests(bool measure_command_evaluation) {
61  string err;
62  RealDiskInterface disk_interface;
63  State state;
64  ManifestParser parser(&state, &disk_interface);
65  if (!parser.Load("build.ninja", &err)) {
66  fprintf(stderr, "Failed to read test data: %s\n", err.c_str());
67  exit(1);
68  }
69  // Doing an empty build involves reading the manifest and evaluating all
70  // commands required for the requested targets. So include command
71  // evaluation in the perftest by default.
72  int optimization_guard = 0;
73  if (measure_command_evaluation)
74  for (size_t i = 0; i < state.edges_.size(); ++i)
75  optimization_guard += state.edges_[i]->EvaluateCommand().size();
76  return optimization_guard;
77 }
78 
79 int main(int argc, char* argv[]) {
80  bool measure_command_evaluation = true;
81  int opt;
82  while ((opt = getopt(argc, argv, const_cast<char*>("fh"))) != -1) {
83  switch (opt) {
84  case 'f':
85  measure_command_evaluation = false;
86  break;
87  case 'h':
88  default:
89  printf("usage: manifest_parser_perftest\n"
90 "\n"
91 "options:\n"
92 " -f only measure manifest load time, not command evaluation time\n"
93  );
94  return 1;
95  }
96  }
97 
98  const char kManifestDir[] = "build/manifest_perftest";
99 
100  string err;
101  if (!WriteFakeManifests(kManifestDir, &err)) {
102  fprintf(stderr, "Failed to write test data: %s\n", err.c_str());
103  return 1;
104  }
105 
106  if (chdir(kManifestDir) < 0)
107  Fatal("chdir: %s", strerror(errno));
108 
109  const int kNumRepetitions = 5;
110  vector<int> times;
111  for (int i = 0; i < kNumRepetitions; ++i) {
112  int64_t start = GetTimeMillis();
113  int optimization_guard = LoadManifests(measure_command_evaluation);
114  int delta = (int)(GetTimeMillis() - start);
115  printf("%dms (hash: %x)\n", delta, optimization_guard);
116  times.push_back(delta);
117  }
118 
119  int min = *min_element(times.begin(), times.end());
120  int max = *max_element(times.begin(), times.end());
121  float total = accumulate(times.begin(), times.end(), 0.0f);
122  printf("min %dms max %dms avg %.1fms\n", min, max, total / times.size());
123 }
int getopt(int argc, char **argv, char *optstring)
int main(int argc, char *argv[])
int LoadManifests(bool measure_command_evaluation)
bool WriteFakeManifests(const string &dir, string *err)
int64_t GetTimeMillis()
Get the current time as relative to some epoch.
Definition: metrics.cc:111
Definition: hash_map.h:26
Parses .ninja files.
bool Load(const std::string &filename, std::string *err, Lexer *parent=NULL)
Load and parse a file.
Definition: parser.cc:22
Implementation of DiskInterface that actually hits the disk.
TimeStamp Stat(const std::string &path, std::string *err) const override
stat() a file, returning the mtime, or 0 if missing and -1 on other errors.
Global state (file status) for a single run.
Definition: state.h:95
std::vector< Edge * > edges_
All the edges of the graph.
Definition: state.h:138
int64_t TimeStamp
Definition: timestamp.h:31
void Fatal(const char *msg,...)
Log a fatal message and exit.
Definition: util.cc:67
signed long long int64_t
A 64-bit integer type.
Definition: win32port.h:28