Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Update docs and add example app #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions examples/fluentui-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
.eslintcache
21 changes: 21 additions & 0 deletions examples/fluentui-app/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Microsoft Corporation. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
62 changes: 62 additions & 0 deletions examples/fluentui-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# fluentui-app

`fluentui-app` is a [Create React App](https://github.com/facebook/create-react-app) that comes with Fluent UI
pre-installed, and serves as an example of how to use `flamegrill` as a standalone tool for performance measurements.

## How to generate flamegraph for the app

`fluentui-app` is a standard CRA app with an artificial bottleneck added to it so that it is better visible in the
generated flamegraphs using the `flamegrill` tool. You should note that this example serves as a general example of how
to use `flamegrill` with any CRA app and not specifically FluentUI.

The bootleneck is called `InefficientComponent`. It is a dummy component that performs a lot pointless work, and is
defined in the `src/App.tsx` as follows

```tsx
const InefficientComponent: React.FunctionComponent = (props) => {
for (let i = 0; i < 100; i++) {
console.log(i);
}
return <div>{props.children}</div>;
}
```

We then wrap the logo image inside of it

```tsx
export const App: React.FunctionComponent = () => {
return
// ...
<InefficientComponent>
<img src={logo} alt="logo" />
</InefficientComponent>;
}
```

In order to generate the flamegraphs for the app, we need to build the app for production. Normally, we'd simply
run `yarn build` which is an alias for `react-scripts build`, which in turn, under-the-hood calls `webpack` to
optimize and minify the app. However, for the flamegraphs to be readable and meaningful, we need to disable the
minification. While `react-scripts` don't allow us to tweak the `webpack` out of the box, it is possible via
`react-app-rewired` package. With `react-app-rewired` added as a dev dependency, we can tell it to override certain
configuration values of `webpack` such as turning minification off for the production build. You can explore this
further in `config-overrides.js` file.

If you build the for production using

```
node_modules/.bin/react-app-rewired build
Copy link
Member

Choose a reason for hiding this comment

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

If this is defined as a script entry, why not use yarn build?

I tried yarn build locally and it looks like there's an issue with the package.json:

D:\git\flamegrill\examples\fluentui-app (examples-update -> kubkon)
λ yarn build
yarn run v1.22.4
error An unexpected error occurred: "D:\\git\\flamegrill\\examples\\fluentui-app\\package.json: Unexpected token } in JSON at position 1069".

Copy link
Author

Choose a reason for hiding this comment

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

If this is defined as a script entry, why not use yarn build?

We could indeed. I just wanted to be more explicit about what is happening under-the-hood, but I'm happy to change this one to yarn build instead.

I tried yarn build locally and it looks like there's an issue with the package.json:

D:\git\flamegrill\examples\fluentui-app (examples-update -> kubkon)
λ yarn build
yarn run v1.22.4
error An unexpected error occurred: "D:\\git\\flamegrill\\examples\\fluentui-app\\package.json: Unexpected token } in JSON at position 1069".

Ah, excellent catch, thanks!

Copy link
Member

@JasonGore JasonGore Mar 16, 2021

Choose a reason for hiding this comment

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

I think exposing what's being done here is preferable, and in that case could just be yarn react-app-rewired build, which should work even without a scripts entry. Fundamentally I think you should be leaning on yarn to find your deps for you rather than doing it yourself with a hardcoded path. :)

```

or simply using `yarn build` alias as it's been preconfigured to use `react-app-rewired`. This will put the
production build of the app in the `build` directory.

Next, create `perf` directory where we'll store generated performance flamegraphs and logs, and run
Copy link
Member

Choose a reason for hiding this comment

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

Might be worth adding perf to .gitignore since it's a recommended directory.

`flamegrill` out of with pointing at the generated `build/index.html`.

```
mkdir perf && cd perf
../../../node_modules/.bin/flamegrill -n AppTest -s file:///path/to/my/app/build/index.html
Copy link
Member

Choose a reason for hiding this comment

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

The pathing here is also pretty ugly. Is there a reason this can't be yarn flamegrill -n AppTest -s file:///path/to/my/app/build/index.html?

Copy link
Author

Choose a reason for hiding this comment

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

Oh, we absolutely could. I just wanted to point out that it is fine to run this example using a local build of flamegrill itself. Lemme adjust then!

Copy link
Member

@JasonGore JasonGore Mar 16, 2021

Choose a reason for hiding this comment

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

That's fine, but just for clarity, I think the two most common use cases would be:

  1. Someone does yarn install -g flamegrill, in which case they should be able to invoke flamegrill directly
  2. Someone adds flamegrill as a devDep, which allows them to invoke it using yarn flamegrill

I don't think in any case people should have to be diving into a specific node_modules directory to invoke things. That said, I think having yarn flamegrill <args> here is fine without the scripts entry, if you want to expose all of the args.

```

This will generate flamegraphs in the `perf` directory which can then be previewed using your favourite browser.

9 changes: 9 additions & 0 deletions examples/fluentui-app/config-overrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
webpack: function(config, env) {
if (env === 'production') {
config.optimization.minimize = false;
config.output.publicPath = './';
}
return config;
}
}
47 changes: 47 additions & 0 deletions examples/fluentui-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "fluentui-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@fluentui/react": "^7.155.3",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@types/jest": "^26.0.15",
"@types/node": "^12.0.0",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.3",
"typescript": "^4.1.2",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-app-rewired build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"react-app-rewired": "^2.1.8",
}
}
38 changes: 38 additions & 0 deletions examples/fluentui-app/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
9 changes: 9 additions & 0 deletions examples/fluentui-app/src/App.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { App } from './App';

it('renders "Welcome to Your Fluent UI App"', () => {
render(<App />);
const linkElement = screen.getByText(/Welcome to Your Fluent UI App/i);
expect(linkElement).toBeInTheDocument();
});
70 changes: 70 additions & 0 deletions examples/fluentui-app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React from "react";
import { Stack, Text, Link, FontWeights, IStackTokens } from "@fluentui/react";
import logo from "./logo.svg";
import "./App.css";

const boldStyle = { root: { fontWeight: FontWeights.semibold } };
const stackTokens: IStackTokens = { childrenGap: 15 };

const InefficientComponent: React.FunctionComponent = (props) => {
for (let i = 0; i < 100; i++) {
console.log(i);
}
return <div>{props.children}</div>;
};

export const App: React.FunctionComponent = () => {
return (
<Stack
horizontalAlign="center"
verticalAlign="center"
verticalFill
styles={{
root: {
width: "960px",
margin: "0 auto",
textAlign: "center",
color: "#605e5c",
},
}}
tokens={stackTokens}
>
<img className="App-logo" src={logo} alt="logo" />
<Text variant="xxLarge" styles={boldStyle}>
Welcome to Your Fluent UI App
</Text>
<Text variant="large">
For a guide on how to customize this project, check out the Fluent UI
documentation.
</Text>
<Text variant="large" styles={boldStyle}>
Essential Links
</Text>
<Stack horizontal tokens={stackTokens} horizontalAlign="center">
<Link href="https://developer.microsoft.com/en-us/fluentui#/get-started/web">
Docs
</Link>
<Link href="https://stackoverflow.com/questions/tagged/office-ui-fabric">
Stack Overflow
</Link>
<Link href="https://github.com/microsoft/fluentui/">Github</Link>
<Link href="https://twitter.com/fluentui">Twitter</Link>
</Stack>
<Text variant="large" styles={boldStyle}>
Design System
</Text>
<InefficientComponent>
<img src={logo} alt="logo" />
</InefficientComponent>
<Stack horizontal tokens={stackTokens} horizontalAlign="center">
<Link href="https://developer.microsoft.com/en-us/fluentui#/styles/web/icons">
Icons
</Link>
<Link href="https://developer.microsoft.com/en-us/fluentui#/styles/web">
Styles
</Link>
<Link href="https://aka.ms/themedesigner">Theme Designer</Link>
</Stack>
</Stack>
);
};
11 changes: 11 additions & 0 deletions examples/fluentui-app/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans',
'Droid Sans', 'Helvetica Neue', sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', monospace;
}
21 changes: 21 additions & 0 deletions examples/fluentui-app/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { App } from './App';
import { mergeStyles } from '@fluentui/react';
import reportWebVitals from './reportWebVitals';

// Inject some global styles
mergeStyles({
':global(body,html,#root)': {
margin: 0,
padding: 0,
height: '100vh',
},
});

ReactDOM.render(<App />, document.getElementById('root'));

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
8 changes: 8 additions & 0 deletions examples/fluentui-app/src/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/fluentui-app/src/react-app-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="react-scripts" />
15 changes: 15 additions & 0 deletions examples/fluentui-app/src/reportWebVitals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ReportHandler } from 'web-vitals';

const reportWebVitals = (onPerfEntry?: ReportHandler) => {
Copy link
Member

Choose a reason for hiding this comment

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

Is web-vitals important to mention? Is this generated from react-scripts?

Copy link
Author

Choose a reason for hiding this comment

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

It's generated by the template, yep.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about source controlling output from react-scripts. It could cause confusion like mine here. Are there any other options like scripting the template generation in the example? Might be a good unit test to make sure it stays working with "live" react-scripts rather than a snapshot of previous output.

Copy link
Author

Choose a reason for hiding this comment

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

Oh, I probably didn't make myself clear last time. The web-vitals code is part of the FluentUI CRA template (see here) and is not auto-generated by react-scripts, hence, as such, I don't think there is much we can or should do here. If the template itself removes it, then it will disappear from the example app here as well.

Copy link
Member

Choose a reason for hiding this comment

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

I just ran npx create-react-app my-app --template @fluentui/cra-template and saw a lot of the same output that's here. It seems this code is autogenerated. Is there a reason we can't make npx create-react-app my-app --template @fluentui/cra-template part of the steps rather than controlling its output? The point being that this example should continue to work with whatever npx create-react-app my-app --template @fluentui/cra-template spits out, not a snapshot of its output at this point in time.

Copy link
Author

Choose a reason for hiding this comment

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

Wait, but that's exactly what is happening here. The only thing we have to rewire is preventing minification which is required to be done regardless of the template used as long as create-react-app is used for bootstrapping.

Copy link
Member

Choose a reason for hiding this comment

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

We have a disconnect so if this reply doesn't clarify let's chat on the side. :)

Is examples/fluentui-app/config-overrides.js the only modification necessary to the template's output? If so, why is it necessary to commit any of the template's output?

Let me know if this is wrong, but I'm seeing the steps for reproducing this example as:

  1. npx create-react-app my-app --template @fluentui/cra-template
  2. Presence of examples/fluentui-app/config-overrides.js
  3. Maybe some modifications to package.json?

I'm trying to figure out why it's necessary to add the output from step 1 into git control at all.

Copy link
Author

Choose a reason for hiding this comment

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

No, your understanding seems bang on. So my reasoning for including the contents of the npx create-react-app was so that first of all, all of the tweaks are applied immediately, and secondly, so that the potential user of flamegrill has some examples that effectively allow them to simply invoke yarn, yarn build and yarn flamegrill and explore the flamegraphs. This, in my opinion, lends itself well as a starting point of exploring the internals of flamegrill and flamegraphs in general, since it shields the user from the nitty-gritty details required just to get started. Let me know if that makes sense!

if (onPerfEntry && onPerfEntry instanceof Function) {
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
getCLS(onPerfEntry);
getFID(onPerfEntry);
getFCP(onPerfEntry);
getLCP(onPerfEntry);
getTTFB(onPerfEntry);
});
}
};

export default reportWebVitals;
5 changes: 5 additions & 0 deletions examples/fluentui-app/src/setupTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom';
26 changes: 26 additions & 0 deletions examples/fluentui-app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
]
}
Loading