Skip to content

Use browser APIs to calculate Blazor's download size#19547

Merged
pranavkm merged 1 commit intoblazor-wasmfrom
prkrishn/fixup-benchmarks
Mar 5, 2020
Merged

Use browser APIs to calculate Blazor's download size#19547
pranavkm merged 1 commit intoblazor-wasmfrom
prkrishn/fixup-benchmarks

Conversation

@pranavkm
Copy link
Copy Markdown
Contributor

@pranavkm pranavkm commented Mar 4, 2020

The publish sizes in our Benchmarks have been broken since the change to the output structure. Looking at the size on disk is also a little cumbersome, since we have to not double count. It seems like it's much more useful to measure the network download size instead.

@ghost ghost added the area-blazor Includes: Blazor, Razor Components label Mar 4, 2020
@pranavkm pranavkm requested a review from javiercn March 4, 2020 01:51
logToConsole() {
logStatistics() {
// Used by test \ benchmarking infrastructure
const onDownloadStats = window['Blazor']._internal.downloadStats;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This seemed "easy" to do, but if there's a better way to approach this, let me know.

const query = new URLSearchParams(window.location.search);
const group = query.get('group');
const resultsUrl = query.get('resultsUrl');
(async function() {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Most of this is just whitespace changes.

@@ -0,0 +1,15 @@
export function getBlazorDownloadSize() {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Again, if there's a better way to do this, I'd be happy to use it. This launches a page in an iframe that wires up the new hook that I added. The contents of the iframe are slightly different because we need to hook up the callback after Blazor's scripts have been initialized, but before downloads happen. The instance of chrome in docker does caching correctly, so relying on caching to happen isn't problematic.

Copy link
Copy Markdown
Member

@javiercn javiercn left a comment

Choose a reason for hiding this comment

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

I think this deserves a more in-depth discussion. There are alternative ways we can do this, that feel simpler to me:

  • Skip .gz files when computing the size (as those are compressed files).
  • Use chrome Remote DevTools Protocol to query the size of the downloaded bits.

@pranavkm
Copy link
Copy Markdown
Contributor Author

pranavkm commented Mar 4, 2020

Skip .gz files when computing the size (as those are compressed files).

I wanted to avoid calculating the sizes in the Benchmark test. It's easy to miss updating the benchmark if we happen to change Blazor's publishing.

Use chrome Remote DevTools Protocol to query the size of the downloaded bits.

That sounds nifty. How does this work?

@SteveSandersonMS
Copy link
Copy Markdown
Member

SteveSandersonMS commented Mar 4, 2020

I think this deserves a more in-depth discussion. There are alternative ways we can do this, that feel simpler to me

I agree. This approach is a bit awkward since it couples us to some internal implementation details, plus it's unable to measure the actual total size including blazor.webassembly.js and any other dependencies we add in the future.

As well as @javiercn's suggestions, another possibly simpler alternative would be to use the performance APIs but not via Blazor's data collection. For example:

  1. If you might need to clear existing caches, execute caches.keys().then(keys => keys.forEach(key => caches.delete(keys)))
  2. Reload the page
  3. Evaluate performance.getEntries().filter(x => x.encodedBodySize).map(x => ({ name: x.name, encodedBodySize: x.encodedBodySize }))

That last expression will give you all the HTTP requests, including for the initial HTML. You can observe both encodedBodySize and decodedBodySize if you want.

If you just want the total transferred size, it's performance.getEntries().reduce((prev, next) => (next.encodedBodySize || 0) + prev, 0). (I know you could figure that out yourself, but I just did it so there you go :) )

@pranavkm pranavkm force-pushed the prkrishn/fixup-benchmarks branch from 5955650 to 7296a03 Compare March 4, 2020 23:39
const app = new BlazorApp();
try {
await app.start();
const downloadSize = app.window.performance.getEntries().reduce((prev, next) => (next.encodedBodySize || 0) + prev, 0);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@SteveSandersonMS the performance trick helped.

@pranavkm
Copy link
Copy Markdown
Contributor Author

pranavkm commented Mar 5, 2020

This has been updated.

@pranavkm pranavkm changed the title Use Blazor's statistics to report download size Use browser APIs to calculate Blazor's download size Mar 5, 2020
Copy link
Copy Markdown
Member

@javiercn javiercn left a comment

Choose a reason for hiding this comment

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

Much better!

@pranavkm pranavkm merged commit f4446f3 into blazor-wasm Mar 5, 2020
@pranavkm pranavkm deleted the prkrishn/fixup-benchmarks branch March 5, 2020 14:38
@pranavkm
Copy link
Copy Markdown
Contributor Author

pranavkm commented Mar 5, 2020

FYI @sebastienros

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants