benchmark-action / github-action-benchmark

GitHub Action for continuous benchmarking to keep performance
https://benchmark-action.github.io/github-action-benchmark/dev/bench/
MIT License
1.02k stars 152 forks source link

Support writing data as JSON but still uploading it #261

Open martincostello opened 3 months ago

martincostello commented 3 months ago

I've written a custom UI on top of the benchmark data where I load the data using fetch() from raw.githubusercontent.com so I can switch between branches in the UI from a single deployed GitHub Pages site (as GitHub Pages does not support multiple branches).

However, as the data is expected to be loaded via a <script> tag as JavaScript, I need to trim the prefix from the file before parsing it as JSON.

const dataUrl = `https://raw.githubusercontent.com/${username}/${repository}/${branch}/${application}/data.js`;
const response = await fetch(dataUrl, { cache: 'no-cache' });
const dataText = await response.text();
const data = JSON.parse(dataText.slice('window.BENCHMARK_DATA = '.length));

I otherwise use the auto-push functionality, so would rather be able to control the format of the written file, rather than need to handle all that that functionality myself by using the external-data-json-path input.

If there were an input option to set the format between JavaScript and JSON, then I could directly fetch the data as JSON:

- uses: benchmark-action/github-action-benchmark@v1
  with:
    output-format: json # Optional, default would be javascript as it is today
const dataUrl = `https://raw.githubusercontent.com/${username}/${repository}/${branch}/${application}/data.json`;
const response = await fetch(dataUrl, { cache: 'no-cache' });
const data = await response.json();

I'd be happy to contribute a PR to add support for this if such a feature would be welcome.