-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench.mojo
More file actions
236 lines (182 loc) · 8.35 KB
/
bench.mojo
File metadata and controls
236 lines (182 loc) · 8.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
"""
https://github.com/krausest/js-framework-benchmark/blob/master/frameworks/non-keyed/vue/src/App.vue
"""
from benchmark import (
Bench,
BenchConfig,
Bencher,
BenchId,
ThroughputMeasure,
BenchMetric,
Format,
)
from utils import IndexList
from gpu.host import DeviceContext
from pathlib import Path
from random import random_si64
from reactpy_vdom import Element, diff
fn row(label: String) -> Element:
return Element("tr", {}, [
Element("td", {"class": "col-md-1"}, ""),
Element("td", {"class": "col-md-4"}, [
Element("a", {"class": "lbl"}, [
Element("span", {}, label)
])
]),
Element("td", {"class": "col-md-1"}, [
Element("a", {"class": "remove"}, [
Element("span", {"class": "glyphicon glyphicon-remove", "aria-hidden": "true"}, [])
])
]),
Element("td", {"class": "col-md-6"}, ""),
])
fn create_rows(n_rows: Int) -> Element:
return Element("table", {"class": "table table-hover table-striped test-data"}, [
Element("tbody", {}, [row("Row " + String(i)) for i in range(n_rows)])
])
# fn get_gbs_measure(input: String) raises -> ThroughputMeasure:
# return ThroughputMeasure(BenchMetric.bytes, input.byte_length())
fn get_throughput_elems(n: Int) -> ThroughputMeasure:
return ThroughputMeasure(BenchMetric.elements, n)
# fn run[
# func: fn (mut Bencher, Int) capturing, name: String
# ](mut m: Bench, data: String) raises:
# m.bench_with_input[Int, func](BenchId(name), data, get_gbs_measure(data))
fn main() raises:
var config = BenchConfig(num_repetitions=1)
config.verbose_timing = True
config.flush_denormals = True
config.show_progress = True
var m = Bench(config)
m.bench_with_input[Int, bench_create_rows]("create_rows[1]", 1, [get_throughput_elems(1)])
m.bench_with_input[Int, bench_create_rows]("create_rows[10]", 10, [get_throughput_elems(10)])
m.bench_with_input[Int, bench_create_rows]("create_rows[100]", 100, [get_throughput_elems(100)])
m.bench_with_input[Int, bench_create_rows]("create_rows[1000]", 1000, [get_throughput_elems(1000)])
m.bench_with_input[Int, bench_create_rows]("create_rows[10000]", 10_000, [get_throughput_elems(10_000)])
m.bench_with_input[Int, bench_diff_append_rows]("diff_append_row[1000+1]", 1, [get_throughput_elems(1001)])
m.bench_with_input[Int, bench_diff_append_rows]("diff_append_row[1000+10]", 10, [get_throughput_elems(1010)])
m.bench_with_input[Int, bench_diff_append_rows]("diff_append_row[1000+100]", 100, [get_throughput_elems(1100)])
m.bench_with_input[Int, bench_diff_append_rows]("diff_append_row[1000+1000]", 1000, [get_throughput_elems(2000)])
m.bench_with_input[Int, bench_diff_append_rows]("diff_append_row[1000+10000]", 10_000, [get_throughput_elems(11_000)])
m.bench_with_input[Int, bench_diff_clear_rows]("diff_clear_rows[1]", 1, [get_throughput_elems(1001)])
m.bench_with_input[Int, bench_diff_clear_rows]("diff_clear_rows[10]", 10, [get_throughput_elems(1010)])
m.bench_with_input[Int, bench_diff_clear_rows]("diff_clear_rows[100]", 100, [get_throughput_elems(1100)])
m.bench_with_input[Int, bench_diff_clear_rows]("diff_clear_rows[1000]", 1000, [get_throughput_elems(2000)])
m.bench_with_input[Int, bench_diff_clear_rows]("diff_clear_rows[10000]", 10_000, [get_throughput_elems(11_000)])
m.bench_with_input[Int, bench_diff_select_row]("diff_select_row[1]", 1, [get_throughput_elems(1)])
m.bench_with_input[Int, bench_diff_select_row]("diff_select_row[10]", 10, [get_throughput_elems(10)])
m.bench_with_input[Int, bench_diff_select_row]("diff_select_row[100]", 100, [get_throughput_elems(100)])
m.bench_with_input[Int, bench_diff_select_row]("diff_select_row[1000]", 1000, [get_throughput_elems(1000)])
m.bench_with_input[Int, bench_diff_select_row]("diff_select_row[10000]", 10_000, [get_throughput_elems(10_000)])
m.bench_with_input[Int, bench_diff_partial_update]("diff_partial_update[100]", 100, [get_throughput_elems(100)])
m.bench_with_input[Int, bench_diff_partial_update]("diff_partial_update[1000]", 1_000, [get_throughput_elems(1_000)])
m.bench_with_input[Int, bench_diff_partial_update]("diff_partial_update[10000]", 10_000, [get_throughput_elems(10_000)])
m.bench_with_input[Int, bench_diff_swap_rows]("diff_swap_rows[10]", 10, [get_throughput_elems(10)])
m.bench_with_input[Int, bench_diff_swap_rows]("diff_swap_rows[100]", 100, [get_throughput_elems(100)])
m.bench_with_input[Int, bench_diff_swap_rows]("diff_swap_rows[1000]", 1000, [get_throughput_elems(1000)])
m.bench_with_input[Int, bench_diff_swap_rows]("diff_swap_rows[10000]", 10_000, [get_throughput_elems(10_000)])
# FIXME: ABORT: Failed to load libpython from
# m.bench_with_input[Int, bench_patches_to_python]("bench_patches_to_python[1]", 1, [get_throughput_elems(1)])
# m.bench_with_input[Int, bench_patches_to_python]("bench_patches_to_python[10]", 10, [get_throughput_elems(10)])
# m.bench_with_input[Int, bench_patches_to_python]("bench_patches_to_python[100]", 100, [get_throughput_elems(100)])
# m.bench_with_input[Int, bench_patches_to_python]("bench_patches_to_python[1000]", 1000, [get_throughput_elems(1000)])
# FIXME: error: failed to materialize top-level module (from emberjson import JSON)
# m.bench_with_input[Int, bench_patches_to_json_string]("patches_to_json_string[1]", 1, [get_throughput_elems(1)])
# m.bench_with_input[Int, bench_patches_to_json_string]("patches_to_json_string[10]", 10, [get_throughput_elems(10)])
# m.bench_with_input[Int, bench_patches_to_json_string]("patches_to_json_string[100]", 100, [get_throughput_elems(100)])
# m.bench_with_input[Int, bench_patches_to_json_string]("patches_to_json_string[100]", 1000, [get_throughput_elems(1000)])
# Pretty print in table format
print()
print(m)
@parameter
fn bench_create_rows(mut b: Bencher, n_rows: Int) raises:
"""Creating N rows."""
@always_inline
@parameter
fn do():
_ = create_rows(n_rows)
b.iter[do]()
@parameter
fn bench_diff_append_rows(mut b: Bencher, n_rows: Int) raises:
"""Diff for appending N rows."""
src = create_rows(1000)
dst = create_rows(1000 + n_rows)
@always_inline
@parameter
fn do():
_ = diff(src, dst)
b.iter[do]()
@parameter
fn bench_diff_clear_rows(mut b: Bencher, n_rows: Int) raises:
"""Diff for clearing N rows."""
src = create_rows(n_rows)
dst = create_rows(0)
@always_inline
@parameter
fn do():
_ = diff(src, dst)
b.iter[do]()
@parameter
fn bench_diff_select_row(mut b: Bencher, n_rows: Int) raises:
"""Diff to select a row."""
src = create_rows(n_rows)
dst = create_rows(n_rows)
i = Int(random_si64(0, n_rows - 1))
ref row = dst.get([0, i])
row.attributes["class"] = "selected"
@always_inline
@parameter
fn do():
_ = diff(src, dst)
b.iter[do]()
@parameter
fn bench_diff_partial_update(mut b: Bencher, n_rows: Int) raises:
"""Diff to update text every 10th row."""
src = create_rows(n_rows)
dst = create_rows(n_rows)
for i in range(0, n_rows, 10):
ref row = dst.get([0, i, 1, 0, 0])
row.attributes["text"] = "this row is: " + String(i)
@always_inline
@parameter
fn do():
_ = diff(src, dst)
b.iter[do]()
@parameter
fn bench_diff_swap_rows(mut b: Bencher, n_rows: Int) raises:
"""Diff to swap 2 rows."""
src = create_rows(n_rows)
dst = create_rows(n_rows)
i = 1
j = n_rows - 2
row_j = dst.remove([0, j]).value()
row_i = dst.remove([0, i]).value()
dst.insert([0, i], row_j)
dst.insert([0, j], row_i)
@always_inline
@parameter
fn do():
_ = diff(src, dst)
b.iter[do]()
@parameter
fn bench_patches_to_python(mut b: Bencher, n_rows: Int) raises:
"""Convert ListPatch to python."""
src = create_rows(0)
dst = create_rows(n_rows)
patches = diff(src, dst)
@always_inline
@parameter
fn do() raises:
_ = patches.python()
b.iter[do]()
# @parameter
# fn bench_patches_to_json_string(mut b: Bencher, n_rows: Int) raises:
# """Convert ListPatch to json string."""
# src = create_rows(0)
# dst = create_rows(n_rows)
# patches = diff(src, dst)
# @always_inline
# @parameter
# fn do():
# _ = patches.json().to_string()
# b.iter[do]()