| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 // SharedOptions=-Dsend_stats=true | |
| 5 | |
| 6 /// Tests that we compute send metrics correctly in many simple scenarios. | |
| 7 library dart2js.test.send_measurements_test; | |
| 8 | |
| 9 import 'dart:async'; | |
| 10 import 'package:test/test.dart'; | |
| 11 import 'package:dart2js_info/info.dart'; | |
| 12 import 'package:dart2js_info/src/util.dart' show | |
| 13 recursiveDiagnosticString; | |
| 14 import 'memory_compiler.dart'; | |
| 15 | |
| 16 main() { | |
| 17 test('nothing is reachable, nothing to count', () { | |
| 18 return _check(''' | |
| 19 main() {} | |
| 20 test() { int x = 3; } | |
| 21 '''); | |
| 22 }); | |
| 23 | |
| 24 test('local variable read', () { | |
| 25 return _check(''' | |
| 26 main() => test(); | |
| 27 test() { int x = 3; int y = x; } | |
| 28 ''', | |
| 29 localSend: 1); // from `int y = x`; | |
| 30 }); | |
| 31 | |
| 32 test('generative constructor call', () { | |
| 33 return _check(''' | |
| 34 class A { | |
| 35 get f => 1; | |
| 36 } | |
| 37 main() => test(); | |
| 38 test() { new A(); } | |
| 39 ''', | |
| 40 constructorSend: 1); // from new A() | |
| 41 }); | |
| 42 | |
| 43 group('instance call', () { | |
| 44 test('monomorphic only one implementor', () { | |
| 45 return _check(''' | |
| 46 class A { | |
| 47 get f => 1; | |
| 48 } | |
| 49 main() => test(); | |
| 50 test() { new A().f; } | |
| 51 ''', | |
| 52 constructorSend: 1, // new A() | |
| 53 instanceSend: 1); // f resolved to A.f | |
| 54 }); | |
| 55 | |
| 56 test('monomorphic only one type possible from types', () { | |
| 57 return _check(''' | |
| 58 class A { | |
| 59 get f => 1; | |
| 60 } | |
| 61 class B extends A { | |
| 62 get f => 1; | |
| 63 } | |
| 64 main() => test(); | |
| 65 test() { new B().f; } | |
| 66 ''', | |
| 67 constructorSend: 1, | |
| 68 instanceSend: 1); // f resolved to B.f | |
| 69 }); | |
| 70 | |
| 71 test('monomorphic only one type possible from liveness', () { | |
| 72 return _check(''' | |
| 73 class A { | |
| 74 get f => 1; | |
| 75 } | |
| 76 class B extends A { | |
| 77 get f => 1; | |
| 78 } | |
| 79 main() => test(); | |
| 80 test() { A x = new B(); x.f; } | |
| 81 ''', | |
| 82 constructorSend: 1, // new B() | |
| 83 localSend: 1, // x in x.f | |
| 84 instanceSend: 1); // x.f known to resolve to B.f | |
| 85 }); | |
| 86 | |
| 87 test('monomorphic one possible, more than one live', () { | |
| 88 return _check(''' | |
| 89 class A { | |
| 90 get f => 1; | |
| 91 } | |
| 92 class B extends A { | |
| 93 get f => 1; | |
| 94 } | |
| 95 main() { new A(); test(); } | |
| 96 test() { B x = new B(); x.f; } | |
| 97 ''', | |
| 98 constructorSend: 1, // new B() | |
| 99 localSend: 1, // x in x.f | |
| 100 instanceSend: 1); // x.f resolves to B.f | |
| 101 }); | |
| 102 | |
| 103 test('polymorphic-virtual couple possible types from liveness', () { | |
| 104 // Note: this would be an instanceSend if we used the inferrer. | |
| 105 return _check(''' | |
| 106 class A { | |
| 107 get f => 1; | |
| 108 } | |
| 109 class B extends A { | |
| 110 get f => 1; | |
| 111 } | |
| 112 main() { new A(); test(); } | |
| 113 test() { A x = new B(); x.f; } | |
| 114 ''', | |
| 115 constructorSend: 1, // new B() | |
| 116 localSend: 1, // x in x.f | |
| 117 virtualSend: 1); // x.f may be A.f or B.f (types alone is not enough) | |
| 118 }); | |
| 119 | |
| 120 test("polymorphic-dynamic: type annotations don't help", () { | |
| 121 return _check(''' | |
| 122 class A { | |
| 123 get f => 1; | |
| 124 } | |
| 125 class B extends A { | |
| 126 get f => 1; | |
| 127 } | |
| 128 main() { new A(); test(); } | |
| 129 test() { var x = new B(); x.f; } | |
| 130 ''', | |
| 131 constructorSend: 1, // new B() | |
| 132 localSend: 1, // x in x.f | |
| 133 dynamicSend: 1); // x.f could be any `f` or no `f` | |
| 134 }); | |
| 135 }); | |
| 136 | |
| 137 group('instance this call', () { | |
| 138 test('monomorphic only one implementor', () { | |
| 139 return _check(''' | |
| 140 class A { | |
| 141 get f => 1; | |
| 142 test() => this.f; | |
| 143 } | |
| 144 main() => new A().test(); | |
| 145 ''', | |
| 146 instanceSend: 1); // this.f resolved to A.f | |
| 147 }); | |
| 148 | |
| 149 test('monomorphic only one type possible from types & liveness', () { | |
| 150 return _check(''' | |
| 151 class A { | |
| 152 get f => 1; | |
| 153 test() => this.f; | |
| 154 } | |
| 155 class B extends A { | |
| 156 get f => 1; | |
| 157 } | |
| 158 main() => new B().test(); | |
| 159 ''', | |
| 160 instanceSend: 1); // this.f resolved to B.f | |
| 161 }); | |
| 162 | |
| 163 test('polymorphic-virtual couple possible types from liveness', () { | |
| 164 // Note: this would be an instanceSend if we used the inferrer. | |
| 165 return _check(''' | |
| 166 class A { | |
| 167 get f => 1; | |
| 168 test() => this.f; | |
| 169 } | |
| 170 class B extends A { | |
| 171 get f => 1; | |
| 172 } | |
| 173 main() { new A(); new B().test(); } | |
| 174 ''', | |
| 175 virtualSend: 1); // this.f may be A.f or B.f | |
| 176 }); | |
| 177 }); | |
| 178 | |
| 179 group('noSuchMethod', () { | |
| 180 test('error will be thrown', () { | |
| 181 return _check(''' | |
| 182 class A { | |
| 183 } | |
| 184 main() { test(); } | |
| 185 test() { new A().f; } | |
| 186 ''', | |
| 187 constructorSend: 1, // new B() | |
| 188 nsmErrorSend: 1); // f not there, A has no nSM | |
| 189 }); | |
| 190 | |
| 191 test('nSM will be called - one option', () { | |
| 192 return _check(''' | |
| 193 class A { | |
| 194 noSuchMethod(i) => null; | |
| 195 } | |
| 196 main() { test(); } | |
| 197 test() { new A().f; } | |
| 198 ''', | |
| 199 constructorSend: 1, // new B() | |
| 200 singleNsmCallSend: 1); // f not there, A has nSM | |
| 201 }); | |
| 202 | |
| 203 // TODO(sigmund): is it worth splitting multiNSMvirtual? | |
| 204 test('nSM will be called - multiple options', () { | |
| 205 return _check(''' | |
| 206 class A { | |
| 207 noSuchMethod(i) => null; | |
| 208 } | |
| 209 class B extends A { | |
| 210 noSuchMethod(i) => null; | |
| 211 } | |
| 212 main() { new A(); test(); } | |
| 213 test() { A x = new B(); x.f; } | |
| 214 ''', | |
| 215 constructorSend: 1, // new B() | |
| 216 localSend: 1, // x in x.f | |
| 217 multiNsmCallSend: 1); // f not there, A has nSM | |
| 218 }); | |
| 219 | |
| 220 // TODO(sigmund): is it worth splitting multiNSMvirtual? | |
| 221 test('nSM will be called - multiple options', () { | |
| 222 return _check(''' | |
| 223 class A { | |
| 224 noSuchMethod(i) => null; | |
| 225 } | |
| 226 class B extends A { | |
| 227 // don't count A's nsm as distinct | |
| 228 } | |
| 229 main() { new A(); test(); } | |
| 230 test() { A x = new B(); x.f; } | |
| 231 ''', | |
| 232 constructorSend: 1, // new B() | |
| 233 localSend: 1, // x in x.f | |
| 234 singleNsmCallSend: 1); // f not there, A has nSM | |
| 235 }); | |
| 236 | |
| 237 test('nSM will be called - multiple options', () { | |
| 238 return _check(''' | |
| 239 class A { | |
| 240 noSuchMethod(i) => null; | |
| 241 } | |
| 242 class B extends A { | |
| 243 get f => null; | |
| 244 } | |
| 245 main() { new A(); test(); } | |
| 246 test() { A x = new B(); x.f; } | |
| 247 ''', | |
| 248 constructorSend: 1, // new B() | |
| 249 localSend: 1, // x in x.f | |
| 250 dynamicSend: 1); // f not known to be there there, A has nSM | |
| 251 }); | |
| 252 | |
| 253 test('nSM in super', () { | |
| 254 return _check(''' | |
| 255 class A { | |
| 256 noSuchMethod(i) => null; | |
| 257 } | |
| 258 class B extends A { | |
| 259 get f => super.f; | |
| 260 } | |
| 261 main() { new A(); test(); } | |
| 262 test() { A x = new B(); x.f; } | |
| 263 ''', | |
| 264 singleNsmCallSend: 1, // super.f | |
| 265 testMethod: 'f'); | |
| 266 }); | |
| 267 }); | |
| 268 } | |
| 269 | |
| 270 | |
| 271 /// Checks that the `test` function in [code] produces the given distribution of | |
| 272 /// sends. | |
| 273 _check(String code, {int staticSend: 0, int superSend: 0, int localSend: 0, | |
| 274 int constructorSend: 0, int typeVariableSend: 0, int nsmErrorSend: 0, | |
| 275 int singleNsmCallSend: 0, int instanceSend: 0, int interceptorSend: 0, | |
| 276 int multiNsmCallSend: 0, int virtualSend: 0, int multiInterceptorSend: 0, | |
| 277 int dynamicSend: 0, String testMethod: 'test'}) async { | |
| 278 | |
| 279 // Set up the expectation. | |
| 280 var expected = new Measurements(); | |
| 281 int monomorphic = staticSend + superSend + localSend + constructorSend + | |
| 282 typeVariableSend + nsmErrorSend + singleNsmCallSend + instanceSend + | |
| 283 interceptorSend; | |
| 284 int polymorphic = multiNsmCallSend + virtualSend + multiInterceptorSend + | |
| 285 dynamicSend; | |
| 286 | |
| 287 expected.counters[Metric.monomorphicSend] = monomorphic; | |
| 288 expected.counters[Metric.staticSend] = staticSend; | |
| 289 expected.counters[Metric.superSend] = superSend; | |
| 290 expected.counters[Metric.localSend] = localSend; | |
| 291 expected.counters[Metric.constructorSend] = constructorSend; | |
| 292 expected.counters[Metric.typeVariableSend] = typeVariableSend; | |
| 293 expected.counters[Metric.nsmErrorSend] = nsmErrorSend; | |
| 294 expected.counters[Metric.singleNsmCallSend] = singleNsmCallSend; | |
| 295 expected.counters[Metric.instanceSend] = instanceSend; | |
| 296 expected.counters[Metric.interceptorSend] = interceptorSend; | |
| 297 | |
| 298 expected.counters[Metric.polymorphicSend] = polymorphic; | |
| 299 expected.counters[Metric.multiNsmCallSend] = multiNsmCallSend; | |
| 300 expected.counters[Metric.virtualSend] = virtualSend; | |
| 301 expected.counters[Metric.multiInterceptorSend] = multiInterceptorSend; | |
| 302 expected.counters[Metric.dynamicSend] = dynamicSend; | |
| 303 | |
| 304 expected.counters[Metric.send] = monomorphic + polymorphic; | |
| 305 | |
| 306 // Run the compiler to get the results. | |
| 307 var all = await _compileAndGetStats(code); | |
| 308 var function = all.functions.firstWhere((f) => f.name == testMethod, | |
| 309 orElse: () => null); | |
| 310 var result = function?.measurements; | |
| 311 if (function == null) { | |
| 312 expect(expected.counters[Metric.send], 0); | |
| 313 return; | |
| 314 } | |
| 315 | |
| 316 expect(result, isNotNull); | |
| 317 | |
| 318 _compareMetric(Metric key) { | |
| 319 var expectedValue = expected.counters[key]; | |
| 320 var value = result.counters[key]; | |
| 321 if (value == null) value = 0; | |
| 322 if (value == expectedValue) return; | |
| 323 expect(expectedValue, value, | |
| 324 reason: "count for `$key` didn't match:\n" | |
| 325 "expected measurements:\n${recursiveDiagnosticString(expected, key)}\n" | |
| 326 "actual measurements:\n${recursiveDiagnosticString(result, key)}"); | |
| 327 } | |
| 328 | |
| 329 _compareMetric(Metric.send); | |
| 330 expected.counters.keys.forEach(_compareMetric); | |
| 331 } | |
| 332 | |
| 333 /// Helper that runs the compiler and returns the [GlobalResult] computed for | |
| 334 /// it. | |
| 335 Future<AllInfo> _compileAndGetStats(String program) async { | |
| 336 var result = await runCompiler( | |
| 337 memorySourceFiles: {'main.dart': program}, options: ['--dump-info']); | |
| 338 expect(result.compiler.compilationFailed, isFalse); | |
| 339 return result.compiler.dumpInfoTask.infoCollector.result; | |
| 340 } | |
| OLD | NEW |