1 // Copyright 2014 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 // Tests manifest parser performance. Expects to be run in ninja's root
33 #include "disk_interface.h"
35 #include "manifest_parser.h"
40 bool WriteFakeManifests(const string& dir, string* err) {
41 RealDiskInterface disk_interface;
42 TimeStamp mtime = disk_interface.Stat(dir + "/build.ninja", err);
43 if (mtime != 0) // 0 means that the file doesn't exist yet.
46 string command = "python misc/write_fake_manifests.py " + dir;
47 printf("Creating manifest data..."); fflush(stdout);
48 int exit_code = system(command.c_str());
51 *err = "Failed to run " + command;
52 return exit_code == 0;
55 int LoadManifests(bool measure_command_evaluation) {
57 RealDiskInterface disk_interface;
59 ManifestParser parser(&state, &disk_interface);
60 if (!parser.Load("build.ninja", &err)) {
61 fprintf(stderr, "Failed to read test data: %s\n", err.c_str());
64 // Doing an empty build involves reading the manifest and evaluating all
65 // commands required for the requested targets. So include command
66 // evaluation in the perftest by default.
67 int optimization_guard = 0;
68 if (measure_command_evaluation)
69 for (size_t i = 0; i < state.edges_.size(); ++i)
70 optimization_guard += state.edges_[i]->EvaluateCommand().size();
71 return optimization_guard;
74 int main(int argc, char* argv[]) {
75 bool measure_command_evaluation = true;
77 while ((opt = getopt(argc, argv, const_cast<char*>("fh"))) != -1) {
80 measure_command_evaluation = false;
84 printf("usage: manifest_parser_perftest\n"
87 " -f only measure manifest load time, not command evaluation time\n"
93 const char kManifestDir[] = "build/manifest_perftest";
96 if (!WriteFakeManifests(kManifestDir, &err)) {
97 fprintf(stderr, "Failed to write test data: %s\n", err.c_str());
101 if (chdir(kManifestDir) < 0)
102 Fatal("chdir: %s", strerror(errno));
104 const int kNumRepetitions = 5;
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);
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());