| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:isolate'; | 5 import 'dart:isolate'; |
| 6 import 'dart:async'; | 6 import 'dart:async'; |
| 7 | 7 |
| 8 class Expect { | 8 class Expect { |
| 9 static void equals(x, y) { | 9 static void equals(x, y) { |
| 10 if (x != y) throw new ArgumentError('not equal'); | 10 if (x != y) throw new ArgumentError('not equal'); |
| 11 } | 11 } |
| 12 } | 12 } |
| 13 | 13 |
| 14 class Fields { | 14 class Fields { |
| 15 Fields(int i, int j) | 15 Fields(int i, int j) : fld1 = i, fld2 = j, fld5 = true {} |
| 16 : fld1 = i, | |
| 17 fld2 = j, | |
| 18 fld5 = true {} | |
| 19 int fld1; | 16 int fld1; |
| 20 final int fld2; | 17 final int fld2; |
| 21 static int fld3; | 18 static int fld3; |
| 22 static const int fld4 = 10; | 19 static const int fld4 = 10; |
| 23 bool fld5; | 20 bool fld5; |
| 24 } | 21 } |
| 25 | |
| 26 class FieldsTest { | 22 class FieldsTest { |
| 27 static Fields testMain() { | 23 static Fields testMain() { |
| 28 Fields obj = new Fields(10, 20); | 24 Fields obj = new Fields(10, 20); |
| 29 Expect.equals(10, obj.fld1); | 25 Expect.equals(10, obj.fld1); |
| 30 Expect.equals(20, obj.fld2); | 26 Expect.equals(20, obj.fld2); |
| 31 Expect.equals(10, Fields.fld4); | 27 Expect.equals(10, Fields.fld4); |
| 32 Expect.equals(true, obj.fld5); | 28 Expect.equals(true, obj.fld5); |
| 33 return obj; | 29 return obj; |
| 34 } | 30 } |
| 35 } | 31 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 const LoopBenchmark() : super("Loop"); | 83 const LoopBenchmark() : super("Loop"); |
| 88 | 84 |
| 89 void warmup() { | 85 void warmup() { |
| 90 Loop.loop(10); | 86 Loop.loop(10); |
| 91 } | 87 } |
| 92 | 88 |
| 93 void exercise() { | 89 void exercise() { |
| 94 // This value has been copied from benchpress.js, so that we can compare | 90 // This value has been copied from benchpress.js, so that we can compare |
| 95 // performance. | 91 // performance. |
| 96 var result = Loop.loop(200); | 92 var result = Loop.loop(200); |
| 97 if (result != 20000) Error.error("Wrong result: $result. Should be: 20000"); | 93 if (result != 20000) |
| 94 Error.error("Wrong result: $result. Should be: 20000"); |
| 98 } | 95 } |
| 99 | 96 |
| 100 static void main() { | 97 static void main() { |
| 101 new LoopBenchmark().report(); | 98 new LoopBenchmark().report(); |
| 102 } | 99 } |
| 103 } | 100 } |
| 104 | 101 |
| 105 // T o w e r s | 102 // T o w e r s |
| 106 class TowersDisk { | 103 class TowersDisk { |
| 107 final int size; | 104 final int size; |
| 108 TowersDisk next; | 105 TowersDisk next; |
| 109 | 106 |
| 110 TowersDisk(size) | 107 TowersDisk(size) : this.size = size, next = null {} |
| 111 : this.size = size, | |
| 112 next = null {} | |
| 113 } | 108 } |
| 114 | 109 |
| 115 class Towers { | 110 class Towers { |
| 116 List<TowersDisk> piles; | 111 List<TowersDisk> piles; |
| 117 int movesDone; | 112 int movesDone; |
| 118 Towers(int disks) | 113 Towers(int disks) |
| 119 : piles = new List<TowersDisk>(3), | 114 : piles = new List<TowersDisk>(3), movesDone = 0 { |
| 120 movesDone = 0 { | |
| 121 build(0, disks); | 115 build(0, disks); |
| 122 } | 116 } |
| 123 | 117 |
| 124 void build(int pile, int disks) { | 118 void build(int pile, int disks) { |
| 125 for (var i = disks - 1; i >= 0; i--) { | 119 for (var i = disks - 1; i >= 0; i--) { |
| 126 push(pile, new TowersDisk(i)); | 120 push(pile, new TowersDisk(i)); |
| 127 } | 121 } |
| 128 } | 122 } |
| 129 | 123 |
| 130 void push(int pile, TowersDisk disk) { | 124 void push(int pile, TowersDisk disk) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 class SieveBenchmark extends BenchmarkBase { | 182 class SieveBenchmark extends BenchmarkBase { |
| 189 const SieveBenchmark() : super("Sieve"); | 183 const SieveBenchmark() : super("Sieve"); |
| 190 | 184 |
| 191 static int sieve(int size) { | 185 static int sieve(int size) { |
| 192 int primeCount = 0; | 186 int primeCount = 0; |
| 193 List<bool> flags = new List<bool>(size + 1); | 187 List<bool> flags = new List<bool>(size + 1); |
| 194 for (int i = 1; i < size; i++) flags[i] = true; | 188 for (int i = 1; i < size; i++) flags[i] = true; |
| 195 for (int i = 2; i < size; i++) { | 189 for (int i = 2; i < size; i++) { |
| 196 if (flags[i]) { | 190 if (flags[i]) { |
| 197 primeCount++; | 191 primeCount++; |
| 198 for (int k = i + 1; k <= size; k += i) flags[k - 1] = false; | 192 for (int k = i + 1; k <= size; k += i) |
| 193 flags[k - 1] = false; |
| 199 } | 194 } |
| 200 } | 195 } |
| 201 return primeCount; | 196 return primeCount; |
| 202 } | 197 } |
| 203 | 198 |
| 199 |
| 204 void warmup() { | 200 void warmup() { |
| 205 sieve(100); | 201 sieve(100); |
| 206 } | 202 } |
| 207 | 203 |
| 208 void exercise() { | 204 void exercise() { |
| 209 // This value has been copied from benchpress.js, so that we can compare | 205 // This value has been copied from benchpress.js, so that we can compare |
| 210 // performance. | 206 // performance. |
| 211 int result = sieve(1000); | 207 int result = sieve(1000); |
| 212 if (result != 168) Error.error("Wrong result: $result should be: 168"); | 208 if (result != 168) |
| 209 Error.error("Wrong result: $result should be: 168"); |
| 213 } | 210 } |
| 214 | 211 |
| 215 static void main() { | 212 static void main() { |
| 216 new SieveBenchmark().report(); | 213 new SieveBenchmark().report(); |
| 217 } | 214 } |
| 218 } | 215 } |
| 219 | 216 |
| 220 // P e r m u t e | 217 // P e r m u t e |
| 221 // The original benchmark uses one-based indexing. Even though arrays in JS and | 218 // The original benchmark uses one-based indexing. Even though arrays in JS and |
| 222 // lists in dart are zero-based, we stay with one-based indexing | 219 // lists in dart are zero-based, we stay with one-based indexing |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 const PermuteBenchmark() : super("Permute"); | 253 const PermuteBenchmark() : super("Permute"); |
| 257 | 254 |
| 258 void warmup() { | 255 void warmup() { |
| 259 new Permute().permute(4); | 256 new Permute().permute(4); |
| 260 } | 257 } |
| 261 | 258 |
| 262 void exercise() { | 259 void exercise() { |
| 263 // This value has been copied from benchpress.js, so that we can compare | 260 // This value has been copied from benchpress.js, so that we can compare |
| 264 // performance. | 261 // performance. |
| 265 int result = new Permute().permute(8); | 262 int result = new Permute().permute(8); |
| 266 if (result != 8660) Error.error("Wrong result: $result should be: 8660"); | 263 if (result != 8660) |
| 264 Error.error("Wrong result: $result should be: 8660"); |
| 267 } | 265 } |
| 268 | 266 |
| 269 static void main() { | 267 static void main() { |
| 270 new PermuteBenchmark().report(); | 268 new PermuteBenchmark().report(); |
| 271 } | 269 } |
| 272 } | 270 } |
| 273 | 271 |
| 274 // Q u e e n s | 272 // Q u e e n s |
| 275 // The original benchmark uses one-based indexing. Even though arrays in JS and | 273 // The original benchmark uses one-based indexing. Even though arrays in JS and |
| 276 // lists in dart are zero-based, we stay with one-based indexing | 274 // lists in dart are zero-based, we stay with one-based indexing |
| 277 // (wasting one element). | 275 // (wasting one element). |
| 278 class Queens { | 276 class Queens { |
| 279 static bool tryQueens( | 277 static bool tryQueens(int i, |
| 280 int i, List<bool> a, List<bool> b, List<bool> c, List<int> x) { | 278 List<bool> a, |
| 279 List<bool> b, |
| 280 List<bool> c, |
| 281 List<int> x) { |
| 281 int j = 0; | 282 int j = 0; |
| 282 bool q = false; | 283 bool q = false; |
| 283 while ((!q) && (j != 8)) { | 284 while ((!q) && (j != 8)) { |
| 284 j++; | 285 j++; |
| 285 q = false; | 286 q = false; |
| 286 if (b[j] && a[i + j] && c[i - j + 7]) { | 287 if (b[j] && a[i + j] && c[i - j + 7]) { |
| 287 x[i] = j; | 288 x[i] = j; |
| 288 b[j] = false; | 289 b[j] = false; |
| 289 a[i + j] = false; | 290 a[i + j] = false; |
| 290 c[i - j + 7] = false; | 291 c[i - j + 7] = false; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 308 List<bool> b = new List<bool>(17); | 309 List<bool> b = new List<bool>(17); |
| 309 List<bool> c = new List<bool>(15); | 310 List<bool> c = new List<bool>(15); |
| 310 List<int> x = new List<int>(9); | 311 List<int> x = new List<int>(9); |
| 311 b[1] = false; | 312 b[1] = false; |
| 312 for (int i = -7; i <= 16; i++) { | 313 for (int i = -7; i <= 16; i++) { |
| 313 if ((i >= 1) && (i <= 8)) a[i] = true; | 314 if ((i >= 1) && (i <= 8)) a[i] = true; |
| 314 if (i >= 2) b[i] = true; | 315 if (i >= 2) b[i] = true; |
| 315 if (i <= 7) c[i + 7] = true; | 316 if (i <= 7) c[i + 7] = true; |
| 316 } | 317 } |
| 317 | 318 |
| 318 if (!tryQueens(1, b, a, c, x)) Error.error("Error in queens"); | 319 if (!tryQueens(1, b, a, c, x)) |
| 320 Error.error("Error in queens"); |
| 319 } | 321 } |
| 320 } | 322 } |
| 321 | 323 |
| 322 class QueensBenchmark extends BenchmarkBase { | 324 class QueensBenchmark extends BenchmarkBase { |
| 323 const QueensBenchmark() : super("Queens"); | 325 const QueensBenchmark() : super("Queens"); |
| 324 | 326 |
| 325 void warmup() { | 327 void warmup() { |
| 326 Queens.queens(); | 328 Queens.queens(); |
| 327 } | 329 } |
| 328 | 330 |
| 329 void exercise() { | 331 void exercise() { |
| 330 Queens.queens(); | 332 Queens.queens(); |
| 331 } | 333 } |
| 332 | 334 |
| 333 static void main() { | 335 static void main() { |
| 334 new QueensBenchmark().report(); | 336 new QueensBenchmark().report(); |
| 335 } | 337 } |
| 336 } | 338 } |
| 337 | 339 |
| 340 |
| 338 // R e c u r s e | 341 // R e c u r s e |
| 339 class Recurse { | 342 class Recurse { |
| 340 static int recurse(int n) { | 343 static int recurse(int n) { |
| 341 if (n <= 0) return 1; | 344 if (n <= 0) return 1; |
| 342 recurse(n - 1); | 345 recurse(n - 1); |
| 343 return recurse(n - 1); | 346 return recurse(n - 1); |
| 344 } | 347 } |
| 345 } | 348 } |
| 346 | 349 |
| 347 class RecurseBenchmark extends BenchmarkBase { | 350 class RecurseBenchmark extends BenchmarkBase { |
| 348 const RecurseBenchmark() : super("Recurse"); | 351 const RecurseBenchmark() : super("Recurse"); |
| 349 | 352 |
| 350 void warmup() { | 353 void warmup() { |
| 351 Recurse.recurse(7); | 354 Recurse.recurse(7); |
| 352 } | 355 } |
| 353 | 356 |
| 354 void exercise() { | 357 void exercise() { |
| 355 // This value has been copied from benchpress.js, so that we can compare | 358 // This value has been copied from benchpress.js, so that we can compare |
| 356 // performance. | 359 // performance. |
| 357 Recurse.recurse(13); | 360 Recurse.recurse(13); |
| 358 } | 361 } |
| 359 | 362 |
| 360 static void main() { | 363 static void main() { |
| 361 new RecurseBenchmark().report(); | 364 new RecurseBenchmark().report(); |
| 362 } | 365 } |
| 363 } | 366 } |
| 364 | 367 |
| 368 |
| 365 // S u m | 369 // S u m |
| 366 class SumBenchmark extends BenchmarkBase { | 370 class SumBenchmark extends BenchmarkBase { |
| 367 const SumBenchmark() : super("Sum"); | 371 const SumBenchmark() : super("Sum"); |
| 368 | 372 |
| 369 static int sum(int start, int end) { | 373 static int sum(int start, int end) { |
| 370 var sum = 0; | 374 var sum = 0; |
| 371 for (var i = start; i <= end; i++) sum += i; | 375 for (var i = start; i <= end; i++) sum += i; |
| 372 return sum; | 376 return sum; |
| 373 } | 377 } |
| 374 | 378 |
| 375 void warmup() { | 379 void warmup() { |
| 376 sum(1, 1000); | 380 sum(1, 1000); |
| 377 } | 381 } |
| 378 | 382 |
| 379 void exercise() { | 383 void exercise() { |
| 380 // This value has been copied from benchpress.js, so that we can compare | 384 // This value has been copied from benchpress.js, so that we can compare |
| 381 // performance. | 385 // performance. |
| 382 int result = sum(1, 10000); | 386 int result = sum(1, 10000); |
| 383 if (result != 50005000) | 387 if (result != 50005000) |
| 384 Error.error("Wrong result: $result should be 50005000"); | 388 Error.error("Wrong result: $result should be 50005000"); |
| 385 } | 389 } |
| 386 | 390 |
| 387 static void main() { | 391 static void main() { |
| 388 new SumBenchmark().report(); | 392 new SumBenchmark().report(); |
| 389 } | 393 } |
| 390 } | 394 } |
| 391 | 395 |
| 396 |
| 392 // H e l p e r f u n c t i o n s f o r s o r t s | 397 // H e l p e r f u n c t i o n s f o r s o r t s |
| 393 class Random { | 398 class Random { |
| 394 static const int INITIAL_SEED = 74755; | 399 static const int INITIAL_SEED = 74755; |
| 395 int seed; | 400 int seed; |
| 396 Random() : seed = INITIAL_SEED {} | 401 Random() : seed = INITIAL_SEED {} |
| 397 | 402 |
| 398 int random() { | 403 int random() { |
| 399 seed = ((seed * 1309) + 13849) % 65536; | 404 seed = ((seed * 1309) + 13849) % 65536; |
| 400 return seed; | 405 return seed; |
| 401 } | 406 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 420 if (e < min) min = e; | 425 if (e < min) min = e; |
| 421 } | 426 } |
| 422 | 427 |
| 423 this.min = min; | 428 this.min = min; |
| 424 this.max = max; | 429 this.max = max; |
| 425 } | 430 } |
| 426 | 431 |
| 427 void check() { | 432 void check() { |
| 428 List<int> a = list; | 433 List<int> a = list; |
| 429 int len = a.length; | 434 int len = a.length; |
| 430 if ((a[0] != min) || a[len - 1] != max) Error.error("List is not sorted"); | 435 if ((a[0] != min) || a[len - 1] != max) |
| 436 Error.error("List is not sorted"); |
| 431 for (var i = 1; i < len; i++) { | 437 for (var i = 1; i < len; i++) { |
| 432 if (a[i - 1] > a[i]) Error.error("List is not sorted"); | 438 if (a[i - 1] > a[i]) Error.error("List is not sorted"); |
| 433 } | 439 } |
| 434 } | 440 } |
| 435 } | 441 } |
| 436 | 442 |
| 443 |
| 437 // B u b b l e S o r t | 444 // B u b b l e S o r t |
| 438 class BubbleSort { | 445 class BubbleSort { |
| 439 static void sort(List<int> a) { | 446 static void sort(List<int> a) { |
| 440 int len = a.length; | 447 int len = a.length; |
| 441 for (int i = len - 2; i >= 0; i--) { | 448 for (int i = len - 2; i >= 0; i--) { |
| 442 for (int j = 0; j <= i; j++) { | 449 for (int j = 0; j <= i; j++) { |
| 443 int c = a[j]; | 450 int c = a[j]; |
| 444 int n = a[j + 1]; | 451 int n = a[j + 1]; |
| 445 if (c > n) { | 452 if (c > n) { |
| 446 a[j] = n; | 453 a[j] = n; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 509 SortData data = new SortData(800); | 516 SortData data = new SortData(800); |
| 510 QuickSort.sort(data.list, 0, data.list.length - 1); | 517 QuickSort.sort(data.list, 0, data.list.length - 1); |
| 511 data.check(); | 518 data.check(); |
| 512 } | 519 } |
| 513 | 520 |
| 514 static void main() { | 521 static void main() { |
| 515 new QuickSortBenchmark().report(); | 522 new QuickSortBenchmark().report(); |
| 516 } | 523 } |
| 517 } | 524 } |
| 518 | 525 |
| 526 |
| 519 // T r e e S o r t | 527 // T r e e S o r t |
| 520 class TreeNodePress { | 528 class TreeNodePress { |
| 521 int value; | 529 int value; |
| 522 TreeNodePress left; | 530 TreeNodePress left; |
| 523 TreeNodePress right; | 531 TreeNodePress right; |
| 524 | 532 |
| 525 TreeNodePress(int n) : value = n {} | 533 TreeNodePress(int n) : value = n {} |
| 526 | 534 |
| 527 void insert(int n) { | 535 void insert(int n) { |
| 528 if (n < value) { | 536 if (n < value) { |
| 529 if (left == null) | 537 if (left == null) left = new TreeNodePress(n); |
| 530 left = new TreeNodePress(n); | 538 else left.insert(n); |
| 531 else | |
| 532 left.insert(n); | |
| 533 } else { | 539 } else { |
| 534 if (right == null) | 540 if (right == null) right = new TreeNodePress(n); |
| 535 right = new TreeNodePress(n); | 541 else right.insert(n); |
| 536 else | |
| 537 right.insert(n); | |
| 538 } | 542 } |
| 539 } | 543 } |
| 540 | 544 |
| 541 void check() { | 545 void check() { |
| 542 TreeNodePress left = this.left; | 546 TreeNodePress left = this.left; |
| 543 TreeNodePress right = this.right; | 547 TreeNodePress right = this.right; |
| 544 int value = this.value; | 548 int value = this.value; |
| 545 | 549 |
| 546 return ((left == null) || ((left.value < value) && left.check())) && | 550 return ((left == null) || ((left.value < value) && left.check())) && |
| 547 ((right == null) || ((right.value >= value) && right.check())); | 551 ((right == null) || ((right.value >= value) && right.check())); |
| 548 } | 552 } |
| 549 } | 553 } |
| 550 | 554 |
| 551 class TreeSort { | 555 class TreeSort { |
| 552 static void sort(List<int> a) { | 556 static void sort(List<int> a) { |
| 553 int len = a.length; | 557 int len = a.length; |
| 554 TreeNodePress tree = new TreeNodePress(a[0]); | 558 TreeNodePress tree = new TreeNodePress(a[0]); |
| 555 for (var i = 1; i < len; i++) tree.insert(a[i]); | 559 for (var i = 1; i < len; i++) tree.insert(a[i]); |
| 556 if (!tree.check()) Error.error("Invalid result, tree not sorted"); | 560 if (!tree.check()) Error.error("Invalid result, tree not sorted"); |
| 557 } | 561 } |
| 558 } | 562 } |
| 559 | 563 |
| 560 class TreeSortBenchmark extends BenchmarkBase { | 564 class TreeSortBenchmark extends BenchmarkBase { |
| 561 const TreeSortBenchmark() : super("TreeSort"); | 565 const TreeSortBenchmark() : super("TreeSort"); |
| 562 | 566 |
| 563 void warmup() { | 567 void warmup() { |
| 564 TreeSort.sort(new SortData(100).list); | 568 TreeSort.sort(new SortData(100).list); |
| 565 } | 569 } |
| 566 | 570 |
| 567 void exercise() { | 571 void exercise() { |
| 568 // This value has been copied from benchpress.js, so that we can compare | 572 // This value has been copied from benchpress.js, so that we can compare |
| 569 // performance. | 573 // performance. |
| 570 TreeSort.sort(new SortData(1000).list); | 574 TreeSort.sort(new SortData(1000).list); |
| 571 } | 575 } |
| 572 } | 576 } |
| 573 | 577 |
| 578 |
| 574 // T a k | 579 // T a k |
| 575 class TakBenchmark extends BenchmarkBase { | 580 class TakBenchmark extends BenchmarkBase { |
| 576 const TakBenchmark() : super("Tak"); | 581 const TakBenchmark() : super("Tak"); |
| 577 | 582 |
| 578 static void tak(int x, int y, int z) { | 583 static void tak(int x, int y, int z) { |
| 579 if (y >= x) return z; | 584 if (y >= x) return z; |
| 580 return tak(tak(x - 1, y, z), tak(y - 1, z, x), tak(z - 1, x, y)); | 585 return tak(tak(x - 1, y, z), tak(y - 1, z, x), tak(z - 1, x, y)); |
| 581 } | 586 } |
| 582 | 587 |
| 583 void warmup() { | 588 void warmup() { |
| 584 tak(9, 6, 3); | 589 tak(9, 6, 3); |
| 585 } | 590 } |
| 586 | 591 |
| 587 void exercise() { | 592 void exercise() { |
| 588 // This value has been copied from benchpress.js, so that we can compare | 593 // This value has been copied from benchpress.js, so that we can compare |
| 589 // performance. | 594 // performance. |
| 590 tak(18, 12, 6); | 595 tak(18, 12, 6); |
| 591 } | 596 } |
| 592 | 597 |
| 593 static void main() { | 598 static void main() { |
| 594 new TakBenchmark().report(); | 599 new TakBenchmark().report(); |
| 595 } | 600 } |
| 596 } | 601 } |
| 597 | 602 |
| 603 |
| 598 // T a k l | 604 // T a k l |
| 599 class ListElement { | 605 class ListElement { |
| 600 final int length; | 606 final int length; |
| 601 final ListElement next; | 607 final ListElement next; |
| 602 | 608 |
| 603 const ListElement(int length, ListElement next) | 609 const ListElement(int length, ListElement next) |
| 604 : this.length = length, | 610 : this.length = length, |
| 605 this.next = next; | 611 this.next = next; |
| 606 | 612 |
| 607 static ListElement makeList(int length) { | 613 static ListElement makeList(int length) { |
| 608 if (length == 0) return null; | 614 if (length == 0) return null; |
| 609 return new ListElement(length, makeList(length - 1)); | 615 return new ListElement(length, makeList(length - 1)); |
| 610 } | 616 } |
| 611 | 617 |
| 612 static bool isShorter(ListElement x, ListElement y) { | 618 static bool isShorter(ListElement x, ListElement y) { |
| 613 ListElement xTail = x; | 619 ListElement xTail = x; |
| 614 ListElement yTail = y; | 620 ListElement yTail = y; |
| 615 while (yTail != null) { | 621 while (yTail != null) { |
| 616 if (xTail == null) return true; | 622 if (xTail == null) return true; |
| 617 xTail = xTail.next; | 623 xTail = xTail.next; |
| 618 yTail = yTail.next; | 624 yTail = yTail.next; |
| 619 } | 625 } |
| 620 return false; | 626 return false; |
| 621 } | 627 } |
| 622 } | 628 } |
| 623 | 629 |
| 624 class Takl { | 630 class Takl { |
| 625 static ListElement takl(ListElement x, ListElement y, ListElement z) { | 631 static ListElement takl(ListElement x, ListElement y, ListElement z) { |
| 626 if (ListElement.isShorter(y, x)) { | 632 if (ListElement.isShorter(y, x)) { |
| 627 return takl(takl(x.next, y, z), takl(y.next, z, x), takl(z.next, x, y)); | 633 return takl(takl(x.next, y, z), |
| 634 takl(y.next, z, x), |
| 635 takl(z.next, x, y)); |
| 628 } else { | 636 } else { |
| 629 return z; | 637 return z; |
| 630 } | 638 } |
| 631 } | 639 } |
| 632 } | 640 } |
| 633 | 641 |
| 634 class TaklBenchmark extends BenchmarkBase { | 642 class TaklBenchmark extends BenchmarkBase { |
| 635 const TaklBenchmark() : super("Takl"); | 643 const TaklBenchmark() : super("Takl"); |
| 636 | 644 |
| 637 void warmup() { | 645 void warmup() { |
| 638 Takl.takl(ListElement.makeList(8), ListElement.makeList(4), | 646 Takl.takl(ListElement.makeList(8), |
| 639 ListElement.makeList(3)); | 647 ListElement.makeList(4), |
| 648 ListElement.makeList(3)); |
| 640 } | 649 } |
| 641 | 650 |
| 642 void exercise() { | 651 void exercise() { |
| 643 // This value has been copied from benchpress.js, so that we can compare | 652 // This value has been copied from benchpress.js, so that we can compare |
| 644 // performance. | 653 // performance. |
| 645 ListElement result = Takl.takl(ListElement.makeList(15), | 654 ListElement result = Takl.takl(ListElement.makeList(15), |
| 646 ListElement.makeList(10), ListElement.makeList(6)); | 655 ListElement.makeList(10), |
| 656 ListElement.makeList(6)); |
| 647 if (result.length != 10) { | 657 if (result.length != 10) { |
| 648 int len = result.length; | 658 int len = result.length; |
| 649 Error.error("Wrong result: $len should be: 10"); | 659 Error.error("Wrong result: $len should be: 10"); |
| 650 } | 660 } |
| 651 } | 661 } |
| 652 | 662 |
| 653 static void main() { | 663 static void main() { |
| 654 new TaklBenchmark().report(); | 664 new TaklBenchmark().report(); |
| 655 } | 665 } |
| 656 } | 666 } |
| 657 | 667 |
| 658 // M a i n | 668 // M a i n |
| 659 | 669 |
| 660 class BenchPress { | 670 class BenchPress { |
| 661 static void mainWithArgs(List<String> args) { | 671 static void mainWithArgs(List<String> args) { |
| 662 List<BenchmarkBase> benchmarks = [ | 672 List<BenchmarkBase> benchmarks = [ |
| 663 new BubbleSortBenchmark(), | 673 new BubbleSortBenchmark(), |
| 664 new FibBenchmark(), | 674 new FibBenchmark(), |
| 665 new LoopBenchmark(), | 675 new LoopBenchmark(), |
| 666 new PermuteBenchmark(), | 676 new PermuteBenchmark(), |
| 667 new QueensBenchmark(), | 677 new QueensBenchmark(), |
| 668 new QuickSortBenchmark(), | 678 new QuickSortBenchmark(), |
| 669 new RecurseBenchmark(), | 679 new RecurseBenchmark(), |
| 670 new SieveBenchmark(), | 680 new SieveBenchmark(), |
| 671 new SumBenchmark(), | 681 new SumBenchmark(), |
| 672 new TakBenchmark(), | 682 new TakBenchmark(), |
| 673 new TaklBenchmark(), | 683 new TaklBenchmark(), |
| 674 new TowersBenchmark(), | 684 new TowersBenchmark(), |
| 675 new TreeSortBenchmark(), | 685 new TreeSortBenchmark(), |
| 676 ]; | 686 ]; |
| 677 if (args.length > 0) { | 687 if (args.length > 0) { |
| 678 String benchName = args[0]; | 688 String benchName = args[0]; |
| 679 bool foundBenchmark = false; | 689 bool foundBenchmark = false; |
| 680 benchmarks.forEach((bench) { | 690 benchmarks.forEach((bench) { |
| 681 if (bench.name == benchName) { | 691 if (bench.name == benchName) { |
| 682 foundBenchmark = true; | 692 foundBenchmark = true; |
| 683 bench.report(); | 693 bench.report(); |
| 684 } | 694 } |
| 685 }); | 695 }); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 707 } | 717 } |
| 708 | 718 |
| 709 class BenchmarkBase { | 719 class BenchmarkBase { |
| 710 final String name; | 720 final String name; |
| 711 | 721 |
| 712 // Empty constructor. | 722 // Empty constructor. |
| 713 const BenchmarkBase(String name) : this.name = name; | 723 const BenchmarkBase(String name) : this.name = name; |
| 714 | 724 |
| 715 // The benchmark code. | 725 // The benchmark code. |
| 716 // This function is not used, if both [warmup] and [exercise] are overwritten. | 726 // This function is not used, if both [warmup] and [exercise] are overwritten. |
| 717 void run() {} | 727 void run() { } |
| 718 | 728 |
| 719 // Runs a short version of the benchmark. By default invokes [run] once. | 729 // Runs a short version of the benchmark. By default invokes [run] once. |
| 720 void warmup() { | 730 void warmup() { |
| 721 run(); | 731 run(); |
| 722 } | 732 } |
| 723 | 733 |
| 724 // Exercices the benchmark. By default invokes [run] 10 times. | 734 // Exercices the benchmark. By default invokes [run] 10 times. |
| 725 void exercise() { | 735 void exercise() { |
| 726 for (int i = 0; i < 10; i++) { | 736 for (int i = 0; i < 10; i++) { |
| 727 run(); | 737 run(); |
| 728 } | 738 } |
| 729 } | 739 } |
| 730 | 740 |
| 731 // Not measured setup code executed prior to the benchmark runs. | 741 // Not measured setup code executed prior to the benchmark runs. |
| 732 void setup() {} | 742 void setup() { } |
| 733 | 743 |
| 734 // Not measures teardown code executed after the benchark runs. | 744 // Not measures teardown code executed after the benchark runs. |
| 735 void teardown() {} | 745 void teardown() { } |
| 736 | 746 |
| 737 // Measures the score for this benchmark by executing it repeately until | 747 // Measures the score for this benchmark by executing it repeately until |
| 738 // time minimum has been reached. | 748 // time minimum has been reached. |
| 739 static double measureFor(Function f, int timeMinimum) { | 749 static double measureFor(Function f, int timeMinimum) { |
| 740 int time = 0; | 750 int time = 0; |
| 741 int iter = 0; | 751 int iter = 0; |
| 742 DateTime start = new DateTime.now(); | 752 DateTime start = new DateTime.now(); |
| 743 while (time < timeMinimum) { | 753 while (time < timeMinimum) { |
| 744 f(); | 754 f(); |
| 745 time = (new DateTime.now().difference(start)).inMilliseconds; | 755 time = (new DateTime.now().difference(start)).inMilliseconds; |
| 746 iter++; | 756 iter++; |
| 747 } | 757 } |
| 748 // Force double result by using a double constant. | 758 // Force double result by using a double constant. |
| 749 return (1000.0 * iter) / time; | 759 return (1000.0 * iter) / time; |
| 750 } | 760 } |
| 751 | 761 |
| 752 // Measures the score for the benchmark and returns it. | 762 // Measures the score for the benchmark and returns it. |
| 753 double measure() { | 763 double measure() { |
| 754 setup(); | 764 setup(); |
| 755 // Warmup for at least 100ms. Discard result. | 765 // Warmup for at least 100ms. Discard result. |
| 756 measureFor(() { | 766 measureFor(() { this.warmup(); }, -100); |
| 757 this.warmup(); | |
| 758 }, -100); | |
| 759 // Run the benchmark for at least 2000ms. | 767 // Run the benchmark for at least 2000ms. |
| 760 double result = measureFor(() { | 768 double result = measureFor(() { this.exercise(); }, -2000); |
| 761 this.exercise(); | |
| 762 }, -2000); | |
| 763 teardown(); | 769 teardown(); |
| 764 return result; | 770 return result; |
| 765 } | 771 } |
| 766 | 772 |
| 767 void report() { | 773 void report() { |
| 768 double score = measure(); | 774 double score = measure(); |
| 769 print("name: $score"); | 775 print("name: $score"); |
| 770 } | 776 } |
| 777 |
| 771 } | 778 } |
| 772 | |
| 773 class Logger { | 779 class Logger { |
| 774 static print(object) { | 780 static print(object) { |
| 775 printobject(object); | 781 printobject(object); |
| 776 } | 782 } |
| 783 static printobject(obj) { } |
| 784 } |
| 777 | 785 |
| 778 static printobject(obj) {} | |
| 779 } | |
| 780 | 786 |
| 781 // | 787 // |
| 782 // Dromaeo ObjectString | 788 // Dromaeo ObjectString |
| 783 // Adapted from Mozilla JavaScript performance test suite. | 789 // Adapted from Mozilla JavaScript performance test suite. |
| 784 // Microtests of strings (concatenation, methods). | 790 // Microtests of strings (concatenation, methods). |
| 785 | 791 |
| 792 |
| 786 class ObjectString extends BenchmarkBase { | 793 class ObjectString extends BenchmarkBase { |
| 794 |
| 787 const ObjectString() : super("Dromaeo.ObjectString"); | 795 const ObjectString() : super("Dromaeo.ObjectString"); |
| 788 | 796 |
| 789 static void main() { | 797 static void main() { |
| 790 new ObjectString().report(); | 798 new ObjectString().report(); |
| 791 } | 799 } |
| 792 | 800 |
| 793 static void print(String str) { | 801 static void print(String str) { |
| 794 print(str); | 802 print(str); |
| 795 } | 803 } |
| 796 | 804 |
| 797 String getRandomString(int characters) { | 805 String getRandomString(int characters) { |
| 798 var result = ""; | 806 var result = ""; |
| 799 for (var i = 0; i < characters; i++) { | 807 for (var i = 0; i < characters; i++) { |
| 800 result += | 808 result += Strings. |
| 801 Strings.createFromCodePoints([(25 * Math.random()).toInt() + 97]); | 809 createFromCodePoints([(25 * Math.random()).toInt() + 97]); |
| 802 } | 810 } |
| 803 result += result; | 811 result += result; |
| 804 result += result; | 812 result += result; |
| 805 return result; | 813 return result; |
| 806 } | 814 } |
| 807 | 815 |
| 808 void run() { | 816 void run() { |
| 809 //JS Dromeaeo uses 16384 | 817 //JS Dromeaeo uses 16384 |
| 810 final ITERATE1 = 384; | 818 final ITERATE1 = 384; |
| 811 //JS Dromeaeo uses 80000 | 819 //JS Dromeaeo uses 80000 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 908 str = input[0]; | 916 str = input[0]; |
| 909 str = input[input.length - 1]; | 917 str = input[input.length - 1]; |
| 910 str = input[150]; //set it to 15000 | 918 str = input[150]; //set it to 15000 |
| 911 str = input[100]; //set it to 10000 | 919 str = input[100]; //set it to 10000 |
| 912 str = input[50]; //set it to 5000 | 920 str = input[50]; //set it to 5000 |
| 913 } | 921 } |
| 914 return str; | 922 return str; |
| 915 } | 923 } |
| 916 } | 924 } |
| 917 | 925 |
| 926 |
| 918 class CodeUnitAtBenchmark { | 927 class CodeUnitAtBenchmark { |
| 919 CodeUnitAtBenchmark() {} | 928 CodeUnitAtBenchmark() {} |
| 920 | 929 |
| 921 static String test(String input, var iterations) { | 930 static String test(String input, var iterations) { |
| 922 var str; | 931 var str; |
| 923 for (var j = 0; j < iterations; j++) { | 932 for (var j = 0; j < iterations; j++) { |
| 924 str = input.codeUnitAt(0); | 933 str = input.codeUnitAt(0); |
| 925 str = input.codeUnitAt(input.length - 1); | 934 str = input.codeUnitAt(input.length - 1); |
| 926 str = input.codeUnitAt(150); //set it to 15000 | 935 str = input.codeUnitAt(150); //set it to 15000 |
| 927 str = input.codeUnitAt(100); //set it to 10000 | 936 str = input.codeUnitAt(100); //set it to 10000 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 965 SliceBenchmark() {} | 974 SliceBenchmark() {} |
| 966 | 975 |
| 967 static String test(String input, var iterations) { | 976 static String test(String input, var iterations) { |
| 968 var str; | 977 var str; |
| 969 for (var j = 0; j < iterations; j++) { | 978 for (var j = 0; j < iterations; j++) { |
| 970 str = input.substring(0, input.length - 1); | 979 str = input.substring(0, input.length - 1); |
| 971 str = input.substring(0, 5); | 980 str = input.substring(0, 5); |
| 972 str = input.substring(input.length - 1, input.length - 1); | 981 str = input.substring(input.length - 1, input.length - 1); |
| 973 str = input.substring(input.length - 6, input.length - 1); | 982 str = input.substring(input.length - 6, input.length - 1); |
| 974 str = input.substring(150, 155); //set to 15000 and 15005 | 983 str = input.substring(150, 155); //set to 15000 and 15005 |
| 975 str = input.substring(120, input.length - 1); //set to 12000 | 984 str = input.substring(120, input.length-1); //set to 12000 |
| 976 } | 985 } |
| 977 return str; | 986 return str; |
| 978 } | 987 } |
| 979 } | 988 } |
| 980 | 989 |
| 981 class SubstrBenchmark { | 990 class SubstrBenchmark { |
| 982 SubstrBenchmark() {} | 991 SubstrBenchmark() {} |
| 983 | 992 |
| 984 static String test(String input, var iterations) { | 993 static String test(String input, var iterations) { |
| 985 var str; | 994 var str; |
| 986 for (var j = 0; j < iterations; j++) { | 995 for (var j = 0; j < iterations; j++) { |
| 987 str = input.substring(0, input.length - 1); | 996 str = input.substring(0, input.length-1); |
| 988 str = input.substring(0, 4); | 997 str = input.substring(0, 4); |
| 989 str = input.substring(input.length - 1, input.length - 1); | 998 str = input.substring(input.length - 1, input.length - 1); |
| 990 str = input.substring(input.length - 6, input.length - 6); | 999 str = input.substring(input.length - 6, input.length - 6); |
| 991 str = input.substring(150, 154); //set to 15000 and 15005 | 1000 str = input.substring(150, 154); //set to 15000 and 15005 |
| 992 str = input.substring(120, 124); //set to 12000 | 1001 str = input.substring(120, 124); //set to 12000 |
| 993 } | 1002 } |
| 994 return str; | 1003 return str; |
| 995 } | 1004 } |
| 996 } | 1005 } |
| 997 | 1006 |
| 998 class SubstringBenchmark { | 1007 class SubstringBenchmark { |
| 999 SubstringBenchmark() {} | 1008 SubstringBenchmark() {} |
| 1000 | 1009 |
| 1001 static String test(String input, var iterations) { | 1010 static String test(String input, var iterations) { |
| 1002 var str; | 1011 var str; |
| 1003 for (var j = 0; j < iterations; j++) { | 1012 for (var j = 0; j < iterations; j++) { |
| 1004 str = input.substring(0, input.length - 1); | 1013 str = input.substring(0, input.length - 1); |
| 1005 str = input.substring(0, 4); | 1014 str = input.substring(0, 4); |
| 1006 str = input.substring(input.length - 1, input.length - 1); | 1015 str = input.substring(input.length - 1, input.length - 1); |
| 1007 str = input.substring(input.length - 6, input.length - 2); | 1016 str = input.substring(input.length - 6, input.length - 2); |
| 1008 str = input.substring(150, 154); //set to 15000 and 15005 | 1017 str = input.substring(150, 154); //set to 15000 and 15005 |
| 1009 str = input.substring(120, input.length - 2); //set to 12000 | 1018 str = input.substring(120, input.length - 2); //set to 12000 |
| 1010 } | 1019 } |
| 1011 return str; | 1020 return str; |
| 1021 |
| 1012 } | 1022 } |
| 1013 } | 1023 } |
| 1014 | 1024 |
| 1015 class ToLowerCaseBenchmark { | 1025 class ToLowerCaseBenchmark { |
| 1016 ToLowerCaseBenchmark() {} | 1026 ToLowerCaseBenchmark() {} |
| 1017 | 1027 |
| 1018 static String test(String input, var iterations) { | 1028 static String test(String input, var iterations) { |
| 1019 var str; | 1029 var str; |
| 1020 for (var j = 0; j < (iterations / 1000); j++) { | 1030 for (var j = 0; j < (iterations / 1000); j++) { |
| 1021 str = Ascii.toLowerCase(input); | 1031 str = Ascii.toLowerCase(input); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 1048 res = (tmp.compareTo(tmp2) < 0); | 1058 res = (tmp.compareTo(tmp2) < 0); |
| 1049 res = (tmp.compareTo(tmp2) > 0); | 1059 res = (tmp.compareTo(tmp2) > 0); |
| 1050 } | 1060 } |
| 1051 return res; | 1061 return res; |
| 1052 } | 1062 } |
| 1053 } | 1063 } |
| 1054 | 1064 |
| 1055 // Benchmarks basic message communication between two isolates. | 1065 // Benchmarks basic message communication between two isolates. |
| 1056 | 1066 |
| 1057 class Benchmark1 { | 1067 class Benchmark1 { |
| 1068 |
| 1058 static const MESSAGES = 10000; | 1069 static const MESSAGES = 10000; |
| 1059 static const INIT_MESSAGE = 0; | 1070 static const INIT_MESSAGE = 0; |
| 1060 static const TERMINATION_MESSAGE = -1; | 1071 static const TERMINATION_MESSAGE = -1; |
| 1061 static const WARMUP_TIME = 1000; | 1072 static const WARMUP_TIME = 1000; |
| 1062 static const RUN_TIME = 1000; | 1073 static const RUN_TIME = 1000; |
| 1063 static const RUNS = 5; | 1074 static const RUNS = 5; |
| 1064 | 1075 |
| 1076 |
| 1065 static int run() { | 1077 static int run() { |
| 1066 return _run; | 1078 return _run; |
| 1067 } | 1079 } |
| 1068 | 1080 |
| 1069 static void add_result(var opsms) { | 1081 static void add_result(var opsms) { |
| 1070 _run++; | 1082 _run++; |
| 1071 _opsms += opsms; | 1083 _opsms += opsms; |
| 1072 } | 1084 } |
| 1073 | 1085 |
| 1074 static void get_result() { | 1086 static void get_result() { |
| 1075 return _opsms / _run; | 1087 return _opsms / _run; |
| 1076 } | 1088 } |
| 1077 | 1089 |
| 1078 static void init() { | 1090 static void init() { |
| 1079 _run = 0; | 1091 _run = 0; |
| 1080 _opsms = 0.0; | 1092 _opsms = 0.0; |
| 1081 } | 1093 } |
| 1082 | 1094 |
| 1083 static void main() { | 1095 static void main() { |
| 1084 init(); | 1096 init(); |
| 1085 PingPongGame pingPongGame = new PingPongGame(); | 1097 PingPongGame pingPongGame = new PingPongGame(); |
| 1086 } | 1098 } |
| 1087 | 1099 |
| 1088 static var _run; | 1100 static var _run; |
| 1089 static var _opsms; | 1101 static var _opsms; |
| 1090 } | 1102 } |
| 1091 | 1103 |
| 1092 class PingPongGame { | 1104 class PingPongGame { |
| 1105 |
| 1093 PingPongGame() | 1106 PingPongGame() |
| 1094 : _ping = new ReceivePort(), | 1107 : _ping = new ReceivePort(), |
| 1095 _pingPort = _ping.toSendPort(), | 1108 _pingPort = _ping.toSendPort(), |
| 1096 _pong = null, | 1109 _pong = null, |
| 1097 _warmedup = false, | 1110 _warmedup = false, |
| 1098 _iterations = 0 { | 1111 _iterations = 0 { |
| 1099 SendPort _pong = spawnFunction(pong); | 1112 SendPort _pong = spawnFunction(pong); |
| 1100 play(); | 1113 play(); |
| 1101 } | 1114 } |
| 1102 | 1115 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1157 if (message == Benchmark1.INIT_MESSAGE) { | 1170 if (message == Benchmark1.INIT_MESSAGE) { |
| 1158 replyTo.send(message, null); | 1171 replyTo.send(message, null); |
| 1159 } else if (message == Benchmark1.TERMINATION_MESSAGE) { | 1172 } else if (message == Benchmark1.TERMINATION_MESSAGE) { |
| 1160 port.close(); | 1173 port.close(); |
| 1161 } else { | 1174 } else { |
| 1162 replyTo.send(message, null); | 1175 replyTo.send(message, null); |
| 1163 } | 1176 } |
| 1164 }); | 1177 }); |
| 1165 } | 1178 } |
| 1166 | 1179 |
| 1180 |
| 1167 class ManyGenericInstanceofTest { | 1181 class ManyGenericInstanceofTest { |
| 1168 static testMain() { | 1182 static testMain() { |
| 1169 for (int i = 0; i < 5000; i++) { | 1183 for (int i = 0; i < 5000; i++) { |
| 1170 GenericInstanceof.testMain(); | 1184 GenericInstanceof.testMain(); |
| 1171 } | 1185 } |
| 1172 } | 1186 } |
| 1173 } | 1187 } |
| 1174 | 1188 |
| 1175 // --------------------------------------------------------------------------- | 1189 // --------------------------------------------------------------------------- |
| 1176 // THE REST OF THIS FILE COULD BE AUTOGENERATED | 1190 // THE REST OF THIS FILE COULD BE AUTOGENERATED |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1204 void isolate_negative_entry() { | 1218 void isolate_negative_entry() { |
| 1205 port.receive((ignored, replyTo) { | 1219 port.receive((ignored, replyTo) { |
| 1206 replyTo.send("foo", null); | 1220 replyTo.send("foo", null); |
| 1207 }); | 1221 }); |
| 1208 } | 1222 } |
| 1209 | 1223 |
| 1210 isolate_negative_test_main() { | 1224 isolate_negative_test_main() { |
| 1211 test("ensure isolate code is executed", () { | 1225 test("ensure isolate code is executed", () { |
| 1212 SendPort port = spawnFunction(isolate_negative_entry); | 1226 SendPort port = spawnFunction(isolate_negative_entry); |
| 1213 port.call("foo").then(expectAsync1((message) { | 1227 port.call("foo").then(expectAsync1((message) { |
| 1214 Expect.equals(true, "Expected fail"); // <=-------- Should fail here. | 1228 Expect.equals(true, "Expected fail"); // <=-------- Should fail here. |
| 1215 })); | 1229 })); |
| 1216 }); | 1230 }); |
| 1217 } | 1231 } |
| 1218 | 1232 |
| 1219 // --------------------------------------------------------------------------- | 1233 // --------------------------------------------------------------------------- |
| 1220 // tests/isolate/message_test.dart | 1234 // tests/isolate/message_test.dart |
| 1221 // --------------------------------------------------------------------------- | 1235 // --------------------------------------------------------------------------- |
| 1222 | 1236 |
| 1223 // --------------------------------------------------------------------------- | 1237 // --------------------------------------------------------------------------- |
| 1224 // Message passing test. | 1238 // Message passing test. |
| 1225 // --------------------------------------------------------------------------- | 1239 // --------------------------------------------------------------------------- |
| 1226 | 1240 |
| 1227 class MessageTest { | 1241 class MessageTest { |
| 1228 static const List list1 = const ["Hello", "World", "Hello", 0xfffffffffff]; | 1242 static const List list1 = const ["Hello", "World", "Hello", 0xfffffffffff]; |
| 1229 static const List list2 = const [null, list1, list1, list1, list1]; | 1243 static const List list2 = const [null, list1, list1, list1, list1]; |
| 1230 static const List list3 = const [list2, 2.0, true, false, 0xfffffffffff]; | 1244 static const List list3 = const [list2, 2.0, true, false, 0xfffffffffff]; |
| 1231 static const Map map1 = const { | 1245 static const Map map1 = const { |
| 1232 "a=1": 1, | 1246 "a=1" : 1, "b=2" : 2, "c=3" : 3, |
| 1233 "b=2": 2, | |
| 1234 "c=3": 3, | |
| 1235 }; | 1247 }; |
| 1236 static const Map map2 = const { | 1248 static const Map map2 = const { |
| 1237 "list1": list1, | 1249 "list1" : list1, "list2" : list2, "list3" : list3, |
| 1238 "list2": list2, | |
| 1239 "list3": list3, | |
| 1240 }; | 1250 }; |
| 1241 static const List list4 = const [map1, map2]; | 1251 static const List list4 = const [map1, map2]; |
| 1242 static const List elms = const [ | 1252 static const List elms = const [ |
| 1243 list1, | 1253 list1, list2, list3, list4, |
| 1244 list2, | |
| 1245 list3, | |
| 1246 list4, | |
| 1247 ]; | 1254 ]; |
| 1248 | 1255 |
| 1249 static void VerifyMap(Map expected, Map actual) { | 1256 static void VerifyMap(Map expected, Map actual) { |
| 1250 Expect.equals(true, expected is Map); | 1257 Expect.equals(true, expected is Map); |
| 1251 Expect.equals(true, actual is Map); | 1258 Expect.equals(true, actual is Map); |
| 1252 Expect.equals(expected.length, actual.length); | 1259 Expect.equals(expected.length, actual.length); |
| 1253 testForEachMap(key, value) { | 1260 testForEachMap(key, value) { |
| 1254 if (value is List) { | 1261 if (value is List) { |
| 1255 VerifyList(value, actual[key]); | 1262 VerifyList(value, actual[key]); |
| 1256 } else { | 1263 } else { |
| 1257 Expect.equals(value, actual[key]); | 1264 Expect.equals(value, actual[key]); |
| 1258 } | 1265 } |
| 1259 } | 1266 } |
| 1260 | |
| 1261 expected.forEach(testForEachMap); | 1267 expected.forEach(testForEachMap); |
| 1262 } | 1268 } |
| 1263 | 1269 |
| 1264 static void VerifyList(List expected, List actual) { | 1270 static void VerifyList(List expected, List actual) { |
| 1265 for (int i = 0; i < expected.length; i++) { | 1271 for (int i = 0; i < expected.length; i++) { |
| 1266 if (expected[i] is List) { | 1272 if (expected[i] is List) { |
| 1267 VerifyList(expected[i], actual[i]); | 1273 VerifyList(expected[i], actual[i]); |
| 1268 } else if (expected[i] is Map) { | 1274 } else if (expected[i] is Map) { |
| 1269 VerifyMap(expected[i], actual[i]); | 1275 VerifyMap(expected[i], actual[i]); |
| 1270 } else { | 1276 } else { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1307 // Send objects and receive them back. | 1313 // Send objects and receive them back. |
| 1308 for (int i = 0; i < MessageTest.elms.length; i++) { | 1314 for (int i = 0; i < MessageTest.elms.length; i++) { |
| 1309 var sentObject = MessageTest.elms[i]; | 1315 var sentObject = MessageTest.elms[i]; |
| 1310 remote.call(sentObject).then(expectAsync1((var receivedObject) { | 1316 remote.call(sentObject).then(expectAsync1((var receivedObject) { |
| 1311 MessageTest.VerifyObject(i, receivedObject); | 1317 MessageTest.VerifyObject(i, receivedObject); |
| 1312 })); | 1318 })); |
| 1313 } | 1319 } |
| 1314 | 1320 |
| 1315 // Send recursive objects and receive them back. | 1321 // Send recursive objects and receive them back. |
| 1316 List local_list1 = ["Hello", "World", "Hello", 0xffffffffff]; | 1322 List local_list1 = ["Hello", "World", "Hello", 0xffffffffff]; |
| 1317 List local_list2 = [null, local_list1, local_list1]; | 1323 List local_list2 = [null, local_list1, local_list1 ]; |
| 1318 List local_list3 = [local_list2, 2.0, true, false, 0xffffffffff]; | 1324 List local_list3 = [local_list2, 2.0, true, false, 0xffffffffff]; |
| 1319 List sendObject = new List(5); | 1325 List sendObject = new List(5); |
| 1320 sendObject[0] = local_list1; | 1326 sendObject[0] = local_list1; |
| 1321 sendObject[1] = sendObject; | 1327 sendObject[1] = sendObject; |
| 1322 sendObject[2] = local_list2; | 1328 sendObject[2] = local_list2; |
| 1323 sendObject[3] = sendObject; | 1329 sendObject[3] = sendObject; |
| 1324 sendObject[4] = local_list3; | 1330 sendObject[4] = local_list3; |
| 1325 remote.call(sendObject).then((var replyObject) { | 1331 remote.call(sendObject).then((var replyObject) { |
| 1326 Expect.equals(true, sendObject is List); | 1332 Expect.equals(true, sendObject is List); |
| 1327 Expect.equals(true, replyObject is List); | 1333 Expect.equals(true, replyObject is List); |
| 1328 Expect.equals(sendObject.length, replyObject.length); | 1334 Expect.equals(sendObject.length, replyObject.length); |
| 1329 Expect.equals(true, identical(replyObject[1], replyObject)); | 1335 Expect.equals(true, identical(replyObject[1], replyObject)); |
| 1330 Expect.equals(true, identical(replyObject[3], replyObject)); | 1336 Expect.equals(true, identical(replyObject[3], replyObject)); |
| 1331 Expect.equals(true, identical(replyObject[0], replyObject[2][1])); | 1337 Expect.equals(true, identical(replyObject[0], replyObject[2][1])); |
| 1332 Expect.equals(true, identical(replyObject[0], replyObject[2][2])); | 1338 Expect.equals(true, identical(replyObject[0], replyObject[2][2])); |
| 1333 Expect.equals(true, identical(replyObject[2], replyObject[4][0])); | 1339 Expect.equals(true, identical(replyObject[2], replyObject[4][0])); |
| 1334 Expect.equals(true, identical(replyObject[0][0], replyObject[0][2])); | 1340 Expect.equals(true, identical(replyObject[0][0], replyObject[0][2])); |
| 1335 // Bigint literals are not canonicalized so do a == check. | 1341 // Bigint literals are not canonicalized so do a == check. |
| 1336 Expect.equals(true, replyObject[0][3] == replyObject[4][4]); | 1342 Expect.equals(true, replyObject[0][3] == replyObject[4][4]); |
| 1337 }); | 1343 }); |
| 1338 | 1344 |
| 1339 // Shutdown the MessageServer. | 1345 // Shutdown the MessageServer. |
| 1340 remote.call(-1).then(expectAsync1((int message) { | 1346 remote.call(-1).then(expectAsync1((int message) { |
| 1341 Expect.equals(MessageTest.elms.length + 1, message); | 1347 Expect.equals(MessageTest.elms.length + 1, message); |
| 1342 })); | 1348 })); |
| 1343 }); | 1349 }); |
| 1344 } | 1350 } |
| 1345 | 1351 |
| 1346 // --------------------------------------------------------------------------- | 1352 // --------------------------------------------------------------------------- |
| 1347 // tests/isolate/request_reply_test.dart | 1353 // tests/isolate/request_reply_test.dart |
| 1348 // --------------------------------------------------------------------------- | 1354 // --------------------------------------------------------------------------- |
| 1349 | 1355 |
| 1350 void request_reply_entry() { | 1356 void request_reply_entry() { |
| 1351 port.receive((message, SendPort replyTo) { | 1357 port.receive((message, SendPort replyTo) { |
| 1352 replyTo.send(message + 87); | 1358 replyTo.send(message + 87); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1409 Expect.equals((count - 1) * 2, message); | 1415 Expect.equals((count - 1) * 2, message); |
| 1410 remote.send(count++, reply); | 1416 remote.send(count++, reply); |
| 1411 if (count == 10) { | 1417 if (count == 10) { |
| 1412 remote.send(-1, reply); | 1418 remote.send(-1, reply); |
| 1413 } | 1419 } |
| 1414 }, 11)); | 1420 }, 11)); |
| 1415 remote.send(count++, reply); | 1421 remote.send(count++, reply); |
| 1416 }); | 1422 }); |
| 1417 } | 1423 } |
| 1418 | 1424 |
| 1425 |
| 1419 // --------------------------------------------------------------------------- | 1426 // --------------------------------------------------------------------------- |
| 1420 // tests/isolate/mandel_isolate_test.dart | 1427 // tests/isolate/mandel_isolate_test.dart |
| 1421 // --------------------------------------------------------------------------- | 1428 // --------------------------------------------------------------------------- |
| 1422 | 1429 |
| 1423 const TERMINATION_MESSAGE = -1; | 1430 const TERMINATION_MESSAGE = -1; |
| 1424 const N = 100; | 1431 const N = 100; |
| 1425 const ISOLATES = 20; | 1432 const ISOLATES = 20; |
| 1426 | 1433 |
| 1427 mandel_main() { | 1434 mandel_main() { |
| 1428 test("Render Mandelbrot in parallel", () { | 1435 test("Render Mandelbrot in parallel", () { |
| 1429 final state = new MandelbrotState(); | 1436 final state = new MandelbrotState(); |
| 1430 state._validated.future.then(expectAsync1((result) { | 1437 state._validated.future.then(expectAsync1((result) { |
| 1431 expect(result, isTrue); | 1438 expect(result, isTrue); |
| 1432 })); | 1439 })); |
| 1433 for (int i = 0; i < Math.min(ISOLATES, N); i++) state.startClient(i); | 1440 for (int i = 0; i < Math.min(ISOLATES, N); i++) state.startClient(i); |
| 1434 }); | 1441 }); |
| 1435 } | 1442 } |
| 1436 | 1443 |
| 1444 |
| 1437 class MandelbrotState { | 1445 class MandelbrotState { |
| 1446 |
| 1438 MandelbrotState() { | 1447 MandelbrotState() { |
| 1439 _result = new List<List<int>>(N); | 1448 _result = new List<List<int>>(N); |
| 1440 _lineProcessedBy = new List<LineProcessorClient>(N); | 1449 _lineProcessedBy = new List<LineProcessorClient>(N); |
| 1441 _sent = 0; | 1450 _sent = 0; |
| 1442 _missing = N; | 1451 _missing = N; |
| 1443 _validated = new Completer<bool>(); | 1452 _validated = new Completer<bool>(); |
| 1444 } | 1453 } |
| 1445 | 1454 |
| 1446 void startClient(int id) { | 1455 void startClient(int id) { |
| 1447 assert(_sent < N); | 1456 assert(_sent < N); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1485 // print(output); | 1494 // print(output); |
| 1486 } | 1495 } |
| 1487 | 1496 |
| 1488 List<List<int>> _result; | 1497 List<List<int>> _result; |
| 1489 List<LineProcessorClient> _lineProcessedBy; | 1498 List<LineProcessorClient> _lineProcessedBy; |
| 1490 int _sent; | 1499 int _sent; |
| 1491 int _missing; | 1500 int _missing; |
| 1492 Completer<bool> _validated; | 1501 Completer<bool> _validated; |
| 1493 } | 1502 } |
| 1494 | 1503 |
| 1504 |
| 1495 class LineProcessorClient { | 1505 class LineProcessorClient { |
| 1506 |
| 1496 LineProcessorClient(MandelbrotState this._state, int this._id) { | 1507 LineProcessorClient(MandelbrotState this._state, int this._id) { |
| 1497 _port = spawnFunction(processLines); | 1508 _port = spawnFunction(processLines); |
| 1498 } | 1509 } |
| 1499 | 1510 |
| 1500 void processLine(int y) { | 1511 void processLine(int y) { |
| 1501 _port.call(y).then((List<int> message) { | 1512 _port.call(y).then((List<int> message) { |
| 1502 _state.notifyProcessedLine(this, y, message); | 1513 _state.notifyProcessedLine(this, y, message); |
| 1503 }); | 1514 }); |
| 1504 } | 1515 } |
| 1505 | 1516 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1542 void processLines() { | 1553 void processLines() { |
| 1543 port.receive((message, SendPort replyTo) { | 1554 port.receive((message, SendPort replyTo) { |
| 1544 if (message == TERMINATION_MESSAGE) { | 1555 if (message == TERMINATION_MESSAGE) { |
| 1545 assert(replyTo == null); | 1556 assert(replyTo == null); |
| 1546 port.close(); | 1557 port.close(); |
| 1547 } else { | 1558 } else { |
| 1548 replyTo.send(processLine(message), null); | 1559 replyTo.send(processLine(message), null); |
| 1549 } | 1560 } |
| 1550 }); | 1561 }); |
| 1551 } | 1562 } |
| OLD | NEW |