OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 26 matching lines...) Expand all Loading... |
37 #include "compiler.h" | 37 #include "compiler.h" |
38 #include "scanner-character-streams.h" | 38 #include "scanner-character-streams.h" |
39 #include "shell-utils.h" | 39 #include "shell-utils.h" |
40 #include "parser.h" | 40 #include "parser.h" |
41 #include "preparse-data-format.h" | 41 #include "preparse-data-format.h" |
42 #include "preparse-data.h" | 42 #include "preparse-data.h" |
43 #include "preparser.h" | 43 #include "preparser.h" |
44 | 44 |
45 using namespace v8::internal; | 45 using namespace v8::internal; |
46 | 46 |
47 enum TestMode { | |
48 PreParseAndParse, | |
49 PreParse, | |
50 Parse | |
51 }; | |
52 | |
53 std::pair<TimeDelta, TimeDelta> RunBaselineParser( | 47 std::pair<TimeDelta, TimeDelta> RunBaselineParser( |
54 const char* fname, Encoding encoding, int repeat, v8::Isolate* isolate, | 48 const char* fname, Encoding encoding, int repeat, v8::Isolate* isolate, |
55 v8::Handle<v8::Context> context, TestMode test_mode) { | 49 v8::Handle<v8::Context> context) { |
56 int length = 0; | 50 int length = 0; |
57 const byte* source = ReadFileAndRepeat(fname, &length, repeat); | 51 const byte* source = ReadFileAndRepeat(fname, &length, repeat); |
58 v8::Handle<v8::String> source_handle; | 52 v8::Handle<v8::String> source_handle; |
59 switch (encoding) { | 53 switch (encoding) { |
60 case UTF8: { | 54 case UTF8: { |
61 source_handle = v8::String::NewFromUtf8( | 55 source_handle = v8::String::NewFromUtf8( |
62 isolate, reinterpret_cast<const char*>(source)); | 56 isolate, reinterpret_cast<const char*>(source)); |
63 break; | 57 break; |
64 } | 58 } |
65 case UTF16: { | 59 case UTF16: { |
66 source_handle = v8::String::NewFromTwoByte( | 60 source_handle = v8::String::NewFromTwoByte( |
67 isolate, reinterpret_cast<const uint16_t*>(source), | 61 isolate, reinterpret_cast<const uint16_t*>(source), |
68 v8::String::kNormalString, length / 2); | 62 v8::String::kNormalString, length / 2); |
69 break; | 63 break; |
70 } | 64 } |
71 case LATIN1: { | 65 case LATIN1: { |
72 source_handle = v8::String::NewFromOneByte(isolate, source); | 66 source_handle = v8::String::NewFromOneByte(isolate, source); |
73 break; | 67 break; |
74 } | 68 } |
75 } | 69 } |
76 v8::ScriptData* cached_data = NULL; | 70 TimeDelta parse_time1, parse_time2; |
77 TimeDelta preparse_time, parse_time; | 71 Handle<Script> script = Isolate::Current()->factory()->NewScript( |
78 if (test_mode == PreParseAndParse || test_mode == PreParse) { | 72 v8::Utils::OpenHandle(*source_handle)); |
| 73 i::ScriptDataImpl* cached_data_impl = NULL; |
| 74 // First round of parsing (produce data to cache). |
| 75 { |
| 76 CompilationInfoWithZone info(script); |
| 77 info.MarkAsGlobal(); |
| 78 info.SetCachedData(&cached_data_impl, i::PRODUCE_CACHED_DATA); |
79 ElapsedTimer timer; | 79 ElapsedTimer timer; |
80 timer.Start(); | 80 timer.Start(); |
81 cached_data = v8::ScriptData::PreCompile(source_handle); | 81 // Allow lazy parsing; otherwise we won't produce cached data. |
82 preparse_time = timer.Elapsed(); | |
83 if (cached_data == NULL || cached_data->HasError()) { | |
84 fprintf(stderr, "Preparsing failed\n"); | |
85 return std::make_pair(TimeDelta(), TimeDelta()); | |
86 } | |
87 } | |
88 if (test_mode == PreParseAndParse || test_mode == Parse) { | |
89 Handle<String> str = v8::Utils::OpenHandle(*source_handle); | |
90 i::Isolate* internal_isolate = str->GetIsolate(); | |
91 Handle<Script> script = internal_isolate->factory()->NewScript(str); | |
92 CompilationInfoWithZone info(script); | |
93 info.MarkAsGlobal(); | |
94 i::ScriptDataImpl* cached_data_impl = | |
95 static_cast<i::ScriptDataImpl*>(cached_data); | |
96 if (test_mode == PreParseAndParse) { | |
97 info.SetCachedData(&cached_data_impl, | |
98 i::CONSUME_CACHED_DATA); | |
99 } | |
100 info.SetContext(v8::Utils::OpenHandle(*context)); | |
101 ElapsedTimer timer; | |
102 timer.Start(); | |
103 // Allow lazy parsing; otherwise the preparse data won't help. | |
104 bool success = Parser::Parse(&info, true); | 82 bool success = Parser::Parse(&info, true); |
105 parse_time = timer.Elapsed(); | 83 parse_time1 = timer.Elapsed(); |
106 if (!success) { | 84 if (!success) { |
107 fprintf(stderr, "Parsing failed\n"); | 85 fprintf(stderr, "Parsing failed\n"); |
108 return std::make_pair(TimeDelta(), TimeDelta()); | 86 return std::make_pair(TimeDelta(), TimeDelta()); |
109 } | 87 } |
110 } | 88 } |
111 return std::make_pair(preparse_time, parse_time); | 89 // Second round of parsing (consume cached data). |
| 90 { |
| 91 CompilationInfoWithZone info(script); |
| 92 info.MarkAsGlobal(); |
| 93 info.SetCachedData(&cached_data_impl, i::CONSUME_CACHED_DATA); |
| 94 ElapsedTimer timer; |
| 95 timer.Start(); |
| 96 // Allow lazy parsing; otherwise cached data won't help. |
| 97 bool success = Parser::Parse(&info, true); |
| 98 parse_time2 = timer.Elapsed(); |
| 99 if (!success) { |
| 100 fprintf(stderr, "Parsing failed\n"); |
| 101 return std::make_pair(TimeDelta(), TimeDelta()); |
| 102 } |
| 103 } |
| 104 return std::make_pair(parse_time1, parse_time2); |
112 } | 105 } |
113 | 106 |
114 | 107 |
115 int main(int argc, char* argv[]) { | 108 int main(int argc, char* argv[]) { |
116 v8::V8::InitializeICU(); | 109 v8::V8::InitializeICU(); |
117 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 110 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
118 Encoding encoding = LATIN1; | 111 Encoding encoding = LATIN1; |
119 TestMode test_mode = PreParseAndParse; | |
120 std::vector<std::string> fnames; | 112 std::vector<std::string> fnames; |
121 std::string benchmark; | 113 std::string benchmark; |
122 int repeat = 1; | 114 int repeat = 1; |
123 for (int i = 0; i < argc; ++i) { | 115 for (int i = 0; i < argc; ++i) { |
124 if (strcmp(argv[i], "--latin1") == 0) { | 116 if (strcmp(argv[i], "--latin1") == 0) { |
125 encoding = LATIN1; | 117 encoding = LATIN1; |
126 } else if (strcmp(argv[i], "--utf8") == 0) { | 118 } else if (strcmp(argv[i], "--utf8") == 0) { |
127 encoding = UTF8; | 119 encoding = UTF8; |
128 } else if (strcmp(argv[i], "--utf16") == 0) { | 120 } else if (strcmp(argv[i], "--utf16") == 0) { |
129 encoding = UTF16; | 121 encoding = UTF16; |
130 } else if (strcmp(argv[i], "--preparse-and-parse") == 0) { | |
131 test_mode = PreParseAndParse; | |
132 } else if (strcmp(argv[i], "--preparse") == 0) { | |
133 test_mode = PreParse; | |
134 } else if (strcmp(argv[i], "--parse") == 0) { | |
135 test_mode = Parse; | |
136 } else if (strncmp(argv[i], "--benchmark=", 12) == 0) { | 122 } else if (strncmp(argv[i], "--benchmark=", 12) == 0) { |
137 benchmark = std::string(argv[i]).substr(12); | 123 benchmark = std::string(argv[i]).substr(12); |
138 } else if (strncmp(argv[i], "--repeat=", 9) == 0) { | 124 } else if (strncmp(argv[i], "--repeat=", 9) == 0) { |
139 std::string repeat_str = std::string(argv[i]).substr(9); | 125 std::string repeat_str = std::string(argv[i]).substr(9); |
140 repeat = atoi(repeat_str.c_str()); | 126 repeat = atoi(repeat_str.c_str()); |
141 } else if (i > 0 && argv[i][0] != '-') { | 127 } else if (i > 0 && argv[i][0] != '-') { |
142 fnames.push_back(std::string(argv[i])); | 128 fnames.push_back(std::string(argv[i])); |
143 } | 129 } |
144 } | 130 } |
145 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 131 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
146 { | 132 { |
147 v8::HandleScope handle_scope(isolate); | 133 v8::HandleScope handle_scope(isolate); |
148 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate); | 134 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate); |
149 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); | 135 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); |
150 ASSERT(!context.IsEmpty()); | 136 ASSERT(!context.IsEmpty()); |
151 { | 137 { |
152 v8::Context::Scope scope(context); | 138 v8::Context::Scope scope(context); |
153 double preparse_total = 0; | 139 double first_parse_total = 0; |
154 double parse_total = 0; | 140 double second_parse_total = 0; |
155 for (size_t i = 0; i < fnames.size(); i++) { | 141 for (size_t i = 0; i < fnames.size(); i++) { |
156 std::pair<TimeDelta, TimeDelta> time = RunBaselineParser( | 142 std::pair<TimeDelta, TimeDelta> time = RunBaselineParser( |
157 fnames[i].c_str(), encoding, repeat, isolate, context, test_mode); | 143 fnames[i].c_str(), encoding, repeat, isolate, context); |
158 preparse_total += time.first.InMillisecondsF(); | 144 first_parse_total += time.first.InMillisecondsF(); |
159 parse_total += time.second.InMillisecondsF(); | 145 second_parse_total += time.second.InMillisecondsF(); |
160 } | 146 } |
161 if (benchmark.empty()) benchmark = "Baseline"; | 147 if (benchmark.empty()) benchmark = "Baseline"; |
162 printf("%s(PreParseRunTime): %.f ms\n", benchmark.c_str(), | 148 printf("%s(FirstParseRunTime): %.f ms\n", benchmark.c_str(), |
163 preparse_total); | 149 first_parse_total); |
164 printf("%s(ParseRunTime): %.f ms\n", benchmark.c_str(), parse_total); | 150 printf("%s(SecondParseRunTime): %.f ms\n", benchmark.c_str(), |
165 printf("%s(RunTime): %.f ms\n", benchmark.c_str(), | 151 second_parse_total); |
166 preparse_total + parse_total); | |
167 } | 152 } |
168 } | 153 } |
169 v8::V8::Dispose(); | 154 v8::V8::Dispose(); |
170 return 0; | 155 return 0; |
171 } | 156 } |
OLD | NEW |