Sitelet https://github.com/phpbench/phpbench/commit/4da0b8279e54f93d760fa078b5f0103c43c555b8
Skip to content

Commit 4da0b82

Browse files
authored
Report Derivations (#1148)
- Derive columns from existing columns. - One or multiple passes. - New "aggregate-preview" report which includes the new "diff" column making use of this feature. - Add documentation - Deprecates the `BASELINE_EXPRESSIONS` option - nl2br in HTML report for description
1 parent e8145c8 commit 4da0b82

14 files changed

Lines changed: 333 additions & 33 deletions

File tree

‎docs/report-generators/expression.rst‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,36 @@ Which yields:
129129
:language: bash
130130
:section: 2
131131

132+
133+
.. _generator_expression_derivations:
134+
135+
Derivations
136+
-----------
137+
138+
:ref:`Expressions<generator_expression_expressions>` effectively enable you to
139+
populate a *table* based on the benchmarking :ref:`data <generator_expression_data>`.
140+
**Derivations** allow you to derive new columns from that table in one or more
141+
passes.
142+
143+
Derivations have access to two variables and each expression is mapped on a row:
144+
145+
- ``table``: a data frame containing the entire table. ``table['mean']`` for example will yield a **list** containing all of the values in the `mean` column.
146+
- ``row``: the current row to which the derivation will be mapped. ``row['mean']`` for example will yield the value of `mean` in the current row.
147+
148+
In the following example we derive a new column ``super mean``:
149+
150+
.. approved:: ../../examples/Command/report-generators-derivations
151+
:language: javascript
152+
:section: 0
153+
154+
Which yields:
155+
156+
.. approved:: ../../examples/Command/report-generators-derivations
157+
:language: bash
158+
:section: 2
159+
160+
.. _generator_expression_data:
161+
132162
Data
133163
----
134164

‎examples/Command/report-generators-column-diff‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
"my-report": {
1212
"generator": "expression",
1313
"aggregate": ["iteration_index"],
14-
"cols": {
15-
"subject": null,
16-
"mode": null,
17-
"diff": "format('%.2fx', mean(result_time_avg) / min(suite['result_time_avg']))"
14+
"cols": [
15+
"subject",
16+
"mode",
17+
"diff"
18+
],
19+
"derivations": {
20+
"diff": "if(min(table['mode']) > 0, format('%.2fx', row['mode'] / min(table['mode'])), 'N/A')"
1821
}
1922
}
2023
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"report.generators": {
3+
"my-report": {
4+
"generator": "expression",
5+
"cols": [ "benchmark", "subject", "mode", "super mode" ],
6+
"derivations": {
7+
"super mode": "row['mode'] ~ ' of ' ~ sum(table['mode'])"
8+
}
9+
}
10+
}
11+
}
12+
---
13+
phpbench run --report=my-report --executor=debug MultipleSubjects.php --progress=none
14+
---
15+
+----------------------+---------------+----------+------------+
16+
| benchmark | subject | mode | super mode |
17+
+----------------------+---------------+----------+------------+
18+
| MultipleSubjectBench | benchSubject1 | 10.000μs | 10 of 30 |
19+
| MultipleSubjectBench | benchSubject2 | 10.000μs | 10 of 30 |
20+
| MultipleSubjectBench | benchSubject3 | 10.000μs | 10 of 30 |
21+
+----------------------+---------------+----------+------------+
22+

‎examples/Command/report-generators-expressions‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
"my-report": {
44
"generator": "expression",
55
"expressions": {
6-
"mode": "\"This is the mode: \" ~ mode(result_time_avg)"
6+
"new_mode": "\"This is the mode: \" ~ mode(result_time_avg)"
77
},
8-
"cols": [ "benchmark", "subject", "mode" ]
8+
"cols": [ "benchmark", "subject", "new_mode" ]
99
}
1010
}
1111
}
1212
---
1313
phpbench run --report=my-report --executor=debug NothingBench.php --progress=none
1414
---
1515
+--------------+--------------+----------------------+
16-
| benchmark | subject | mode |
16+
| benchmark | subject | new_mode |
1717
+--------------+--------------+----------------------+
1818
| NothingBench | benchNothing | This is the mode: 10 |
1919
+--------------+--------------+----------------------+

‎lib/Extension/config/report/generators.php‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
<?php
22

3+
$formatTime = function (string $expr) {
4+
return sprintf(
5+
<<<'EOT'
6+
display_as_time(
7+
%s,
8+
coalesce(
9+
row["time_unit"],
10+
"time"
11+
),
12+
row["time_precision"],
13+
row["time_mode"]
14+
)
15+
EOT
16+
,
17+
$expr
18+
);
19+
};
20+
321
return [
422
'bare' => [
523
'generator' => 'bare',
@@ -21,6 +39,54 @@
2139
'rstdev',
2240
]
2341
],
42+
'aggregate-preview' => [
43+
'title' => 'Aggregate by Subject',
44+
'description' => "Aggregate results by subject and group by benchmark class.\nThis is a preview report and may change in minor version updates.",
45+
'extends' => 'aggregate',
46+
'break' => [
47+
'benchmark',
48+
],
49+
'expressions' => [
50+
'name' => sprintf(
51+
'if(first(variant_name) != "", %s, %s)',
52+
'first(subject_name) ~ " (" ~ first(variant_name) ~ ")"',
53+
'first(subject_name)'
54+
),
55+
'_mode' => 'mode(result_time_avg)',
56+
],
57+
'cols' => [
58+
'name',
59+
'revs',
60+
'its',
61+
'mem_peak',
62+
'mode',
63+
'rstdev',
64+
'diff',
65+
],
66+
'baseline_expressions' => [],
67+
'derivations' => [
68+
'diff' => sprintf(
69+
'if(min(table["_mode"]) > 0, %s, %s)',
70+
'format("%.2fx", row["_mode"] / min(table["_mode"]))',
71+
'"N/A"'
72+
),
73+
'mode' => sprintf(
74+
'if(row["baseline_exists"], %s, %s)',
75+
$formatTime('row["_mode"]') . ' ~ " " ~ percent_diff(row["baseline_mode"], row["_mode"])',
76+
$formatTime('row["_mode"]'),
77+
),
78+
'mem_peak' => sprintf(
79+
'if(row["baseline_exists"], %s, %s)',
80+
'(row["mem_peak"] as memory) ~ " " ~ percent_diff(row["baseline_mem_peak"], row["mem_peak"])',
81+
'row["mem_peak"] as memory',
82+
),
83+
'rstdev' => sprintf(
84+
'if(row["baseline_exists"], %s, %s)',
85+
'(row["rstdev"] as "rstdev") ~ " " ~ percent_diff(row["baseline_rstdev"], row["rstdev"])',
86+
'row["rstdev"] as "rstdev"',
87+
),
88+
]
89+
],
2490
'bar_chart_time' => [
2591
'generator' => 'component',
2692
'components' => [

0 commit comments

Comments
 (0)