# -*- tcl -*-
# Tcl Benchmark File
#
# This file contains a number of benchmarks for XPath query speed.
# This allow developers to monitor/gauge/track package performance.
#
# (c) 2006 Rolf Ade <rolf@pointsman.de>
#
# $Id$


# ### ### ### ######### ######### ######### ###########################
## Setting up the environment ...

package require tdom 

# ### ### ### ######### ######### ######### ###########################
## Benchmarks.

bench -desc "not() function - not cached" -pre {
    set doc [dom parse <root/>]
} -body {
    $doc selectNodes not(foo)
}

bench -desc "not() function - cached" -pre {
    set doc [dom parse <root/>]
} -body {
    $doc selectNodes -cache 1 not(foo)
}

bench -desc "path /foo/bar/baz  - not cached" -pre {
    set doc [dom parse <root/>]
} -body {
    $doc selectNodes /foo/bar/baz
}

bench -desc "path /foo/bar/baz - cached" -pre {
    set doc [dom parse <root/>]
} -body {
    $doc selectNodes -cache 1 /foo/bar/baz
}

bench -desc "path with predicate  - not cached" -pre {
    set doc [dom parse <root/>]
} -body {
    $doc selectNodes {/foo/bar/baz[not(grill) and @some='foo']}
}

bench -desc "path with predicate - cached" -pre {
    set doc [dom parse <root/>]
} -body {
    $doc selectNodes -cache 1 {/foo/bar/baz[not(grill) and @some='foo']}
}


namespace eval ::dom {
    namespace eval  xpathFunc {
    }
}

proc ::dom::xpathFunc::myfunc {args} {
    return "foo"
}

dom createNodeCmd elementNode e1

foreach nrOf {1 10 20 30 40 50 60} {

    bench -desc "Tcl scripted XPath func with $nrOf nodes in the current nodelist" -pre {
        dom createDocument root doc
        $doc documentElement root
        $root appendFromScript {
            for {set x 0} {$x < $nrOf} {incr x} {
                e1
            }
        }
    } -body {
        $root selectNodes {e1[myfunc() = '']}
    } -post {
        $doc delete
    }

}

foreach nrOf {1 10 50 100} {

    bench -desc "string(<nodeset with $nrOf nodes>)" -pre {
        dom createDocument root doc
        $doc documentElement root
        $root appendFromScript {
            for {set x 0} {$x < $nrOf} {incr x} {
                e1
            }
        }
        $root selectNodes -cache 1 string(e1)
    } -body {
        $root selectNodes -cache 1 string(e1)
    } -post {
        $doc delete
    }

}

foreach nrOf {1 10 50 100} {

    bench -desc "count(<nodeset with $nrOf nodes>)" -pre {
        dom createDocument root doc
        $doc documentElement root
        $root appendFromScript {
            for {set x 0} {$x < $nrOf} {incr x} {
                e1
            }
        }
        $root selectNodes -cache 1 count(e1)
    } -body {
        $root selectNodes -cache 1 count(e1)
    } -post {
        $doc delete
    }

}

dom createNodeCmd elementNode e2

dom createDocument root doc
$doc documentElement root
$root appendFromScript {
    e2
    for {set x 1} {$x < 1000} {incr x} {
        e1
    }
}

bench -desc "Select unique element between 1000, at first, with pred" -body {
    $root selectNodes -cache 1 {e2[1]}
} -post {
    $doc delete
}

bench -desc "Select unique element between 1000, at first, without pred" -pre {
    dom createDocument root doc
    $doc documentElement root
    $root appendFromScript {
        e2
        for {set x 1} {$x < 1000} {incr x} {
            e1
        }
    }
} -body {
    $root selectNodes -cache 1 {e2}
} -post {
    $doc delete
}

bench -desc "Select unique element between 1000, at first, with last() pred" -pre {
    dom createDocument root doc
    $doc documentElement root
    $root appendFromScript {
        e2
        for {set x 1} {$x < 1000} {incr x} {
            e1
        }
    }
} -body {
    $root selectNodes -cache 1 {e2[last()]}
} -post {
    $doc delete
}

bench -desc "Select unique element between 1000, at 500, with pred" -pre {
    dom createDocument root doc
    $doc documentElement root
    $root appendFromScript {
        for {set x 1} {$x < 500} {incr x} {
            e1
        }
        e2
        for {set x 501} {$x <= 1000} {incr x} {
            e1
        }
    }
} -body {
    $root selectNodes -cache 1 {e2[1]}
} -post {
    $doc delete
}

bench -desc "Select unique element between 1000, at 500, without pred" -pre {
    dom createDocument root doc
    $doc documentElement root
    $root appendFromScript {
        for {set x 1} {$x < 500} {incr x} {
            e1
        }
        e2
        for {set x 501} {$x <= 1000} {incr x} {
            e1
        }
    }
} -body {
    $root selectNodes -cache 1 {e2}
} -post {
    $doc delete
}

bench -desc "Select unique element between 1000, at 500, with last()" -pre {
    dom createDocument root doc
    $doc documentElement root
    $root appendFromScript {
        for {set x 1} {$x < 500} {incr x} {
            e1
        }
        e2
        for {set x 501} {$x <= 1000} {incr x} {
            e1
        }
    }
} -body {
    $root selectNodes -cache 1 {e2[last()]}
} -post {
    $doc delete
}

bench -desc "Select unique element between 1000, at 1000, with pred" -pre {
    dom createDocument root doc
    $doc documentElement root
    $root appendFromScript {
        for {set x 1} {$x < 1000} {incr x} {
            e1
        }
        e2
    }
} -body {
    $root selectNodes -cache 1 {e2[1]}
} -post {
    $doc delete
}

bench -desc "Select unique element between 1000, at 1000, without pred" -pre {
    dom createDocument root doc
    $doc documentElement root
    $root appendFromScript {
        for {set x 1} {$x < 1000} {incr x} {
            e1
        }
        e2
    }
} -body {
    $root selectNodes -cache 1 {e2}
} -post {
    $doc delete
}

bench -desc "Select unique element between 1000, at 1000, with last()" -pre {
    dom createDocument root doc
    $doc documentElement root
    $root appendFromScript {
        for {set x 1} {$x < 1000} {incr x} {
            e1
        }
        e2
    }
} -body {
    $root selectNodes -cache 1 {e2[last()]}
} -post {
    $doc delete
}
