github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/goquery/bench_traversal_test.go (about) 1 package goquery 2 3 import ( 4 "testing" 5 ) 6 7 func BenchmarkFind(b *testing.B) { 8 var n int 9 10 for i := 0; i < b.N; i++ { 11 if n == 0 { 12 n = DocB().Find("dd").Length() 13 14 } else { 15 DocB().Find("dd") 16 } 17 } 18 b.Logf("Find=%d", n) 19 } 20 21 func BenchmarkFindWithinSelection(b *testing.B) { 22 var n int 23 24 b.StopTimer() 25 sel := DocW().Find("ul") 26 b.StartTimer() 27 for i := 0; i < b.N; i++ { 28 if n == 0 { 29 n = sel.Find("a[class]").Length() 30 } else { 31 sel.Find("a[class]") 32 } 33 } 34 b.Logf("FindWithinSelection=%d", n) 35 } 36 37 func BenchmarkFindSelection(b *testing.B) { 38 var n int 39 40 b.StopTimer() 41 sel := DocW().Find("ul") 42 sel2 := DocW().Find("span") 43 b.StartTimer() 44 for i := 0; i < b.N; i++ { 45 if n == 0 { 46 n = sel.FindSelection(sel2).Length() 47 } else { 48 sel.FindSelection(sel2) 49 } 50 } 51 b.Logf("FindSelection=%d", n) 52 } 53 54 func BenchmarkFindNodes(b *testing.B) { 55 var n int 56 57 b.StopTimer() 58 sel := DocW().Find("ul") 59 sel2 := DocW().Find("span") 60 nodes := sel2.Nodes 61 b.StartTimer() 62 for i := 0; i < b.N; i++ { 63 if n == 0 { 64 n = sel.FindNodes(nodes...).Length() 65 } else { 66 sel.FindNodes(nodes...) 67 } 68 } 69 b.Logf("FindNodes=%d", n) 70 } 71 72 func BenchmarkContents(b *testing.B) { 73 var n int 74 75 b.StopTimer() 76 sel := DocW().Find(".toclevel-1") 77 b.StartTimer() 78 for i := 0; i < b.N; i++ { 79 if n == 0 { 80 n = sel.Contents().Length() 81 } else { 82 sel.Contents() 83 } 84 } 85 b.Logf("Contents=%d", n) 86 } 87 88 func BenchmarkContentsFiltered(b *testing.B) { 89 var n int 90 91 b.StopTimer() 92 sel := DocW().Find(".toclevel-1") 93 b.StartTimer() 94 for i := 0; i < b.N; i++ { 95 if n == 0 { 96 n = sel.ContentsFiltered("a[href=\"#Examples\"]").Length() 97 } else { 98 sel.ContentsFiltered("a[href=\"#Examples\"]") 99 } 100 } 101 b.Logf("ContentsFiltered=%d", n) 102 } 103 104 func BenchmarkChildren(b *testing.B) { 105 var n int 106 107 b.StopTimer() 108 sel := DocW().Find(".toclevel-2") 109 b.StartTimer() 110 for i := 0; i < b.N; i++ { 111 if n == 0 { 112 n = sel.Children().Length() 113 } else { 114 sel.Children() 115 } 116 } 117 b.Logf("Children=%d", n) 118 } 119 120 func BenchmarkChildrenFiltered(b *testing.B) { 121 var n int 122 123 b.StopTimer() 124 sel := DocW().Find("h3") 125 b.StartTimer() 126 for i := 0; i < b.N; i++ { 127 if n == 0 { 128 n = sel.ChildrenFiltered(".editsection").Length() 129 } else { 130 sel.ChildrenFiltered(".editsection") 131 } 132 } 133 b.Logf("ChildrenFiltered=%d", n) 134 } 135 136 func BenchmarkParent(b *testing.B) { 137 var n int 138 139 b.StopTimer() 140 sel := DocW().Find("li") 141 b.StartTimer() 142 for i := 0; i < b.N; i++ { 143 if n == 0 { 144 n = sel.Parent().Length() 145 } else { 146 sel.Parent() 147 } 148 } 149 b.Logf("Parent=%d", n) 150 } 151 152 func BenchmarkParentFiltered(b *testing.B) { 153 var n int 154 155 b.StopTimer() 156 sel := DocW().Find("li") 157 b.StartTimer() 158 for i := 0; i < b.N; i++ { 159 if n == 0 { 160 n = sel.ParentFiltered("ul[id]").Length() 161 } else { 162 sel.ParentFiltered("ul[id]") 163 } 164 } 165 b.Logf("ParentFiltered=%d", n) 166 } 167 168 func BenchmarkParents(b *testing.B) { 169 var n int 170 171 b.StopTimer() 172 sel := DocW().Find("th a") 173 b.StartTimer() 174 for i := 0; i < b.N; i++ { 175 if n == 0 { 176 n = sel.Parents().Length() 177 } else { 178 sel.Parents() 179 } 180 } 181 b.Logf("Parents=%d", n) 182 } 183 184 func BenchmarkParentsFiltered(b *testing.B) { 185 var n int 186 187 b.StopTimer() 188 sel := DocW().Find("th a") 189 b.StartTimer() 190 for i := 0; i < b.N; i++ { 191 if n == 0 { 192 n = sel.ParentsFiltered("tr").Length() 193 } else { 194 sel.ParentsFiltered("tr") 195 } 196 } 197 b.Logf("ParentsFiltered=%d", n) 198 } 199 200 func BenchmarkParentsUntil(b *testing.B) { 201 var n int 202 203 b.StopTimer() 204 sel := DocW().Find("th a") 205 b.StartTimer() 206 for i := 0; i < b.N; i++ { 207 if n == 0 { 208 n = sel.ParentsUntil("table").Length() 209 } else { 210 sel.ParentsUntil("table") 211 } 212 } 213 b.Logf("ParentsUntil=%d", n) 214 } 215 216 func BenchmarkParentsUntilSelection(b *testing.B) { 217 var n int 218 219 b.StopTimer() 220 sel := DocW().Find("th a") 221 sel2 := DocW().Find("#content") 222 b.StartTimer() 223 for i := 0; i < b.N; i++ { 224 if n == 0 { 225 n = sel.ParentsUntilSelection(sel2).Length() 226 } else { 227 sel.ParentsUntilSelection(sel2) 228 } 229 } 230 b.Logf("ParentsUntilSelection=%d", n) 231 } 232 233 func BenchmarkParentsUntilNodes(b *testing.B) { 234 var n int 235 236 b.StopTimer() 237 sel := DocW().Find("th a") 238 sel2 := DocW().Find("#content") 239 nodes := sel2.Nodes 240 b.StartTimer() 241 for i := 0; i < b.N; i++ { 242 if n == 0 { 243 n = sel.ParentsUntilNodes(nodes...).Length() 244 } else { 245 sel.ParentsUntilNodes(nodes...) 246 } 247 } 248 b.Logf("ParentsUntilNodes=%d", n) 249 } 250 251 func BenchmarkParentsFilteredUntil(b *testing.B) { 252 var n int 253 254 b.StopTimer() 255 sel := DocW().Find(".toclevel-1 a") 256 b.StartTimer() 257 for i := 0; i < b.N; i++ { 258 if n == 0 { 259 n = sel.ParentsFilteredUntil(":nth-child(1)", "ul").Length() 260 } else { 261 sel.ParentsFilteredUntil(":nth-child(1)", "ul") 262 } 263 } 264 b.Logf("ParentsFilteredUntil=%d", n) 265 } 266 267 func BenchmarkParentsFilteredUntilSelection(b *testing.B) { 268 var n int 269 270 b.StopTimer() 271 sel := DocW().Find(".toclevel-1 a") 272 sel2 := DocW().Find("ul") 273 b.StartTimer() 274 for i := 0; i < b.N; i++ { 275 if n == 0 { 276 n = sel.ParentsFilteredUntilSelection(":nth-child(1)", sel2).Length() 277 } else { 278 sel.ParentsFilteredUntilSelection(":nth-child(1)", sel2) 279 } 280 } 281 b.Logf("ParentsFilteredUntilSelection=%d", n) 282 } 283 284 func BenchmarkParentsFilteredUntilNodes(b *testing.B) { 285 var n int 286 287 b.StopTimer() 288 sel := DocW().Find(".toclevel-1 a") 289 sel2 := DocW().Find("ul") 290 nodes := sel2.Nodes 291 b.StartTimer() 292 for i := 0; i < b.N; i++ { 293 if n == 0 { 294 n = sel.ParentsFilteredUntilNodes(":nth-child(1)", nodes...).Length() 295 } else { 296 sel.ParentsFilteredUntilNodes(":nth-child(1)", nodes...) 297 } 298 } 299 b.Logf("ParentsFilteredUntilNodes=%d", n) 300 } 301 302 func BenchmarkSiblings(b *testing.B) { 303 var n int 304 305 b.StopTimer() 306 sel := DocW().Find("ul li:nth-child(1)") 307 b.StartTimer() 308 for i := 0; i < b.N; i++ { 309 if n == 0 { 310 n = sel.Siblings().Length() 311 } else { 312 sel.Siblings() 313 } 314 } 315 b.Logf("Siblings=%d", n) 316 } 317 318 func BenchmarkSiblingsFiltered(b *testing.B) { 319 var n int 320 321 b.StopTimer() 322 sel := DocW().Find("ul li:nth-child(1)") 323 b.StartTimer() 324 for i := 0; i < b.N; i++ { 325 if n == 0 { 326 n = sel.SiblingsFiltered("[class]").Length() 327 } else { 328 sel.SiblingsFiltered("[class]") 329 } 330 } 331 b.Logf("SiblingsFiltered=%d", n) 332 } 333 334 func BenchmarkNext(b *testing.B) { 335 var n int 336 337 b.StopTimer() 338 sel := DocW().Find("li:nth-child(1)") 339 b.StartTimer() 340 for i := 0; i < b.N; i++ { 341 if n == 0 { 342 n = sel.Next().Length() 343 } else { 344 sel.Next() 345 } 346 } 347 b.Logf("Next=%d", n) 348 } 349 350 func BenchmarkNextFiltered(b *testing.B) { 351 var n int 352 353 b.StopTimer() 354 sel := DocW().Find("li:nth-child(1)") 355 b.StartTimer() 356 for i := 0; i < b.N; i++ { 357 if n == 0 { 358 n = sel.NextFiltered("[class]").Length() 359 } else { 360 sel.NextFiltered("[class]") 361 } 362 } 363 b.Logf("NextFiltered=%d", n) 364 } 365 366 func BenchmarkNextAll(b *testing.B) { 367 var n int 368 369 b.StopTimer() 370 sel := DocW().Find("li:nth-child(3)") 371 b.StartTimer() 372 for i := 0; i < b.N; i++ { 373 if n == 0 { 374 n = sel.NextAll().Length() 375 } else { 376 sel.NextAll() 377 } 378 } 379 b.Logf("NextAll=%d", n) 380 } 381 382 func BenchmarkNextAllFiltered(b *testing.B) { 383 var n int 384 385 b.StopTimer() 386 sel := DocW().Find("li:nth-child(3)") 387 b.StartTimer() 388 for i := 0; i < b.N; i++ { 389 if n == 0 { 390 n = sel.NextAllFiltered("[class]").Length() 391 } else { 392 sel.NextAllFiltered("[class]") 393 } 394 } 395 b.Logf("NextAllFiltered=%d", n) 396 } 397 398 func BenchmarkPrev(b *testing.B) { 399 var n int 400 401 b.StopTimer() 402 sel := DocW().Find("li:last-child") 403 b.StartTimer() 404 for i := 0; i < b.N; i++ { 405 if n == 0 { 406 n = sel.Prev().Length() 407 } else { 408 sel.Prev() 409 } 410 } 411 b.Logf("Prev=%d", n) 412 } 413 414 func BenchmarkPrevFiltered(b *testing.B) { 415 var n int 416 417 b.StopTimer() 418 sel := DocW().Find("li:last-child") 419 b.StartTimer() 420 for i := 0; i < b.N; i++ { 421 if n == 0 { 422 n = sel.PrevFiltered("[class]").Length() 423 } else { 424 sel.PrevFiltered("[class]") 425 } 426 } 427 // There is one more Prev li with a class, compared to Next li with a class 428 // (confirmed by looking at the HTML, this is ok) 429 b.Logf("PrevFiltered=%d", n) 430 } 431 432 func BenchmarkPrevAll(b *testing.B) { 433 var n int 434 435 b.StopTimer() 436 sel := DocW().Find("li:nth-child(4)") 437 b.StartTimer() 438 for i := 0; i < b.N; i++ { 439 if n == 0 { 440 n = sel.PrevAll().Length() 441 } else { 442 sel.PrevAll() 443 } 444 } 445 b.Logf("PrevAll=%d", n) 446 } 447 448 func BenchmarkPrevAllFiltered(b *testing.B) { 449 var n int 450 451 b.StopTimer() 452 sel := DocW().Find("li:nth-child(4)") 453 b.StartTimer() 454 for i := 0; i < b.N; i++ { 455 if n == 0 { 456 n = sel.PrevAllFiltered("[class]").Length() 457 } else { 458 sel.PrevAllFiltered("[class]") 459 } 460 } 461 b.Logf("PrevAllFiltered=%d", n) 462 } 463 464 func BenchmarkNextUntil(b *testing.B) { 465 var n int 466 467 b.StopTimer() 468 sel := DocW().Find("li:first-child") 469 b.StartTimer() 470 for i := 0; i < b.N; i++ { 471 if n == 0 { 472 n = sel.NextUntil(":nth-child(4)").Length() 473 } else { 474 sel.NextUntil(":nth-child(4)") 475 } 476 } 477 b.Logf("NextUntil=%d", n) 478 } 479 480 func BenchmarkNextUntilSelection(b *testing.B) { 481 var n int 482 483 b.StopTimer() 484 sel := DocW().Find("h2") 485 sel2 := DocW().Find("ul") 486 b.StartTimer() 487 for i := 0; i < b.N; i++ { 488 if n == 0 { 489 n = sel.NextUntilSelection(sel2).Length() 490 } else { 491 sel.NextUntilSelection(sel2) 492 } 493 } 494 b.Logf("NextUntilSelection=%d", n) 495 } 496 497 func BenchmarkNextUntilNodes(b *testing.B) { 498 var n int 499 500 b.StopTimer() 501 sel := DocW().Find("h2") 502 sel2 := DocW().Find("p") 503 nodes := sel2.Nodes 504 b.StartTimer() 505 for i := 0; i < b.N; i++ { 506 if n == 0 { 507 n = sel.NextUntilNodes(nodes...).Length() 508 } else { 509 sel.NextUntilNodes(nodes...) 510 } 511 } 512 b.Logf("NextUntilNodes=%d", n) 513 } 514 515 func BenchmarkPrevUntil(b *testing.B) { 516 var n int 517 518 b.StopTimer() 519 sel := DocW().Find("li:last-child") 520 b.StartTimer() 521 for i := 0; i < b.N; i++ { 522 if n == 0 { 523 n = sel.PrevUntil(":nth-child(4)").Length() 524 } else { 525 sel.PrevUntil(":nth-child(4)") 526 } 527 } 528 b.Logf("PrevUntil=%d", n) 529 } 530 531 func BenchmarkPrevUntilSelection(b *testing.B) { 532 var n int 533 534 b.StopTimer() 535 sel := DocW().Find("h2") 536 sel2 := DocW().Find("ul") 537 b.StartTimer() 538 for i := 0; i < b.N; i++ { 539 if n == 0 { 540 n = sel.PrevUntilSelection(sel2).Length() 541 } else { 542 sel.PrevUntilSelection(sel2) 543 } 544 } 545 b.Logf("PrevUntilSelection=%d", n) 546 } 547 548 func BenchmarkPrevUntilNodes(b *testing.B) { 549 var n int 550 551 b.StopTimer() 552 sel := DocW().Find("h2") 553 sel2 := DocW().Find("p") 554 nodes := sel2.Nodes 555 b.StartTimer() 556 for i := 0; i < b.N; i++ { 557 if n == 0 { 558 n = sel.PrevUntilNodes(nodes...).Length() 559 } else { 560 sel.PrevUntilNodes(nodes...) 561 } 562 } 563 b.Logf("PrevUntilNodes=%d", n) 564 } 565 566 func BenchmarkNextFilteredUntil(b *testing.B) { 567 var n int 568 569 b.StopTimer() 570 sel := DocW().Find("h2") 571 b.StartTimer() 572 for i := 0; i < b.N; i++ { 573 if n == 0 { 574 n = sel.NextFilteredUntil("p", "div").Length() 575 } else { 576 sel.NextFilteredUntil("p", "div") 577 } 578 } 579 b.Logf("NextFilteredUntil=%d", n) 580 } 581 582 func BenchmarkNextFilteredUntilSelection(b *testing.B) { 583 var n int 584 585 b.StopTimer() 586 sel := DocW().Find("h2") 587 sel2 := DocW().Find("div") 588 b.StartTimer() 589 for i := 0; i < b.N; i++ { 590 if n == 0 { 591 n = sel.NextFilteredUntilSelection("p", sel2).Length() 592 } else { 593 sel.NextFilteredUntilSelection("p", sel2) 594 } 595 } 596 b.Logf("NextFilteredUntilSelection=%d", n) 597 } 598 599 func BenchmarkNextFilteredUntilNodes(b *testing.B) { 600 var n int 601 602 b.StopTimer() 603 sel := DocW().Find("h2") 604 sel2 := DocW().Find("div") 605 nodes := sel2.Nodes 606 b.StartTimer() 607 for i := 0; i < b.N; i++ { 608 if n == 0 { 609 n = sel.NextFilteredUntilNodes("p", nodes...).Length() 610 } else { 611 sel.NextFilteredUntilNodes("p", nodes...) 612 } 613 } 614 b.Logf("NextFilteredUntilNodes=%d", n) 615 } 616 617 func BenchmarkPrevFilteredUntil(b *testing.B) { 618 var n int 619 620 b.StopTimer() 621 sel := DocW().Find("h2") 622 b.StartTimer() 623 for i := 0; i < b.N; i++ { 624 if n == 0 { 625 n = sel.PrevFilteredUntil("p", "div").Length() 626 } else { 627 sel.PrevFilteredUntil("p", "div") 628 } 629 } 630 b.Logf("PrevFilteredUntil=%d", n) 631 } 632 633 func BenchmarkPrevFilteredUntilSelection(b *testing.B) { 634 var n int 635 636 b.StopTimer() 637 sel := DocW().Find("h2") 638 sel2 := DocW().Find("div") 639 b.StartTimer() 640 for i := 0; i < b.N; i++ { 641 if n == 0 { 642 n = sel.PrevFilteredUntilSelection("p", sel2).Length() 643 } else { 644 sel.PrevFilteredUntilSelection("p", sel2) 645 } 646 } 647 b.Logf("PrevFilteredUntilSelection=%d", n) 648 } 649 650 func BenchmarkPrevFilteredUntilNodes(b *testing.B) { 651 var n int 652 653 b.StopTimer() 654 sel := DocW().Find("h2") 655 sel2 := DocW().Find("div") 656 nodes := sel2.Nodes 657 b.StartTimer() 658 for i := 0; i < b.N; i++ { 659 if n == 0 { 660 n = sel.PrevFilteredUntilNodes("p", nodes...).Length() 661 } else { 662 sel.PrevFilteredUntilNodes("p", nodes...) 663 } 664 } 665 b.Logf("PrevFilteredUntilNodes=%d", n) 666 } 667 668 func BenchmarkClosest(b *testing.B) { 669 var n int 670 671 b.StopTimer() 672 sel := Doc().Find(".container-fluid") 673 b.StartTimer() 674 for i := 0; i < b.N; i++ { 675 if n == 0 { 676 n = sel.Closest(".pvk-content").Length() 677 } else { 678 sel.Closest(".pvk-content") 679 } 680 } 681 b.Logf("Closest=%d", n) 682 } 683 684 func BenchmarkClosestSelection(b *testing.B) { 685 var n int 686 687 b.StopTimer() 688 sel := Doc().Find(".container-fluid") 689 sel2 := Doc().Find(".pvk-content") 690 b.StartTimer() 691 for i := 0; i < b.N; i++ { 692 if n == 0 { 693 n = sel.ClosestSelection(sel2).Length() 694 } else { 695 sel.ClosestSelection(sel2) 696 } 697 } 698 b.Logf("ClosestSelection=%d", n) 699 } 700 701 func BenchmarkClosestNodes(b *testing.B) { 702 var n int 703 704 b.StopTimer() 705 sel := Doc().Find(".container-fluid") 706 nodes := Doc().Find(".pvk-content").Nodes 707 b.StartTimer() 708 for i := 0; i < b.N; i++ { 709 if n == 0 { 710 n = sel.ClosestNodes(nodes...).Length() 711 } else { 712 sel.ClosestNodes(nodes...) 713 } 714 } 715 b.Logf("ClosestNodes=%d", n) 716 }