123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802 |
- package goquery
- import (
- "testing"
- )
- func BenchmarkFind(b *testing.B) {
- var n int
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = DocB().Find("dd").Length()
- } else {
- DocB().Find("dd")
- }
- }
- if n != 41 {
- b.Fatalf("want 41, got %d", n)
- }
- }
- func BenchmarkFindWithinSelection(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("ul")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.Find("a[class]").Length()
- } else {
- sel.Find("a[class]")
- }
- }
- if n != 39 {
- b.Fatalf("want 39, got %d", n)
- }
- }
- func BenchmarkFindSelection(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("ul")
- sel2 := DocW().Find("span")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.FindSelection(sel2).Length()
- } else {
- sel.FindSelection(sel2)
- }
- }
- if n != 73 {
- b.Fatalf("want 73, got %d", n)
- }
- }
- func BenchmarkFindNodes(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("ul")
- sel2 := DocW().Find("span")
- nodes := sel2.Nodes
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.FindNodes(nodes...).Length()
- } else {
- sel.FindNodes(nodes...)
- }
- }
- if n != 73 {
- b.Fatalf("want 73, got %d", n)
- }
- }
- func BenchmarkContents(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find(".toclevel-1")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.Contents().Length()
- } else {
- sel.Contents()
- }
- }
- if n != 16 {
- b.Fatalf("want 16, got %d", n)
- }
- }
- func BenchmarkContentsFiltered(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find(".toclevel-1")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.ContentsFiltered("a[href=\"#Examples\"]").Length()
- } else {
- sel.ContentsFiltered("a[href=\"#Examples\"]")
- }
- }
- if n != 1 {
- b.Fatalf("want 1, got %d", n)
- }
- }
- func BenchmarkChildren(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find(".toclevel-2")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.Children().Length()
- } else {
- sel.Children()
- }
- }
- if n != 2 {
- b.Fatalf("want 2, got %d", n)
- }
- }
- func BenchmarkChildrenFiltered(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("h3")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.ChildrenFiltered(".editsection").Length()
- } else {
- sel.ChildrenFiltered(".editsection")
- }
- }
- if n != 2 {
- b.Fatalf("want 2, got %d", n)
- }
- }
- func BenchmarkParent(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("li")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.Parent().Length()
- } else {
- sel.Parent()
- }
- }
- if n != 55 {
- b.Fatalf("want 55, got %d", n)
- }
- }
- func BenchmarkParentFiltered(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("li")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.ParentFiltered("ul[id]").Length()
- } else {
- sel.ParentFiltered("ul[id]")
- }
- }
- if n != 4 {
- b.Fatalf("want 4, got %d", n)
- }
- }
- func BenchmarkParents(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("th a")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.Parents().Length()
- } else {
- sel.Parents()
- }
- }
- if n != 73 {
- b.Fatalf("want 73, got %d", n)
- }
- }
- func BenchmarkParentsFiltered(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("th a")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.ParentsFiltered("tr").Length()
- } else {
- sel.ParentsFiltered("tr")
- }
- }
- if n != 18 {
- b.Fatalf("want 18, got %d", n)
- }
- }
- func BenchmarkParentsUntil(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("th a")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.ParentsUntil("table").Length()
- } else {
- sel.ParentsUntil("table")
- }
- }
- if n != 52 {
- b.Fatalf("want 52, got %d", n)
- }
- }
- func BenchmarkParentsUntilSelection(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("th a")
- sel2 := DocW().Find("#content")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.ParentsUntilSelection(sel2).Length()
- } else {
- sel.ParentsUntilSelection(sel2)
- }
- }
- if n != 70 {
- b.Fatalf("want 70, got %d", n)
- }
- }
- func BenchmarkParentsUntilNodes(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("th a")
- sel2 := DocW().Find("#content")
- nodes := sel2.Nodes
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.ParentsUntilNodes(nodes...).Length()
- } else {
- sel.ParentsUntilNodes(nodes...)
- }
- }
- if n != 70 {
- b.Fatalf("want 70, got %d", n)
- }
- }
- func BenchmarkParentsFilteredUntil(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find(".toclevel-1 a")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.ParentsFilteredUntil(":nth-child(1)", "ul").Length()
- } else {
- sel.ParentsFilteredUntil(":nth-child(1)", "ul")
- }
- }
- if n != 2 {
- b.Fatalf("want 2, got %d", n)
- }
- }
- func BenchmarkParentsFilteredUntilSelection(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find(".toclevel-1 a")
- sel2 := DocW().Find("ul")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.ParentsFilteredUntilSelection(":nth-child(1)", sel2).Length()
- } else {
- sel.ParentsFilteredUntilSelection(":nth-child(1)", sel2)
- }
- }
- if n != 2 {
- b.Fatalf("want 2, got %d", n)
- }
- }
- func BenchmarkParentsFilteredUntilNodes(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find(".toclevel-1 a")
- sel2 := DocW().Find("ul")
- nodes := sel2.Nodes
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.ParentsFilteredUntilNodes(":nth-child(1)", nodes...).Length()
- } else {
- sel.ParentsFilteredUntilNodes(":nth-child(1)", nodes...)
- }
- }
- if n != 2 {
- b.Fatalf("want 2, got %d", n)
- }
- }
- func BenchmarkSiblings(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("ul li:nth-child(1)")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.Siblings().Length()
- } else {
- sel.Siblings()
- }
- }
- if n != 293 {
- b.Fatalf("want 293, got %d", n)
- }
- }
- func BenchmarkSiblingsFiltered(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("ul li:nth-child(1)")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.SiblingsFiltered("[class]").Length()
- } else {
- sel.SiblingsFiltered("[class]")
- }
- }
- if n != 46 {
- b.Fatalf("want 46, got %d", n)
- }
- }
- func BenchmarkNext(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("li:nth-child(1)")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.Next().Length()
- } else {
- sel.Next()
- }
- }
- if n != 49 {
- b.Fatalf("want 49, got %d", n)
- }
- }
- func BenchmarkNextFiltered(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("li:nth-child(1)")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.NextFiltered("[class]").Length()
- } else {
- sel.NextFiltered("[class]")
- }
- }
- if n != 6 {
- b.Fatalf("want 6, got %d", n)
- }
- }
- func BenchmarkNextAll(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("li:nth-child(3)")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.NextAll().Length()
- } else {
- sel.NextAll()
- }
- }
- if n != 234 {
- b.Fatalf("want 234, got %d", n)
- }
- }
- func BenchmarkNextAllFiltered(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("li:nth-child(3)")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.NextAllFiltered("[class]").Length()
- } else {
- sel.NextAllFiltered("[class]")
- }
- }
- if n != 33 {
- b.Fatalf("want 33, got %d", n)
- }
- }
- func BenchmarkPrev(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("li:last-child")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.Prev().Length()
- } else {
- sel.Prev()
- }
- }
- if n != 49 {
- b.Fatalf("want 49, got %d", n)
- }
- }
- func BenchmarkPrevFiltered(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("li:last-child")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.PrevFiltered("[class]").Length()
- } else {
- sel.PrevFiltered("[class]")
- }
- }
- // There is one more Prev li with a class, compared to Next li with a class
- // (confirmed by looking at the HTML, this is ok)
- if n != 7 {
- b.Fatalf("want 7, got %d", n)
- }
- }
- func BenchmarkPrevAll(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("li:nth-child(4)")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.PrevAll().Length()
- } else {
- sel.PrevAll()
- }
- }
- if n != 78 {
- b.Fatalf("want 78, got %d", n)
- }
- }
- func BenchmarkPrevAllFiltered(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("li:nth-child(4)")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.PrevAllFiltered("[class]").Length()
- } else {
- sel.PrevAllFiltered("[class]")
- }
- }
- if n != 6 {
- b.Fatalf("want 6, got %d", n)
- }
- }
- func BenchmarkNextUntil(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("li:first-child")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.NextUntil(":nth-child(4)").Length()
- } else {
- sel.NextUntil(":nth-child(4)")
- }
- }
- if n != 84 {
- b.Fatalf("want 84, got %d", n)
- }
- }
- func BenchmarkNextUntilSelection(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("h2")
- sel2 := DocW().Find("ul")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.NextUntilSelection(sel2).Length()
- } else {
- sel.NextUntilSelection(sel2)
- }
- }
- if n != 42 {
- b.Fatalf("want 42, got %d", n)
- }
- }
- func BenchmarkNextUntilNodes(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("h2")
- sel2 := DocW().Find("p")
- nodes := sel2.Nodes
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.NextUntilNodes(nodes...).Length()
- } else {
- sel.NextUntilNodes(nodes...)
- }
- }
- if n != 12 {
- b.Fatalf("want 12, got %d", n)
- }
- }
- func BenchmarkPrevUntil(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("li:last-child")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.PrevUntil(":nth-child(4)").Length()
- } else {
- sel.PrevUntil(":nth-child(4)")
- }
- }
- if n != 238 {
- b.Fatalf("want 238, got %d", n)
- }
- }
- func BenchmarkPrevUntilSelection(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("h2")
- sel2 := DocW().Find("ul")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.PrevUntilSelection(sel2).Length()
- } else {
- sel.PrevUntilSelection(sel2)
- }
- }
- if n != 49 {
- b.Fatalf("want 49, got %d", n)
- }
- }
- func BenchmarkPrevUntilNodes(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("h2")
- sel2 := DocW().Find("p")
- nodes := sel2.Nodes
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.PrevUntilNodes(nodes...).Length()
- } else {
- sel.PrevUntilNodes(nodes...)
- }
- }
- if n != 11 {
- b.Fatalf("want 11, got %d", n)
- }
- }
- func BenchmarkNextFilteredUntil(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("h2")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.NextFilteredUntil("p", "div").Length()
- } else {
- sel.NextFilteredUntil("p", "div")
- }
- }
- if n != 22 {
- b.Fatalf("want 22, got %d", n)
- }
- }
- func BenchmarkNextFilteredUntilSelection(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("h2")
- sel2 := DocW().Find("div")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.NextFilteredUntilSelection("p", sel2).Length()
- } else {
- sel.NextFilteredUntilSelection("p", sel2)
- }
- }
- if n != 22 {
- b.Fatalf("want 22, got %d", n)
- }
- }
- func BenchmarkNextFilteredUntilNodes(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("h2")
- sel2 := DocW().Find("div")
- nodes := sel2.Nodes
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.NextFilteredUntilNodes("p", nodes...).Length()
- } else {
- sel.NextFilteredUntilNodes("p", nodes...)
- }
- }
- if n != 22 {
- b.Fatalf("want 22, got %d", n)
- }
- }
- func BenchmarkPrevFilteredUntil(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("h2")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.PrevFilteredUntil("p", "div").Length()
- } else {
- sel.PrevFilteredUntil("p", "div")
- }
- }
- if n != 20 {
- b.Fatalf("want 20, got %d", n)
- }
- }
- func BenchmarkPrevFilteredUntilSelection(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("h2")
- sel2 := DocW().Find("div")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.PrevFilteredUntilSelection("p", sel2).Length()
- } else {
- sel.PrevFilteredUntilSelection("p", sel2)
- }
- }
- if n != 20 {
- b.Fatalf("want 20, got %d", n)
- }
- }
- func BenchmarkPrevFilteredUntilNodes(b *testing.B) {
- var n int
- b.StopTimer()
- sel := DocW().Find("h2")
- sel2 := DocW().Find("div")
- nodes := sel2.Nodes
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.PrevFilteredUntilNodes("p", nodes...).Length()
- } else {
- sel.PrevFilteredUntilNodes("p", nodes...)
- }
- }
- if n != 20 {
- b.Fatalf("want 20, got %d", n)
- }
- }
- func BenchmarkClosest(b *testing.B) {
- var n int
- b.StopTimer()
- sel := Doc().Find(".container-fluid")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.Closest(".pvk-content").Length()
- } else {
- sel.Closest(".pvk-content")
- }
- }
- if n != 2 {
- b.Fatalf("want 2, got %d", n)
- }
- }
- func BenchmarkClosestSelection(b *testing.B) {
- var n int
- b.StopTimer()
- sel := Doc().Find(".container-fluid")
- sel2 := Doc().Find(".pvk-content")
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.ClosestSelection(sel2).Length()
- } else {
- sel.ClosestSelection(sel2)
- }
- }
- if n != 2 {
- b.Fatalf("want 2, got %d", n)
- }
- }
- func BenchmarkClosestNodes(b *testing.B) {
- var n int
- b.StopTimer()
- sel := Doc().Find(".container-fluid")
- nodes := Doc().Find(".pvk-content").Nodes
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- if n == 0 {
- n = sel.ClosestNodes(nodes...).Length()
- } else {
- sel.ClosestNodes(nodes...)
- }
- }
- if n != 2 {
- b.Fatalf("want 2, got %d", n)
- }
- }
|