slowestProfileFunctions.tsx 8.4 KB

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