-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8a274f
commit 3e9ba9c
Showing
51 changed files
with
1,053 additions
and
602 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
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,46 @@ | ||
// @flow | ||
/* eslint-disable no-console */ | ||
import type { Action } from '../../types'; | ||
|
||
type Bucket = { | ||
[key: string]: number[], | ||
} | ||
|
||
const average = (values: number[]): number => { | ||
const sum: number = values.reduce((previous: number, current: number) => previous + current, 0); | ||
return sum / values.length; | ||
}; | ||
|
||
export default (groupSize: number) => { | ||
console.log('Starting average action timer middleware'); | ||
console.log(`Will take an average every ${groupSize} actions`); | ||
const bucket: Bucket = {}; | ||
|
||
return () => (next: (Action) => mixed) => (action: Action): mixed => { | ||
const start: number = performance.now(); | ||
|
||
const result: mixed = next(action); | ||
|
||
const end: number = performance.now(); | ||
|
||
const duration: number = end - start; | ||
|
||
if (!bucket[action.type]) { | ||
bucket[action.type] = [duration]; | ||
return result; | ||
} | ||
|
||
bucket[action.type].push(duration); | ||
|
||
if (bucket[action.type].length < groupSize) { | ||
return result; | ||
} | ||
|
||
console.warn(`Average time for ${action.type}`, average(bucket[action.type])); | ||
|
||
// reset | ||
bucket[action.type] = []; | ||
|
||
return result; | ||
}; | ||
}; |
Oops, something went wrong.