-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(dynamic-chunk): increase chunks time limits (#342)
Changed the chunk time range from 2 - 8 sec to 8 - 24 sec. This noticeably reduces server and client overhead and decreases upload time.
- Loading branch information
Showing
6 changed files
with
75 additions
and
72 deletions.
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,16 @@ | ||
import { DynamicChunk } from './dynamic-chunk'; | ||
|
||
describe('DynamicChunk', () => { | ||
const init = DynamicChunk.size; | ||
it('scale', () => { | ||
expect(DynamicChunk.scale(0)).toEqual(init / 2); | ||
expect(DynamicChunk.scale(0)).toEqual(init / 4); | ||
expect(DynamicChunk.scale(Number.MAX_SAFE_INTEGER)).toEqual(init / 2); | ||
expect(DynamicChunk.scale(Number.MAX_SAFE_INTEGER)).toEqual(init); | ||
expect(DynamicChunk.scale(Number.MAX_SAFE_INTEGER)).toEqual(init * 2); | ||
expect(DynamicChunk.scale(undefined as any)).toEqual(init * 2); | ||
}); | ||
afterEach(() => { | ||
DynamicChunk.size = init; | ||
}); | ||
}); |
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,25 @@ | ||
const KiB = 1024; | ||
/** | ||
* Adaptive chunk size | ||
*/ | ||
export class DynamicChunk { | ||
/** Maximum chunk size in bytes */ | ||
static maxSize = Number.MAX_SAFE_INTEGER; | ||
/** Minimum chunk size in bytes */ | ||
static minSize = 256 * KiB; | ||
/** Initial chunk size in bytes */ | ||
static size = 4 * (256 * KiB); | ||
static minChunkTime = 8; | ||
static maxChunkTime = 24; | ||
|
||
static scale(throughput: number): number { | ||
const elapsedTime = DynamicChunk.size / throughput; | ||
if (elapsedTime < DynamicChunk.minChunkTime) { | ||
DynamicChunk.size = Math.min(DynamicChunk.maxSize, DynamicChunk.size * 2); | ||
} | ||
if (elapsedTime > DynamicChunk.maxChunkTime) { | ||
DynamicChunk.size = Math.max(DynamicChunk.minSize, DynamicChunk.size / 2); | ||
} | ||
return DynamicChunk.size; | ||
} | ||
} |
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