slowestProfileFunctions.tsx 8.7 KB

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