diff --git a/benchmark/benchmarks.jl b/benchmark/benchmarks.jl index 6b9d8c4c5..bf33e8851 100644 --- a/benchmark/benchmarks.jl +++ b/benchmark/benchmarks.jl @@ -15,8 +15,17 @@ GRAPHS = Dict{String,Graph}( suite = BenchmarkGroup() + +# include all benchmarks include("core.jl") +include("centrality.jl") +include("connectivity.jl") +include("edges.jl") +include("insertions.jl") +include("traversals.jl") +# include parallel benchmarks +include("parallel/parallel_benchmarks.jl") tune!(suite); results = run(suite, verbose = true, seconds = 10) diff --git a/benchmark/centrality.jl b/benchmark/centrality.jl index f52403704..636d993c2 100644 --- a/benchmark/centrality.jl +++ b/benchmark/centrality.jl @@ -1,26 +1,25 @@ +suite["centrality"] = BenchmarkGroup(["degree", "closeness", "betweenness", "katz", "pagerank"]) -@benchgroup "centrality" begin - @benchgroup "graphs" begin - for (name, g) in GRAPHS - @bench "$(name): degree" LightGraphs.degree_centrality($g) - @bench "$(name): closeness" LightGraphs.closeness_centrality($g) - if nv(g) < 1000 - @bench "$(name): betweenness" LightGraphs.betweenness_centrality($g) - @bench "$(name): katz" LightGraphs.katz_centrality($g) - end - end - end #graphs - @benchgroup "digraphs" begin - for (name, g) in DIGRAPHS - @bench "$(name): degree" LightGraphs.degree_centrality($g) - @bench "$(name): closeness" LightGraphs.closeness_centrality($g) - if nv(g) < 1000 - @bench "$(name): betweenness" LightGraphs.betweenness_centrality($g) - @bench "$(name): katz" LightGraphs.katz_centrality($g) - end - if nv(g) < 500 - @bench "$(name): pagerank" LightGraphs.pagerank($g) - end - end - end # digraphs -end # centrality +# betweenness +suite["centrality"]["degree"] = BenchmarkGroup(["graphs", "digraphs"]) +suite["centrality"]["degree"]["graphs"] = @benchmarkable [LightGraphs.degree_centrality(g) for (n,g) in $GRAPHS] +suite["centrality"]["degree"]["digraphs"] = @benchmarkable [LightGraphs.degree_centrality(g) for (n,g) in $DIGRAPHS] + +# closeness +suite["centrality"]["closeness"] = BenchmarkGroup(["graphs", "digraphs"]) +suite["centrality"]["closeness"]["graphs"] = @benchmarkable [LightGraphs.closeness_centrality(g) for (n,g) in $GRAPHS] +suite["centrality"]["closeness"]["digraphs"] = @benchmarkable [LightGraphs.closeness_centrality(g) for (n,g) in $DIGRAPHS] + +# betweenness +suite["centrality"]["betweenness"] = BenchmarkGroup(["graphs", "digraphs"]) +suite["centrality"]["betweenness"]["graphs"] = @benchmarkable [LightGraphs.betweenness_centrality(g) for (n,g) in $GRAPHS if nv(g) < 1000] +suite["centrality"]["betweenness"]["digraphs"] = @benchmarkable [LightGraphs.betweenness_centrality(g) for (n,g) in $DIGRAPHS if nv(g) < 1000] + +# katz +suite["centrality"]["katz"] = BenchmarkGroup(["graphs", "digraphs"]) +suite["centrality"]["katz"]["graphs"] = @benchmarkable [LightGraphs.katz_centrality(g) for (n,g) in $GRAPHS if nv(g) < 1000] +suite["centrality"]["katz"]["digraphs"] = @benchmarkable [LightGraphs.katz_centrality(g) for (n,g) in $DIGRAPHS if nv(g) < 1000] + +#pagerank +suite["centrality"]["katz"] = BenchmarkGroup(["digraphs"]) +suite["centrality"]["katz"]["digraphs"] = @benchmarkable [LightGraphs.pagerank(g) for (n,g) in $DIGRAPHS if nv(g) < 500] diff --git a/benchmark/connectivity.jl b/benchmark/connectivity.jl index 111b20c8e..c9b2eadea 100644 --- a/benchmark/connectivity.jl +++ b/benchmark/connectivity.jl @@ -1,12 +1,3 @@ -@benchgroup "connectivity" begin - @benchgroup "digraphs" begin - for (name, g) in DIGRAPHS - @bench "$(name): strongly_connected_components" LightGraphs.strongly_connected_components($g) - end - end # digraphs - @benchgroup "graphs" begin - for (name, g) in GRAPHS - @bench "$(name): connected_components" LightGraphs.connected_components($g) - end - end # graphs -end # connectivity +suite["connectivity"] = BenchmarkGroup(["graphs", "digraphs"]) +suite["connectivity"]["graphs"] = @benchmarkable [LightGraphs.connected_components(g) for (n,g) in $GRAPHS] +suite["connectivity"]["digraphs"] = @benchmarkable [LightGraphs.strongly_connected_components(g) for (n,g) in $DIGRAPHS] diff --git a/benchmark/core.jl b/benchmark/core.jl index 10cfceb59..edaa01298 100644 --- a/benchmark/core.jl +++ b/benchmark/core.jl @@ -1,12 +1,18 @@ suite["core"] = BenchmarkGroup(["nv", "edges", "has_edge"]) +function all_has_edge(g::AbstractGraph) + nvg = nv(g) + srcs = rand([1:nvg;], cld(nvg, 4)) + dsts = rand([1:nvg;], cld(nvg, 4)) + i = 0 + for (s, d) in zip(srcs, dsts) + if has_edge(g, s, d) + i += 1 + end + end + return i +end -# nv -suite["core"]["nv"] = BenchmarkGroup(["graphs", "digraphs"]) -suite["core"]["nv"]["graphs"] = @benchmarkable [nv(g) for (n,g) in $GRAPHS] -suite["core"]["nv"]["digraphs"] = @benchmarkable [nv(g) for (n,g) in $DIGRAPHS] - -# iterate edges function iter_edges(g::AbstractGraph) i = 0 for e in edges(g) @@ -15,24 +21,17 @@ function iter_edges(g::AbstractGraph) return i end +# nv +suite["core"]["nv"] = BenchmarkGroup(["graphs", "digraphs"]) +suite["core"]["nv"]["graphs"] = @benchmarkable [nv(g) for (n,g) in $GRAPHS] +suite["core"]["nv"]["digraphs"] = @benchmarkable [nv(g) for (n,g) in $DIGRAPHS] + +# iter edges suite["core"]["edges"] = BenchmarkGroup(["graphs", "digraphs"]) suite["core"]["edges"]["graphs"] = @benchmarkable [iter_edges(g) for (n,g) in $GRAPHS] suite["core"]["edges"]["digraphs"] = @benchmarkable [iter_edges(g) for (n,g) in $DIGRAPHS] # has edge -function all_has_edge(g::AbstractGraph) - nvg = nv(g) - srcs = rand([1:nvg;], cld(nvg, 4)) - dsts = rand([1:nvg;], cld(nvg, 4)) - i = 0 - for (s, d) in zip(srcs, dsts) - if has_edge(g, s, d) - i += 1 - end - end - return i -end - suite["core"]["has_edge"] = BenchmarkGroup(["graphs", "digraphs"]) -suite["core"]["has_edge"]["graphs"] = @benchmark [all_has_edge(g) for (n,g) in $GRAPHS] -suite["core"]["has_edge"]["digraphs"] = @benchmark [all_has_edge(g) for (n,g) in $DIGRAPHS] +suite["core"]["has_edge"]["graphs"] = @benchmarkable [all_has_edge(g) for (n,g) in $GRAPHS] +suite["core"]["has_edge"]["digraphs"] = @benchmarkable [all_has_edge(g) for (n,g) in $DIGRAPHS] diff --git a/benchmark/edges.jl b/benchmark/edges.jl index 066ea8ecd..94a925155 100644 --- a/benchmark/edges.jl +++ b/benchmark/edges.jl @@ -1,11 +1,13 @@ import Base: convert +suite["edges"] = BenchmarkGroup(["fille", "fillp", "tsume", "tsump"]) + const P = Pair{Int,Int} convert(::Type{Tuple}, e::Pair) = (e.first, e.second) function fille(n) - t = Array{LightGraphs.Edge,1}(n) + t = Array{LightGraphs.Edge,1}(undef, n) for i in 1:n t[i] = LightGraphs.Edge(i, i + 1) end @@ -13,7 +15,7 @@ function fille(n) end function fillp(n) - t = Array{P,1}(n) + t = Array{P,1}(undef, n) for i in 1:n t[i] = P(i, i + 1) end @@ -30,11 +32,10 @@ function tsum(t) return x end + n = 10000 -@benchgroup "edges" begin - @bench "$(n): fille" fille($n) - @bench "$(n): fillp" fillp($n) - a, b = fille(n), fillp(n) - @bench "$(n): tsume" tsum($a) - @bench "$(n): tsump" tsum($b) -end # edges +suite["edges"]["fille"] = @benchmarkable fille($n) +suite["edges"]["fillp"] = @benchmarkable fillp($n) +a, b = fille(n), fillp(n) +suite["edges"]["tsume"] = @benchmarkable tsum($a) +suite["edges"]["tsump"] = @benchmarkable tsum($b) diff --git a/benchmark/insertions.jl b/benchmark/insertions.jl index b81437886..3a14390ca 100644 --- a/benchmark/insertions.jl +++ b/benchmark/insertions.jl @@ -1,4 +1,4 @@ -@benchgroup "insertions" begin - n = 10000 - @bench "ER Generation" g = SimpleGraph($n, 16 * $n) -end +suite["insertions"] = BenchmarkGroup(["ER Generation"]) + +n = 10000 +suite["insertions"]["ER Generation"] = @benchmarkable SimpleGraph($n, 16 * $n) diff --git a/benchmark/parallel/egonets.jl b/benchmark/parallel/egonets.jl index e9f93e0d5..7115a3831 100644 --- a/benchmark/parallel/egonets.jl +++ b/benchmark/parallel/egonets.jl @@ -1,63 +1,47 @@ -using LightGraphs -using BenchmarkTools -@show Threads.nthreads() +suite["parallel"]["egonets"] = BenchmarkGroup(["vertex_function", "twohop", "singlethread_vertex_function", "singlethread_twohop"]) -@benchgroup "parallel" begin - @benchgroup "egonet" begin - function vertex_function(g::Graph, i::Int) - a = 0 - for u in neighbors(g, i) - a += degree(g, u) - end - return a - end - - function twohop(g::Graph, i::Int) - a = 0 - for u in neighbors(g, i) - for v in neighbors(g, u) - a += degree(g, v) - end - end - return a - end - - - function mapvertices(f, g::Graph) - n = nv(g) - a = zeros(Int, n) - Threads.@threads for i in 1:n - a[i] = f(g, i) - end - return a - end +function vertex_function(g::Graph, i::Int) + a = 0 + for u in neighbors(g, i) + a += degree(g, u) + end + return a +end - function mapvertices_single(f, g) - n = nv(g) - a = zeros(Int, n) - for i in 1:n - a[i] = f(g, i) - end - return a +function twohop(g::Graph, i::Int) + a = 0 + for u in neighbors(g, i) + for v in neighbors(g, u) + a += degree(g, v) end + end + return a +end - function comparison(f, g) - println("Mulithreaded on $(Threads.nthreads())") - b1 = @benchmark mapvertices($f, $g) - println(b1) - - println("singlethreaded") - b2 = @benchmark mapvertices_single($f, $g) - println(b2) - println("done") - end - nv_ = 10000 - g = SimpleGraph(nv_, 64 * nv_) - f = vertex_function - println(g) +function mapvertices(f, g::Graph) + n = nv(g) + a = zeros(Int, n) + Threads.@threads for i in 1:n + a[i] = f(g, i) + end + return a +end - comparison(vertex_function, g) - comparison(twohop, g) +function mapvertices_single(f, g) + n = nv(g) + a = zeros(Int, n) + for i in 1:n + a[i] = f(g, i) end + return a end + +nv_ = 10000 +g = SimpleGraph(nv_, 64 * nv_) +f = vertex_function + +suite["parallel"]["egonets"]["singlethread_vertex_function"] = @benchmarkable mapvertices_single(vertex_function, $g) +suite["parallel"]["egonets"]["singlethread_twohop"] = @benchmarkable mapvertices_single(twohop, $g) +suite["parallel"]["egonets"]["vertex_function"] = @benchmarkable mapvertices(vertex_function, $g) +suite["parallel"]["egonets"]["twohop"] = @benchmarkable mapvertices(twohop, $g) diff --git a/benchmark/parallel/parallel_benchmarks.jl b/benchmark/parallel/parallel_benchmarks.jl new file mode 100644 index 000000000..5a0055df0 --- /dev/null +++ b/benchmark/parallel/parallel_benchmarks.jl @@ -0,0 +1,6 @@ +@show Threads.nthreads() + +suite["parallel"] = BenchmarkGroup() + +# include all benchmarks +include("egonets.jl") diff --git a/benchmark/traversals.jl b/benchmark/traversals.jl index cfd510711..833eb10a6 100644 --- a/benchmark/traversals.jl +++ b/benchmark/traversals.jl @@ -1,14 +1,11 @@ -@benchgroup "traversals" begin - @benchgroup "graphs" begin - for (name, g) in GRAPHS - @bench "$(name): bfs_tree" LightGraphs.bfs_tree($g, 1) - @bench "$(name): dfs_tree" LightGraphs.dfs_tree($g, 1) - end - end # graphs - @benchgroup "digraphs" begin - for (name, g) in DIGRAPHS - @bench "$(name): bfs_tree" LightGraphs.bfs_tree($g, 1) - @bench "$(name): dfs_tree" LightGraphs.dfs_tree($g, 1) - end - end # digraphs -end # traversals +suite["traversals"] = BenchmarkGroup(["bfs", "dfs"]) + +# breadth first +suite["traversals"]["bfs"] = BenchmarkGroup(["graphs", "digraphs"]) +suite["traversals"]["bfs"]["graphs"] = @benchmarkable [LightGraphs.bfs_tree(g, 1) for (n,g) in $GRAPHS] +suite["traversals"]["bfs"]["digraphs"] = @benchmarkable [LightGraphs.bfs_tree(g, 1) for (n,g) in $DIGRAPHS] + +# depth first +suite["traversals"]["dfs"] = BenchmarkGroup(["graphs", "digraphs"]) +suite["traversals"]["dfs"]["graphs"] = @benchmarkable [LightGraphs.dfs_tree(g, 1) for (n,g) in $GRAPHS] +suite["traversals"]["dfs"]["digraphs"] = @benchmarkable [LightGraphs.dfs_tree(g, 1) for (n,g) in $DIGRAPHS]