Skip to content

Commit

Permalink
Add timing/benchmarking fascility and UI (#1128)
Browse files Browse the repository at this point in the history
  • Loading branch information
borisyankov authored Sep 5, 2017
1 parent 0688b9b commit 00eec20
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/StoreHydrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { PureComponent } from 'react';
import { Provider } from 'react-redux';

import store, { restore } from './store';

import timing from './utils/timing';
import LoadingScreen from './start/LoadingScreen';
import Providers from './Providers';

Expand All @@ -17,7 +17,9 @@ export default class StoreHydrator extends PureComponent {
};

componentWillMount() {
timing.start('Store hydration');
restore(() => {
timing.end('Store hydration');
this.setState({ isHydrated: true });
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/translations/messages_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,6 @@
"Diagnostics": "Diagnostics",
"Toggle compose tools": "Toggle compose tools",
"Terms of service": "Terms of service",
"Privacy policy": "Privacy policy"
"Privacy policy": "Privacy policy",
"Timing": "Timing"
}
2 changes: 2 additions & 0 deletions src/nav/AppNavigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import LoadingScreen from '../start/LoadingScreen';
import SettingsScreen from '../settings/SettingsScreen';
import LanguageScreen from '../settings/LanguageScreen';
import DiagnosticsScreen from '../settings/DiagnosticsScreen';
import TimingScreen from '../settings/TimingScreen';
import LightboxScreen from '../lightbox/LightboxScreen';
import GroupScreen from '../group/GroupScreen';

Expand All @@ -38,6 +39,7 @@ export default StackNavigator(
lightbox: { screen: LightboxScreen },
group: { screen: GroupScreen },
diagnostics: { screen: DiagnosticsScreen },
timing: { screen: TimingScreen },
},
{
initialRouteName: 'main',
Expand Down
2 changes: 2 additions & 0 deletions src/nav/navActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ export const navigateToCreateGroup = (): Action =>

export const navigateToDiagnostics = (): Action =>
NavigationActions.navigate({ routeName: 'diagnostics' });

export const navigateToTiming = (): Action => NavigationActions.navigate({ routeName: 'timing' });
4 changes: 3 additions & 1 deletion src/settings/SettingsCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const styles = StyleSheet.create({
flex: 1,
},
divider: {
height: 30,
height: 16,
},
padding: {
padding: 16,
Expand Down Expand Up @@ -74,6 +74,8 @@ export default class SettingsCard extends PureComponent {
<OptionButton label="Language" onPress={actions.navigateToLanguage} />
<View style={styles.divider} />
<OptionButton label="Diagnostics" onPress={actions.navigateToDiagnostics} />
<View style={styles.divider} />
<OptionButton label="Timing" onPress={actions.navigateToTiming} />
<View style={styles.padding}>
<WebLink label="Terms of service" href="/terms/" />
<WebLink label="Privacy policy" href="/privacy/" />
Expand Down
43 changes: 43 additions & 0 deletions src/settings/TimeItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* @flow */
import React, { PureComponent } from 'react';
import { StyleSheet, View } from 'react-native';
import format from 'date-fns/format';
import differenceInMilliseconds from 'date-fns/difference_in_milliseconds';

import { RawLabel } from '../common';

const styles = StyleSheet.create({
item: {
padding: 16,
borderBottomWidth: 1,
borderColor: 'rgba(127, 127, 127, 0.25)',
},
label: {
fontWeight: 'bold',
},
value: {
opacity: 0.9,
},
});

export default class TimeItem extends PureComponent {
props: {
text: string,
start: Date,
end: Date,
};

render() {
const { text, start, end } = this.props;
const startStr = format(start, 'HH:mm:ss'); // eslint-disable-line
const durationStr = differenceInMilliseconds(end, start);
const timingStr = `Start: ${startStr} Duration: ${durationStr} ms`;

return (
<View style={styles.item}>
<RawLabel style={styles.label} text={text} />
<RawLabel style={styles.value} text={timingStr} />
</View>
);
}
}
21 changes: 21 additions & 0 deletions src/settings/TimingScreen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* @flow */
import React, { PureComponent } from 'react';
import { FlatList } from 'react-native';

import { Screen } from '../common';
import TimeItem from './TimeItem';
import timing from '../utils/timing';

export default class TimingScreen extends PureComponent {
render() {
return (
<Screen title="Timing">
<FlatList
data={timing.log}
keyExtractor={item => item}
renderItem={({ item }) => <TimeItem text={item.text} start={item.start} end={item.end} />}
/>
</Screen>
);
}
}
6 changes: 6 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,9 @@ export type RenderedSectionDescriptor = {
};

export type DraftState = { string: string };

export type TimingItem = {
text: string,
start: Date,
end: Date,
};
31 changes: 31 additions & 0 deletions src/utils/timing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* @flow */
import { TimingItem } from '../types';

const timingMap = {};
const log: TimingItem[] = [];

const start = (key: string) => {
timingMap[key] = Date.now();
};

const end = (key: string) => {
if (timingMap[key]) {
log.push({
text: key,
start: timingMap[key],
end: Date.now(),
});
delete timingMap[key];
} else {
log.push({
text: key,
end: Date.now(),
});
}
};

export default {
start,
end,
log,
};

0 comments on commit 00eec20

Please sign in to comment.