Skip to content

Commit

Permalink
made BenchPlugin more extensible to support custom CSS files
Browse files Browse the repository at this point in the history
  • Loading branch information
ennerf committed Sep 26, 2023
1 parent e841751 commit 58aa95e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
30 changes: 15 additions & 15 deletions chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,18 +292,6 @@ protected void redrawCanvas() {
public void setGlobalRecorder(MeasurementRecorder recorder) {
setRecorder(recorder);
int i = 0;
for (Axis axis : getAxes()) {
if (axis == getXAxis()) {
axis.setRecorder(recorder.addPrefix("x"));
} else if (axis == getYAxis()) {
axis.setRecorder(recorder.addPrefix("y"));
} else {
axis.setRecorder(recorder.addPrefix("axis" + i));
}
i++;
}
i = 0;
gridRenderer.setRecorder(recorder);
for (var renderer : getRenderers()) {
var p = recorder.addPrefix("renderer" + i);
renderer.setRecorder(p);
Expand All @@ -315,6 +303,18 @@ public void setGlobalRecorder(MeasurementRecorder recorder) {
}
i++;
}
gridRenderer.setRecorder(recorder);
i = 0;

Check warning on line 307 in chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java

View check run for this annotation

Codecov / codecov/patch

chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java#L306-L307

Added lines #L306 - L307 were not covered by tests
for (Axis axis : getAxes()) {
if (axis == getXAxis()) {
axis.setRecorder(recorder.addPrefix("x"));

Check warning on line 310 in chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java

View check run for this annotation

Codecov / codecov/patch

chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java#L310

Added line #L310 was not covered by tests
} else if (axis == getYAxis()) {
axis.setRecorder(recorder.addPrefix("y"));

Check warning on line 312 in chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java

View check run for this annotation

Codecov / codecov/patch

chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java#L312

Added line #L312 was not covered by tests
} else {
axis.setRecorder(recorder.addPrefix("axis" + i));

Check warning on line 314 in chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java

View check run for this annotation

Codecov / codecov/patch

chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java#L314

Added line #L314 was not covered by tests
}
i++;
}

Check warning on line 317 in chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java

View check run for this annotation

Codecov / codecov/patch

chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java#L316-L317

Added lines #L316 - L317 were not covered by tests
i = 0;
for (ChartPlugin plugin : getPlugins()) {
plugin.setRecorder(recorder.addPrefix("plugin" + i++));
Expand All @@ -323,11 +323,11 @@ public void setGlobalRecorder(MeasurementRecorder recorder) {

@Override
public void setRecorder(MeasurementRecorder recorder) {
super.setRecorder(recorder);
benchDrawGrid = recorder.newDuration("xychart-drawGrid");
benchDrawData = recorder.newDuration("xychart-drawData");
benchDrawGrid = recorder.newDuration("xychart-drawGrid");
super.setRecorder(recorder);

Check warning on line 328 in chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java

View check run for this annotation

Codecov / codecov/patch

chartfx-chart/src/main/java/io/fair_acc/chartfx/XYChart.java#L327-L328

Added lines #L327 - L328 were not covered by tests
}

private DurationMeasure benchDrawGrid = DurationMeasure.DISABLED;
private DurationMeasure benchDrawData = DurationMeasure.DISABLED;
private DurationMeasure benchDrawGrid = DurationMeasure.DISABLED;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import java.util.Optional;
import java.util.function.UnaryOperator;

import io.fair_acc.chartfx.Chart;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
Expand Down Expand Up @@ -32,12 +35,12 @@ public class BenchPlugin extends ChartPlugin {
private static final int FONT_SIZE = 22;
private static final String ICON_ENABLE_BENCH = "fa-hourglass-start:" + FONT_SIZE;
private static final String ICON_DISABLE_BENCH = "fa-hourglass-end:" + FONT_SIZE;
private final BooleanProperty enabled = new SimpleBooleanProperty(false);
protected final BooleanProperty enabled = new SimpleBooleanProperty(false);

Check warning on line 38 in chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java

View check run for this annotation

Codecov / codecov/patch

chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java#L38

Added line #L38 was not covered by tests
private final HBox buttons = createButtonBar();
private UnaryOperator<MeasurementRecorder> measurementFilter = rec -> rec.atLevel(BenchLevel.Info).contains("draw");
private final Stage stage = new Stage();
protected final Stage stage = new Stage();

Check warning on line 41 in chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java

View check run for this annotation

Codecov / codecov/patch

chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java#L41

Added line #L41 was not covered by tests

public HBox createButtonBar() {
protected HBox createButtonBar() {
final Button enableBtn = new Button(null, new FontIcon(ICON_ENABLE_BENCH));
enableBtn.setPadding(new Insets(3, 3, 3, 3));
enableBtn.setTooltip(new Tooltip("starts displaying live benchmark stats"));
Expand All @@ -47,7 +50,7 @@ public HBox createButtonBar() {
disableBtn.setTooltip(new Tooltip("stops displaying live benchmark stats"));

FXUtils.bindManagedToVisible(enableBtn).bind(enabled.not());
enableBtn.setOnAction(this::enable);
enableBtn.setOnAction(evt -> enable());

Check warning on line 53 in chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java

View check run for this annotation

Codecov / codecov/patch

chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java#L53

Added line #L53 was not covered by tests
FXUtils.bindManagedToVisible(disableBtn).bind(enabled);
disableBtn.setOnAction(evt -> disable());

Expand Down Expand Up @@ -81,25 +84,27 @@ public BenchPlugin setFilter(UnaryOperator<MeasurementRecorder> measurementFilte
return this;
}

private void enable(ActionEvent event) {
public void enable() {
if (!enabled.get() && getChart() != null && getChart() instanceof XYChart) {
XYChart chart = (XYChart) getChart();
String title = Optional.ofNullable(chart.getTitle())
.filter(string -> !string.isEmpty())
.orElse("Benchmark");
LiveDisplayRecorder recorder = LiveDisplayRecorder.createChart(title, pane -> {
Scene scene = new Scene(pane);
scene.getStylesheets().addAll(chart.getScene().getStylesheets());
stage.setScene(scene);
resetRecorder = () -> chart.setGlobalRecorder(MeasurementRecorder.DISABLED);
stage.show();
});
MeasurementRecorder recorder = createRecorder(chart);

Check warning on line 90 in chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java

View check run for this annotation

Codecov / codecov/patch

chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java#L90

Added line #L90 was not covered by tests
chart.setGlobalRecorder(measurementFilter.apply(recorder));
resetRecorder = () -> chart.setGlobalRecorder(MeasurementRecorder.DISABLED);

Check warning on line 92 in chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java

View check run for this annotation

Codecov / codecov/patch

chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java#L92

Added line #L92 was not covered by tests
enabled.set(true);
}
}

private void disable() {
protected MeasurementRecorder createRecorder(XYChart chart) {
String title = Optional.ofNullable(chart.getTitle())

Check warning on line 98 in chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java

View check run for this annotation

Codecov / codecov/patch

chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java#L98

Added line #L98 was not covered by tests
.filter(string -> !string.isEmpty())
.orElse("Benchmark");
return LiveDisplayRecorder.createChart(title, pane -> {
stage.setScene(new Scene(pane));
stage.show();
});

Check warning on line 104 in chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java

View check run for this annotation

Codecov / codecov/patch

chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java#L100-L104

Added lines #L100 - L104 were not covered by tests
}

public void disable() {
if (enabled.get()) {
enabled.set(false);
resetRecorder.run();
Expand All @@ -108,6 +113,7 @@ private void disable() {
}
}

private static final Runnable NO_OP = () -> {};
private static final Runnable NO_OP = () -> {
};

Check warning on line 117 in chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java

View check run for this annotation

Codecov / codecov/patch

chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/BenchPlugin.java#L116-L117

Added lines #L116 - L117 were not covered by tests
private Runnable resetRecorder = NO_OP;
}

0 comments on commit 58aa95e

Please sign in to comment.