slowestProfileFunctions.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import {useCallback, useMemo, useState} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {CompactSelect, SelectOption} from 'sentry/components/compactSelect';
  5. import Count from 'sentry/components/count';
  6. import Link from 'sentry/components/links/link';
  7. import LoadingIndicator from 'sentry/components/loadingIndicator';
  8. import Pagination from 'sentry/components/pagination';
  9. import PerformanceDuration from 'sentry/components/performanceDuration';
  10. import {TextTruncateOverflow} from 'sentry/components/profiling/textTruncateOverflow';
  11. import {t, tn} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import {Organization, Project} from 'sentry/types';
  14. import {trackAnalytics} from 'sentry/utils/analytics';
  15. import {Frame} from 'sentry/utils/profiling/frame';
  16. import {EventsResultsDataRow} from 'sentry/utils/profiling/hooks/types';
  17. import {useCurrentProjectFromRouteParam} from 'sentry/utils/profiling/hooks/useCurrentProjectFromRouteParam';
  18. import {useProfileFunctions} from 'sentry/utils/profiling/hooks/useProfileFunctions';
  19. import {formatSort} from 'sentry/utils/profiling/hooks/utils';
  20. import {generateProfileFlamechartRouteWithQuery} from 'sentry/utils/profiling/routes';
  21. import {decodeScalar} from 'sentry/utils/queryString';
  22. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  23. import {useLocation} from 'sentry/utils/useLocation';
  24. import useOrganization from 'sentry/utils/useOrganization';
  25. const SLOWEST_FUNCTIONS_LIMIT = 15;
  26. const SLOWEST_FUNCTIONS_CURSOR = 'functionsCursor';
  27. const functionsFields = [
  28. 'package',
  29. 'function',
  30. 'count()',
  31. 'p75()',
  32. 'sum()',
  33. 'examples()',
  34. ] as const;
  35. type FunctionsField = (typeof functionsFields)[number];
  36. interface SlowestProfileFunctionsProps {
  37. transaction: string;
  38. }
  39. export function SlowestProfileFunctions(props: SlowestProfileFunctionsProps) {
  40. const organization = useOrganization();
  41. const project = useCurrentProjectFromRouteParam();
  42. const [functionType, setFunctionType] = useState<'application' | 'system' | 'all'>(
  43. 'application'
  44. );
  45. const location = useLocation();
  46. const functionsCursor = useMemo(
  47. () => decodeScalar(location.query[SLOWEST_FUNCTIONS_CURSOR]),
  48. [location.query]
  49. );
  50. const functionsSort = useMemo(
  51. () =>
  52. formatSort<FunctionsField>(
  53. decodeScalar(location.query.functionsSort),
  54. functionsFields,
  55. {
  56. key: 'sum()',
  57. order: 'desc',
  58. }
  59. ),
  60. [location.query.functionsSort]
  61. );
  62. const handleFunctionsCursor = useCallback((cursor, pathname, query) => {
  63. browserHistory.push({
  64. pathname,
  65. query: {...query, [SLOWEST_FUNCTIONS_CURSOR]: cursor},
  66. });
  67. }, []);
  68. const query = useMemo(() => {
  69. const conditions = new MutableSearch('');
  70. conditions.setFilterValues('transaction', [props.transaction]);
  71. if (functionType === 'application') {
  72. conditions.setFilterValues('is_application', ['1']);
  73. } else if (functionType === 'system') {
  74. conditions.setFilterValues('is_application', ['0']);
  75. }
  76. return conditions.formatString();
  77. }, [functionType, props.transaction]);
  78. const functionsQuery = useProfileFunctions<FunctionsField>({
  79. fields: functionsFields,
  80. referrer: 'api.profiling.profile-summary-functions-table',
  81. sort: functionsSort,
  82. query,
  83. limit: SLOWEST_FUNCTIONS_LIMIT,
  84. cursor: functionsCursor,
  85. });
  86. const onChangeFunctionType = useCallback(v => setFunctionType(v.value), []);
  87. const functions = functionsQuery.data?.data ?? [];
  88. const onSlowestFunctionClick = useCallback(() => {
  89. trackAnalytics('profiling_views.go_to_flamegraph', {
  90. organization,
  91. source: `profiling_transaction.slowest_functions_table`,
  92. });
  93. }, [organization]);
  94. return (
  95. <SlowestFunctionsContainer>
  96. <SlowestFunctionsTitleContainer>
  97. <SlowestFunctionsTypeSelect
  98. value={functionType}
  99. options={SLOWEST_FUNCTION_OPTIONS}
  100. onChange={onChangeFunctionType}
  101. triggerProps={TRIGGER_PROPS}
  102. offset={4}
  103. />
  104. <SlowestFunctionsPagination
  105. pageLinks={functionsQuery.getResponseHeader?.('Link')}
  106. onCursor={handleFunctionsCursor}
  107. size="xs"
  108. />
  109. </SlowestFunctionsTitleContainer>
  110. <SlowestFunctionsList>
  111. {functionsQuery.isLoading ? (
  112. <SlowestFunctionsQueryState>
  113. <LoadingIndicator size={36} />
  114. </SlowestFunctionsQueryState>
  115. ) : functionsQuery.isError ? (
  116. <SlowestFunctionsQueryState>
  117. {t('Failed to fetch slowest functions')}
  118. </SlowestFunctionsQueryState>
  119. ) : !functions.length ? (
  120. <SlowestFunctionsQueryState>
  121. {t('The fastest code is one that never runs.')}
  122. </SlowestFunctionsQueryState>
  123. ) : (
  124. functions.map((fn, i) => {
  125. return (
  126. <SlowestFunctionEntry
  127. key={i}
  128. func={fn}
  129. organization={organization}
  130. project={project}
  131. onSlowestFunctionClick={onSlowestFunctionClick}
  132. />
  133. );
  134. })
  135. )}
  136. </SlowestFunctionsList>
  137. </SlowestFunctionsContainer>
  138. );
  139. }
  140. interface SlowestFunctionEntryProps {
  141. func: EventsResultsDataRow<'function' | 'package' | 'count()' | 'p75()' | 'sum()'>;
  142. onSlowestFunctionClick: () => void;
  143. organization: Organization;
  144. project: Project | null;
  145. }
  146. function SlowestFunctionEntry(props: SlowestFunctionEntryProps) {
  147. const frame = useMemo(() => {
  148. return new Frame(
  149. {
  150. key: 0,
  151. name: props.func.function as string,
  152. package: props.func.package as string,
  153. },
  154. // Ensures that the frame runs through the normalization code path
  155. props.project?.platform && /node|javascript/.test(props.project.platform)
  156. ? props.project.platform
  157. : undefined,
  158. 'aggregate'
  159. );
  160. }, [props.func, props.project]);
  161. return (
  162. <SlowestFunctionRow>
  163. <SlowestFunctionMainRow>
  164. <div>
  165. <Link
  166. onClick={props.onSlowestFunctionClick}
  167. to={generateProfileFlamechartRouteWithQuery({
  168. orgSlug: props.organization.slug,
  169. projectSlug: props.project?.slug ?? '',
  170. profileId: (props.func['examples()']?.[0] as string) ?? '',
  171. query: {
  172. // specify the frame to focus, the flamegraph will switch
  173. // to the appropriate thread when these are specified
  174. frameName: frame.name as string,
  175. framePackage: frame.package as string,
  176. },
  177. })}
  178. >
  179. <TextTruncateOverflow>{frame.name}</TextTruncateOverflow>
  180. </Link>
  181. </div>
  182. <div>
  183. <PerformanceDuration nanoseconds={props.func['sum()'] as number} abbreviation />
  184. </div>
  185. </SlowestFunctionMainRow>
  186. <SlowestFunctionMetricsRow>
  187. <div>
  188. <TextTruncateOverflow>{frame.package}</TextTruncateOverflow>
  189. </div>
  190. <div>
  191. <Count value={props.func['count()'] as number} />{' '}
  192. {tn('time', 'times', props.func['count()'])}
  193. {', '}
  194. <PerformanceDuration nanoseconds={props.func['p75()'] as number} abbreviation />
  195. </div>
  196. </SlowestFunctionMetricsRow>
  197. </SlowestFunctionRow>
  198. );
  199. }
  200. const SlowestFunctionsList = styled('div')`
  201. flex-basis: 100%;
  202. overflow: auto;
  203. min-height: 0;
  204. `;
  205. const SlowestFunctionsContainer = styled('div')`
  206. margin-top: ${space(0.5)};
  207. min-height: 0;
  208. display: flex;
  209. flex-direction: column;
  210. padding: 0 ${space(1)};
  211. border-bottom: 1px solid ${p => p.theme.border};
  212. `;
  213. const SlowestFunctionsPagination = styled(Pagination)`
  214. margin: 0;
  215. button {
  216. height: 16px;
  217. width: 16px;
  218. min-width: 16px;
  219. min-height: 16px;
  220. svg {
  221. width: 10px;
  222. height: 10px;
  223. }
  224. }
  225. `;
  226. const SlowestFunctionsTitleContainer = styled('div')`
  227. display: flex;
  228. align-items: center;
  229. justify-content: space-between;
  230. margin-bottom: ${space(1)};
  231. `;
  232. const SlowestFunctionsTypeSelect = styled(CompactSelect)`
  233. button {
  234. margin: 0;
  235. padding: 0;
  236. }
  237. `;
  238. const SlowestFunctionsQueryState = styled('div')`
  239. text-align: center;
  240. padding: ${space(2)} ${space(0.5)};
  241. color: ${p => p.theme.subText};
  242. `;
  243. const SlowestFunctionRow = styled('div')`
  244. margin-bottom: ${space(1)};
  245. `;
  246. const SlowestFunctionMainRow = styled('div')`
  247. display: flex;
  248. align-items: center;
  249. justify-content: space-between;
  250. > div:first-child {
  251. min-width: 0;
  252. }
  253. > div:last-child {
  254. white-space: nowrap;
  255. }
  256. `;
  257. const SlowestFunctionMetricsRow = styled('div')`
  258. display: flex;
  259. align-items: center;
  260. justify-content: space-between;
  261. font-size: ${p => p.theme.fontSizeSmall};
  262. color: ${p => p.theme.subText};
  263. margin-top: ${space(0.25)};
  264. `;
  265. const TRIGGER_PROPS = {borderless: true, size: 'zero' as const};
  266. const SLOWEST_FUNCTION_OPTIONS: SelectOption<'application' | 'system' | 'all'>[] = [
  267. {
  268. label: t('Slowest Application Functions'),
  269. value: 'application' as const,
  270. },
  271. {
  272. label: t('Slowest System Functions'),
  273. value: 'system' as const,
  274. },
  275. {
  276. label: t('Slowest Functions'),
  277. value: 'all' as const,
  278. },
  279. ];