Skip to content

Commit

Permalink
Migrate Vector_Operations to the new bench API
Browse files Browse the repository at this point in the history
  • Loading branch information
Akirathan committed Aug 10, 2023
1 parent 910911f commit f4e287d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 39 deletions.
2 changes: 1 addition & 1 deletion test/Benchmarks/src/Main.enso
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import project.Vector.Distinct
import project.Vector.Operations

all_benchmarks =
vec_ops = Operations.all
vec_ops = Operations.collect_benches
vec_distinct = Distinct.collect_benches
[vec_ops, vec_distinct]

Expand Down
112 changes: 74 additions & 38 deletions test/Benchmarks/src/Vector/Operations.enso
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,87 @@ import project.Vector.Utils

polyglot java import java.util.Random as Java_Random

## Bench Utilities ============================================================

vector_size = 1000000
warmup = Phase_Conf.Secs 2
measure = Phase_Conf.Secs 2

# The Benchmarks ==============================================================

collect_benches group_builder =
random_vec = Utils.make_random_vec vector_size
random_vec_2 = Utils.make_random_vec 100000
random_gen = Java_Random.new 123456

bench_measure ~act name = group_builder.specify name act

bench_measure (Base.Vector.new vector_size i->i) "New_Vector"
bench_measure (Base.Vector.new vector_size _->42) "New_Constant"
bench_measure (Base.Vector.new vector_size _->random_gen.nextLong) "New_Random"
bench_measure (Base.Vector.fill vector_size 42) "Fill_Constant"
bench_measure (Base.Vector.fill vector_size random_gen.nextLong) "Fill_Random_constant"
bench_measure (random_vec + [1]) "Append_Single"
bench_measure (random_vec + random_vec_2) "Append_Large"
bench_measure (random_vec.reduce (Math.max)) "Max"
bench_measure (random_vec.reduce (+)) "Sum"
bench_measure ((random_vec.drop (First 20)).reduce (+)) "Drop_First_20_and_Sum"
bench_measure ((random_vec.drop (Last 20)).reduce (+)) "Drop_Last_20_and_Sum"
bench_measure (random_vec.filter (x -> x % 3 == 1)) "Filter"
bench_measure (random_vec.filter_with_index (i-> x-> (i+x) % 3 == 1)) "Filter_With_Index"
bench_measure (random_vec.compute Statistic.Maximum) "Max_Stats"
bench_measure (random_vec.compute Statistic.Sum) "Sum_Stats"
bench_measure (random_vec.compute Statistic.Variance) "Variance_Stats"

bench_measure (random_vec . map (x -> x + random_gen.nextLong) . filter (x -> x % 3 == 1)) "Map_and_Filter"
bench_measure (random_vec.partition (x -> x % 3 == 1)) "Partition"
bench_measure (random_vec.partition_with_index (i-> x-> (i+x) % 3 == 1)) "Partition_With_Index"
type Setup
Value vector_size random_vec random_vec_2 random_gen

setup =
vector_size = 1000000
random_vec = Utils.make_random_vec vector_size
random_vec_2 = Utils.make_random_vec 100000
random_gen = Java_Random.new 123456
Setup.Value vector_size random_vec random_vec_2 random_gen

stateful_fun x =
s = State.get Number
State.put s+x
bench_measure (State.run Number 0 <| random_vec.each stateful_fun) "Each"

options = Bench.options . set_warmup warmup . set_measure measure

all = Bench.build builder->
options = Bench.options . set_warmup (Phase_Conf.Secs 2) . set_measure (Phase_Conf.Secs 2) . set_setup Setup


collect_benches = Bench.build builder->
builder.group "Vector_Operations" options group_builder->
collect_benches group_builder
group_builder.specify "New_Vector" arg->
Base.Vector.new arg.vector_size i->i

group_builder.specify "New_Constant" arg->
Base.Vector.new arg.vector_size _->42

group_builder.specify "New_Random" arg->
Base.Vector.new arg.vector_size _->arg.random_gen.nextLong

group_builder.specify "Fill_Constant" arg->
Base.Vector.fill arg.vector_size 42

group_builder.specify "Fill_Random_constant" arg->
Base.Vector.fill arg.vector_size arg.random_gen.nextLong

group_builder.specify "Append_Single" arg->
arg.random_vec + [1]

group_builder.specify "Append_Large" arg->
arg.random_vec + arg.random_vec_2

group_builder.specify "Max" arg->
arg.random_vec.reduce (Math.max)

group_builder.specify "Sum" arg->
arg.random_vec.reduce (+)

group_builder.specify "Drop_First_20_and_Sum" arg->
(arg.random_vec.drop (First 20)).reduce (+)

group_builder.specify "Drop_Last_20_and_Sum" arg->
(arg.random_vec.drop (Last 20)).reduce (+)

group_builder.specify "Filter" arg->
arg.random_vec.filter (x -> x % 3 == 1)

group_builder.specify "Filter_With_Index" arg->
arg.random_vec.filter_with_index (i-> x-> (i+x) % 3 == 1)

group_builder.specify "Max_Stats" arg->
arg.random_vec.compute Statistic.Maximum

group_builder.specify "Sum_Stats" arg->
arg.random_vec.compute Statistic.Sum

group_builder.specify "Variance_Stats" arg->
arg.random_vec.compute Statistic.Variance

group_builder.specify "Map_and_Filter" arg->
arg.random_vec . map (x -> x + arg.random_gen.nextLong) . filter (x -> x % 3 == 1)

group_builder.specify "Partition" arg->
arg.random_vec.partition (x -> x % 3 == 1)

group_builder.specify "Partition_With_Index" arg->
arg.random_vec.partition_with_index (i-> x-> (i+x) % 3 == 1)

group_builder.specify "Each" arg->
State.run Number 0 <| arg.random_vec.each arg.stateful_fun


main = all . run_main
main = collect_benches . run_main

0 comments on commit f4e287d

Please sign in to comment.