slowestProfileFunctions.tsx 9.3 KB

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