Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Migrate Benchmarks #1538

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
9 changes: 9 additions & 0 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
49 changes: 24 additions & 25 deletions benchmark/centrality.jl
Original file line number Diff line number Diff line change
@@ -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]
15 changes: 3 additions & 12 deletions benchmark/connectivity.jl
Original file line number Diff line number Diff line change
@@ -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]
41 changes: 20 additions & 21 deletions benchmark/core.jl
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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]
19 changes: 10 additions & 9 deletions benchmark/edges.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
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
return t
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
Expand All @@ -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)
8 changes: 4 additions & 4 deletions benchmark/insertions.jl
Original file line number Diff line number Diff line change
@@ -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)
94 changes: 39 additions & 55 deletions benchmark/parallel/egonets.jl
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 6 additions & 0 deletions benchmark/parallel/parallel_benchmarks.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@show Threads.nthreads()

suite["parallel"] = BenchmarkGroup()

# include all benchmarks
include("egonets.jl")
25 changes: 11 additions & 14 deletions benchmark/traversals.jl
Original file line number Diff line number Diff line change
@@ -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]