Skip to content

Commit

Permalink
made BenchPlugin more extensible to support custom CSS files (#623)
Browse files Browse the repository at this point in the history
  • Loading branch information
ennerf authored Sep 26, 2023
1 parent e841751 commit 77a2a8c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 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;
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;
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);
}

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 @@ -5,6 +5,8 @@

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 All @@ -17,6 +19,7 @@

import io.fair_acc.bench.BenchLevel;
import io.fair_acc.bench.MeasurementRecorder;
import io.fair_acc.chartfx.Chart;
import io.fair_acc.chartfx.XYChart;
import io.fair_acc.chartfx.bench.LiveDisplayRecorder;
import io.fair_acc.chartfx.utils.FXUtils;
Expand All @@ -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);
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();

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());
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);
chart.setGlobalRecorder(measurementFilter.apply(recorder));
resetRecorder = () -> chart.setGlobalRecorder(MeasurementRecorder.DISABLED);
enabled.set(true);
}
}

private void disable() {
protected MeasurementRecorder createRecorder(XYChart chart) {
String title = Optional.ofNullable(chart.getTitle())
.filter(string -> !string.isEmpty())
.orElse("Benchmark");
return LiveDisplayRecorder.createChart(title, pane -> {
stage.setScene(new Scene(pane));
stage.show();
});
}

public void disable() {
if (enabled.get()) {
enabled.set(false);
resetRecorder.run();
Expand Down

0 comments on commit 77a2a8c

Please sign in to comment.