-
-
Notifications
You must be signed in to change notification settings - Fork 118
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
Calculate upload rate #1029
Merged
Merged
Calculate upload rate #1029
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
aa38c78
Add rate to files and queue
RobbieTheWagner 417e054
Add estimated time to demo
RobbieTheWagner 3112dfd
experiment: store chunk rates and display average
gilest df1d0c7
fix(queue): only sum rates of files which are currently uploading
gilest 436c7c1
chore(website): improve est remaining display using date-fns
gilest 8f3118a
fix(updateRate): ensure to take only one time value per update
gilest efae27c
feat(rate calculation): implement a weighted moving average
gilest aeb8d71
docs(queue): add rate to public api
gilest 80605d6
chore(self-review): optimise util functions and add comments
gilest d662e48
chore(website): fix lint
gilest d11e3a2
chore(self-review): don't need to reset file progress attributes
gilest fa4723d
chore(self-review): correct column size in demo upload component
gilest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Calculate from the last x rate recordings | ||
const CALCULATE_FROM_LAST = 30; | ||
|
||
// Weigh recent rates more highly | ||
const THRESHOLDS = [ | ||
{ threshold: 10, proportion: 3 }, | ||
{ threshold: 20, proportion: 2 }, | ||
{ threshold: 30, proportion: 1 }, | ||
]; | ||
const DEFAULT_PROPORTION = 1; | ||
|
||
export function estimatedRate(allRates: number[]): number { | ||
if (!allRates.length) return 0; | ||
|
||
const rates = allRates.slice(CALCULATE_FROM_LAST * -1).reverse(); | ||
const weights = generateWeights(rates.length); | ||
|
||
// Multiply each rate by its respective weight for a weighted average | ||
return rates.reduce((acc, rate, index) => { | ||
const weight = weights[index] as number; | ||
return acc + rate * weight; | ||
}, 0); | ||
} | ||
|
||
export function generateWeights(totalRates: number): number[] { | ||
// Generate an array the same length as totalRates filled with proportioanl weights | ||
const proportions = Array.from({ length: totalRates }).map((_, index) => { | ||
return proportionForPosition(index + 1); | ||
}); | ||
|
||
const proportionTotal = proportions.reduce((acc, value) => acc + value, 0); | ||
// Convert proportional weights to real weights by division | ||
const realWeights = proportions.map( | ||
(proportion) => proportion / proportionTotal, | ||
); | ||
|
||
return realWeights; | ||
} | ||
|
||
export function proportionForPosition(position: number) { | ||
for (const { threshold, proportion } of THRESHOLDS) { | ||
if (position <= threshold) return proportion; | ||
} | ||
return DEFAULT_PROPORTION; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import type { Queue } from './queue.ts'; | |
import { guidFor } from '@ember/object/internals'; | ||
import RSVP from 'rsvp'; | ||
import { FileSource, FileState, type UploadOptions } from './interfaces.ts'; | ||
import { estimatedRate } from './system/rate.ts'; | ||
|
||
/** | ||
* Files provide a uniform interface for interacting | ||
|
@@ -51,6 +52,11 @@ export class UploadFile { | |
this.#name = value; | ||
} | ||
|
||
/** The current speed in ms that it takes to upload one byte */ | ||
get rate() { | ||
return estimatedRate(this.rates); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Work done in plain functions for unit-testing purposes. |
||
} | ||
|
||
#size = 0; | ||
|
||
/** The size of the file in bytes. */ | ||
|
@@ -79,6 +85,11 @@ export class UploadFile { | |
return this.type.split('/').slice(-1)[0] ?? ''; | ||
} | ||
|
||
/** | ||
* Tracks the number of bytes that had been uploaded when progress values last changed. | ||
*/ | ||
bytesWhenProgressLastUpdated = 0; | ||
|
||
/** The number of bytes that have been uploaded to the server */ | ||
@tracked loaded = 0; | ||
|
||
|
@@ -138,6 +149,14 @@ export class UploadFile { | |
// */ | ||
// source?: FileSource; | ||
|
||
/** | ||
* The timestamp of when the progress last updated in milliseconds. Used to calculate the time | ||
* that has elapsed. | ||
*/ | ||
timestampWhenProgressLastUpdated = 0; | ||
|
||
@tracked rates: number[] = []; | ||
|
||
/** | ||
* Upload file with `application/octet-stream` content type. | ||
* | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupTest } from 'ember-qunit'; | ||
import { | ||
estimatedRate, | ||
generateWeights, | ||
proportionForPosition, | ||
} from 'ember-file-upload/internal'; | ||
|
||
module('Unit | rate', function (hooks) { | ||
setupTest(hooks); | ||
|
||
test('estimatedRate', function (assert) { | ||
assert.strictEqual( | ||
estimatedRate([80, 80, 80, 80, 80, 80, 80, 80, 80, 80]), | ||
80, | ||
'straight average up to first threshold', | ||
); | ||
assert.strictEqual( | ||
estimatedRate([ | ||
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 50, 50, 50, 50, 50, 50, 50, 50, | ||
50, 50, | ||
]), | ||
62.00000000000003, | ||
'slowing down, recent values have more affect', | ||
); | ||
assert.strictEqual( | ||
estimatedRate([ | ||
50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 80, 80, 80, 80, 80, 80, 80, 80, | ||
80, 80, | ||
]), | ||
68, | ||
'speeding up, recent values have more affect', | ||
); | ||
}); | ||
|
||
test('generateWeights', function (assert) { | ||
assert.deepEqual(generateWeights(1), [1], 'single weight is 1'); | ||
assert.deepEqual( | ||
generateWeights(10), | ||
[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1], | ||
'first 10 weights evenly divided', | ||
); | ||
|
||
assert.deepEqual( | ||
generateWeights(20), | ||
[ | ||
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.04, 0.04, | ||
0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, | ||
], | ||
'proportionally even weights in each threshold', | ||
); | ||
|
||
assert.deepEqual( | ||
generateWeights(30), | ||
[ | ||
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, | ||
0.03333333333333333, 0.03333333333333333, 0.03333333333333333, | ||
0.03333333333333333, 0.03333333333333333, 0.03333333333333333, | ||
0.03333333333333333, 0.03333333333333333, 0.03333333333333333, | ||
0.03333333333333333, 0.016666666666666666, 0.016666666666666666, | ||
0.016666666666666666, 0.016666666666666666, 0.016666666666666666, | ||
0.016666666666666666, 0.016666666666666666, 0.016666666666666666, | ||
0.016666666666666666, 0.016666666666666666, | ||
], | ||
'proportionally even weights in each threshold', | ||
); | ||
}); | ||
|
||
test('proportionForPosition', function (assert) { | ||
assert.strictEqual(proportionForPosition(1), 3, 'rate 1 high proportion'); | ||
assert.strictEqual(proportionForPosition(10), 3, 'rate 10 high proportion'); | ||
assert.strictEqual(proportionForPosition(11), 2, 'rate 11 med proportion'); | ||
assert.strictEqual(proportionForPosition(20), 2, 'rate 20 med proportion'); | ||
assert.strictEqual(proportionForPosition(21), 1, 'rate 21 low proportion'); | ||
assert.strictEqual(proportionForPosition(30), 1, 'rate 30 low proportion'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be more performant to use a
TrackedArray
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure. I say leave it unless we have problems.