Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Onxy.printMetrics MD, CSV and JSON formats #90

Merged
merged 9 commits into from
Jul 21, 2021

Conversation

kidroca
Copy link
Contributor

@kidroca kidroca commented Jul 16, 2021

@marcaaron

Details

This is a continuation of #89
This PR adds more print options:

  • print data as Markdown tables
  • as csv strings (easy import to Excel)
  • as json

It necessary to have something like this as it aids capturing and saving benchmark information

This PR does not change how Onyx works and only affects dev, debug, benchmark usages

Some examples

MD (Console)

image

CSV

image

When the data is too much it's not printed in full, but can be expanded and/or copied
image

JSON

image

MD (String)

image
This is useful when __DEV__ is false as calling console.info would not output anything
image

Related Issues

N/A

Automated Tests

Covered by existing tests,
Manually tested the printing functionality

Linked PRs

kidroca added 5 commits July 16, 2021 16:33
…available

Since the update for Reanimated 2 (TurboModules/JSI)
It's no longer possible to use the old (Chrome) debugging flow

`react-native` runtime does not implement the `console.table` we're using to print information
It was provided by Chrome, by launching the (old) debug flow

`AsciiTable` allows us to print a MarkDown compatible table which is handy for posting
data to github and other places that support MD
@kidroca kidroca requested a review from a team as a code owner July 16, 2021 18:30
@MelvinBot MelvinBot requested review from johnmlee101 and removed request for a team July 16, 2021 18:30
@kidroca
Copy link
Contributor Author

kidroca commented Jul 16, 2021

Oops some leftovers that should be cleaned up, will push another commit

@kidroca kidroca marked this pull request as draft July 16, 2021 18:40
Comment on lines +25 to +37
// Ignore modifying the first |---| for titled tables
let idx = this.getTitle() ? -2 : -1;
const ascii = super.toString()
.replace(/-\|/g, () => {
/* we replace "----|" with "---:|" to align the data to the right in MD */
idx++;

if (idx < 0 || this.leftAlignedCols.includes(idx)) {
return '-|';
}

return ':|';
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is the most weird part of the PR

Markdown tables are left aligned by default
To align a column you add a : column on the side you want it to align (or on both sides for centered)

|----------|---------:|----------:|

When we're printing a table with a title we want to skip the first row which looks like:

|            Onyx:set            |
|--------------------------------|

That's why we start with -2 on that case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe could use a different token than Onyx:set like Onyx~set but this seems fine.

Copy link
Contributor Author

@kidroca kidroca Jul 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Individual tables would be labeled with whatever alias is provided here:
https://github.com/Expensify/react-native-onyx/blob/master/lib/Onyx.js#L771-L777

lib/MDTable.js Show resolved Hide resolved
*/
function printMetrics(raw = false) {
const {totalTime, summaries, lastCompleteCall = {endTime: -1}} = getMetrics();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this to print Last call finished at: N/A when there are no calls made yet (or were cleared)

toDuration(call.startTime, raw),
toDuration(call.endTime, raw),
toDuration(call.duration, raw),
call.args.map(String).join(', ').slice(0, 60), // Restrict cell width to 60 chars max
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're limiting this as it breaks the table when it's too large
I find it better to not JSON.stringify the data and instead keep it simple like: account, [object Object]
otherwise it adds too much noise

Comment on lines -179 to -187
console.table(prettyData.map(({prettyCalls, ...summary}) => summary));
const mainOutput = [
'### Onyx Benchmark',
` - Total: ${toDuration(totalTime, raw)}`,
` - Last call finished at: ${lastCompleteCall ? toDuration(lastCompleteCall.endTime, raw) : 'N/A'}`,
'',
tableSummary.toString()
];

prettyData.forEach((method) => {
console.groupCollapsed(`[${method.methodName}] individual calls: `);
console.table(method.prettyCalls);
/* eslint-disable no-console */
console.info(mainOutput.join('\n'));
methodCallTables.forEach((table) => {
console.groupCollapsed(table.getTitle());
console.info(table.toString());
console.groupEnd();
});

console.groupEnd();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether we need console printing at all
The only convenience here is console.groupCollapsed

@@ -12,6 +12,7 @@
"test": "jest"
},
"dependencies": {
"ascii-table": "0.0.9",
Copy link
Contributor Author

@kidroca kidroca Jul 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was expecting to find something that will build directly as MD, but this was the closest thing I've found
This library required the least modifications to achieve an MD table
I've tried these alternatives:

@kidroca kidroca marked this pull request as ready for review July 16, 2021 19:27
kidroca added 2 commits July 17, 2021 02:01
This allows us to capture start/end time between different interactions
e.g. resetMetrics then switch to a new chat
When you print the stats you'll see the end time compared relative to
the point before making the switch
This helps identify the tables when they are exported to excel
@kidroca kidroca force-pushed the kidroca/md-print-format branch from edfc47e to 5111c1b Compare July 19, 2021 10:19
@johnmlee101
Copy link
Contributor

Is this ready for review?

@kidroca
Copy link
Contributor Author

kidroca commented Jul 20, 2021

@johnmlee101 Yes

default:
return table.toString();
}
}).join('\n\n');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any specific reason we join with 2 new lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To distinct separate tables easier

Copy link
Contributor

@johnmlee101 johnmlee101 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me AFAICT, I asked @marcaaron to do final review

Comment on lines +25 to +37
// Ignore modifying the first |---| for titled tables
let idx = this.getTitle() ? -2 : -1;
const ascii = super.toString()
.replace(/-\|/g, () => {
/* we replace "----|" with "---:|" to align the data to the right in MD */
idx++;

if (idx < 0 || this.leftAlignedCols.includes(idx)) {
return '-|';
}

return ':|';
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe could use a different token than Onyx:set like Onyx~set but this seems fine.

@marcaaron marcaaron merged commit 0b0e089 into Expensify:master Jul 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants