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 #else
29 #include <getopt.h>
30 #include <unistd.h>
31 #endif
32
33 #include "disk_interface.h"
34 #include "graph.h"
35 #include "manifest_parser.h"
36 #include "metrics.h"
37 #include "state.h"
38 #include "util.h"
39
WriteFakeManifests(const string & dir,string * err)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.
44 return mtime != -1;
45
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());
49 printf("done.\n");
50 if (exit_code != 0)
51 *err = "Failed to run " + command;
52 return exit_code == 0;
53 }
54
LoadManifests(bool measure_command_evaluation)55 int LoadManifests(bool measure_command_evaluation) {
56 string err;
57 RealDiskInterface disk_interface;
58 State state;
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());
62 exit(1);
63 }
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;
72 }
73
main(int argc,char * argv[])74 int main(int argc, char* argv[]) {
75 bool measure_command_evaluation = true;
76 int opt;
77 while ((opt = getopt(argc, argv, const_cast<char*>("fh"))) != -1) {
78 switch (opt) {
79 case 'f':
80 measure_command_evaluation = false;
81 break;
82 case 'h':
83 default:
84 printf("usage: manifest_parser_perftest\n"
85 "\n"
86 "options:\n"
87 " -f only measure manifest load time, not command evaluation time\n"
88 );
89 return 1;
90 }
91 }
92
93 const char kManifestDir[] = "build/manifest_perftest";
94
95 string err;
96 if (!WriteFakeManifests(kManifestDir, &err)) {
97 fprintf(stderr, "Failed to write test data: %s\n", err.c_str());
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 }
119