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 <string.h>
23 
24 #ifdef _WIN32
25 #include "getopt.h"
26 #include <direct.h>
27 #else
28 #include <getopt.h>
29 #include <unistd.h>
30 #endif
31 
32 #include "disk_interface.h"
33 #include "graph.h"
34 #include "manifest_parser.h"
35 #include "metrics.h"
36 #include "state.h"
37 #include "util.h"
38 
39 struct RealFileReader : public ManifestParser::FileReader {
40  virtual bool ReadFile(const string& path, string* content, string* err) {
41  return ::ReadFile(path, content, err) == 0;
42  }
43 };
44 
45 bool WriteFakeManifests(const string& dir) {
46  RealDiskInterface disk_interface;
47  if (disk_interface.Stat(dir + "/build.ninja") > 0)
48  return true;
49 
50  printf("Creating manifest data..."); fflush(stdout);
51  int err = system(("python misc/write_fake_manifests.py " + dir).c_str());
52  printf("done.\n");
53  return err == 0;
54 }
55 
56 int LoadManifests(bool measure_command_evaluation) {
57  string err;
58  RealFileReader file_reader;
59  State state;
60  ManifestParser parser(&state, &file_reader);
61  if (!parser.Load("build.ninja", &err)) {
62  fprintf(stderr, "Failed to read test data: %s\n", err.c_str());
63  exit(1);
64  }
65  // Doing an empty build involves reading the manifest and evaluating all
66  // commands required for the requested targets. So include command
67  // evaluation in the perftest by default.
68  int optimization_guard = 0;
69  if (measure_command_evaluation)
70  for (size_t i = 0; i < state.edges_.size(); ++i)
71  optimization_guard += state.edges_[i]->EvaluateCommand().size();
72  return optimization_guard;
73 }
74 
75 int main(int argc, char* argv[]) {
76  bool measure_command_evaluation = true;
77  int opt;
78  while ((opt = getopt(argc, argv, const_cast<char*>("fh"))) != -1) {
79  switch (opt) {
80  case 'f':
81  measure_command_evaluation = false;
82  break;
83  case 'h':
84  default:
85  printf("usage: manifest_parser_perftest\n"
86 "\n"
87 "options:\n"
88 " -f only measure manifest load time, not command evaluation time\n"
89  );
90  return 1;
91  }
92  }
93 
94  const char kManifestDir[] = "build/manifest_perftest";
95 
96  if (!WriteFakeManifests(kManifestDir)) {
97  fprintf(stderr, "Failed to write test data\n");
98  return 1;
99  }
100 
101  if (chdir(kManifestDir) < 0)
102  Fatal("chdir: %s", strerror(errno));
103 
104  const int kNumRepetitions = 5;
105  vector<int> times;
106  for (int i = 0; i < kNumRepetitions; ++i) {
107  int64_t start = GetTimeMillis();
108  int optimization_guard = LoadManifests(measure_command_evaluation);
109  int delta = (int)(GetTimeMillis() - start);
110  printf("%dms (hash: %x)\n", delta, optimization_guard);
111  times.push_back(delta);
112  }
113 
114  int min = *min_element(times.begin(), times.end());
115  int max = *max_element(times.begin(), times.end());
116  float total = accumulate(times.begin(), times.end(), 0.0f);
117  printf("min %dms max %dms avg %.1fms\n", min, max, total / times.size());
118 }
bool Load(const string &filename, string *err, Lexer *parent=NULL)
Load and parse a file.
vector< Edge * > edges_
All the edges of the graph.
Definition: state.h:129
int64_t GetTimeMillis()
Get the current time as relative to some epoch.
Definition: metrics.cc:124
virtual TimeStamp Stat(const string &path) const
stat() a file, returning the mtime, or 0 if missing and -1 on other errors.
signed long long int64_t
A 64-bit integer type.
Definition: win32port.h:21
Parses .ninja files.
Implementation of DiskInterface that actually hits the disk.
int getopt(int argc, char **argv, char *optstring)
int LoadManifests(bool measure_command_evaluation)
int ReadFile(const string &path, string *contents, string *err)
Read a file to a string (in text mode: with CRLF conversion on Windows).
Definition: util.cc:282
int main(int argc, char *argv[])
virtual bool ReadFile(const string &path, string *content, string *err)
void Fatal(const char *msg,...)
Log a fatal message and exit.
Definition: util.cc:52
Global state (file status, loaded rules) for a single run.
Definition: state.h:83
bool WriteFakeManifests(const string &dir)