-
-
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 1 commit
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
feat(rate calculation): implement a weighted moving average
- Loading branch information
commit efae27c1642168a66d6cec7b0cedc341b39c560b
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// 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); | ||
|
||
return rates.reduce((acc, rate, index) => { | ||
const weight = weights[index] as number; | ||
return acc + rate * weight; | ||
}, 0); | ||
} | ||
|
||
export function generateWeights(totalRates: number): number[] { | ||
const proportions: number[] = []; | ||
|
||
Array.from({ length: totalRates }).forEach((_, index) => { | ||
proportions.push(proportionForPosition(index + 1)); | ||
}); | ||
|
||
const proportionTotal = proportions.reduce((acc, value) => acc + value, 0); | ||
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
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
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.
Work done in plain functions for unit-testing purposes.