# Backport
This will backport the following commits from `main` to `8.16`:
- [[Security Solution] [Timeline] Consolidate reduces, remove unneeded
async/awaits, other small fixes
(#197168)](https://github.com/elastic/kibana/pull/197168)
<!--- Backport version: 9.4.3 -->
### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)
<!--BACKPORT [{"author":{"name":"Kevin
Qualters","email":"56408403+kqualters-elastic@users.noreply.github.com"},"sourceCommit":{"committedDate":"2024-11-22T18:24:54Z","message":"[Security
Solution] [Timeline] Consolidate reduces, remove unneeded async/awaits,
other small fixes (#197168)\n\n## Summary\r\n\r\nFor most of 8.x, both
anecdotally from users and in development,\r\ntimeline search strategy
based apis would often seem slower than the\r\nequivalent search in
discover or elsewhere in kibana, and I have long\r\nsuspected that this
came from how the timeline sever code formatted the\r\nelasticsearch
responses for use in the UI, and while working on\r\nsomething else,
noticed even higher than normal occurrences in logs
of\r\n\"][http.server.Kibana] Event loop utilization
for\r\n/internal/search/timelineSearchStrategy exceeded threshold
of...\" and so\r\nI tried to refactor all of the functions in place as
much as possible,\r\nkeeping the apis similar, most of the unit tests,
etc, but removing as\r\nmany as possible of the Promise.alls, reduce
within reduce, etc. This\r\nhas lead to a substantial improvement in
performance, as you can see\r\nbelow, and with larger result sets, I
think the difference would only be\r\nmore noticeable.\r\n\r\nAfter
fix:\r\n~40 ms for formatTimelineData with ~1000 docs\r\n<img
width=\"1470\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/c664f940-aa37-4335-9204-2a9300fbafa0\">\r\nBefore
fix:\r\n~18000 ms for formatTimelineData with ~1000 docs\r\n<img
width=\"1464\"
alt=\"image\"\r\nsrc=\"17825606/chrome_profile_timeline_fast.cpuprofile)\r\nI've
attached the chrome devtools profiles for each, the time was\r\nmeasured
with the function:\r\n\r\n```\r\nasync function measureAwait<T>(promise:
Promise<T>, label: string): Promise<T> {\r\n const start =
performance.now();\r\n try {\r\n const result = await promise;\r\n const
duration = performance.now() - start;\r\n console.log(`${label} took
${duration}ms`);\r\n return result;\r\n } catch (error) {\r\n const
duration = performance.now() - start;\r\n console.log(`${label} failed
after ${duration}ms`);\r\n throw error;\r\n }\r\n}\r\n```\r\n\r\nWrapped
around the call to formatTimelineData
in\r\nx-pack/plugins/timelines/server/search_strategy/timeline/factory/events/all/index.ts\r\n\r\n\r\n###
Checklist\r\n\r\n- [ ] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"30fb8dd5bb97b5001030ed9eed355ab4fffc9070","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:Threat
Hunting:Investigations","backport:prev-major"],"title":"[Security
Solution] [Timeline] Consolidate reduces, remove unneeded async/awaits,
other small
fixes","number":197168,"url":"https://github.com/elastic/kibana/pull/197168","mergeCommit":{"message":"[Security
Solution] [Timeline] Consolidate reduces, remove unneeded async/awaits,
other small fixes (#197168)\n\n## Summary\r\n\r\nFor most of 8.x, both
anecdotally from users and in development,\r\ntimeline search strategy
based apis would often seem slower than the\r\nequivalent search in
discover or elsewhere in kibana, and I have long\r\nsuspected that this
came from how the timeline sever code formatted the\r\nelasticsearch
responses for use in the UI, and while working on\r\nsomething else,
noticed even higher than normal occurrences in logs
of\r\n\"][http.server.Kibana] Event loop utilization
for\r\n/internal/search/timelineSearchStrategy exceeded threshold
of...\" and so\r\nI tried to refactor all of the functions in place as
much as possible,\r\nkeeping the apis similar, most of the unit tests,
etc, but removing as\r\nmany as possible of the Promise.alls, reduce
within reduce, etc. This\r\nhas lead to a substantial improvement in
performance, as you can see\r\nbelow, and with larger result sets, I
think the difference would only be\r\nmore noticeable.\r\n\r\nAfter
fix:\r\n~40 ms for formatTimelineData with ~1000 docs\r\n<img
width=\"1470\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/c664f940-aa37-4335-9204-2a9300fbafa0\">\r\nBefore
fix:\r\n~18000 ms for formatTimelineData with ~1000 docs\r\n<img
width=\"1464\"
alt=\"image\"\r\nsrc=\"17825606/chrome_profile_timeline_fast.cpuprofile)\r\nI've
attached the chrome devtools profiles for each, the time was\r\nmeasured
with the function:\r\n\r\n```\r\nasync function measureAwait<T>(promise:
Promise<T>, label: string): Promise<T> {\r\n const start =
performance.now();\r\n try {\r\n const result = await promise;\r\n const
duration = performance.now() - start;\r\n console.log(`${label} took
${duration}ms`);\r\n return result;\r\n } catch (error) {\r\n const
duration = performance.now() - start;\r\n console.log(`${label} failed
after ${duration}ms`);\r\n throw error;\r\n }\r\n}\r\n```\r\n\r\nWrapped
around the call to formatTimelineData
in\r\nx-pack/plugins/timelines/server/search_strategy/timeline/factory/events/all/index.ts\r\n\r\n\r\n###
Checklist\r\n\r\n- [ ] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"30fb8dd5bb97b5001030ed9eed355ab4fffc9070"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/197168","number":197168,"mergeCommit":{"message":"[Security
Solution] [Timeline] Consolidate reduces, remove unneeded async/awaits,
other small fixes (#197168)\n\n## Summary\r\n\r\nFor most of 8.x, both
anecdotally from users and in development,\r\ntimeline search strategy
based apis would often seem slower than the\r\nequivalent search in
discover or elsewhere in kibana, and I have long\r\nsuspected that this
came from how the timeline sever code formatted the\r\nelasticsearch
responses for use in the UI, and while working on\r\nsomething else,
noticed even higher than normal occurrences in logs
of\r\n\"][http.server.Kibana] Event loop utilization
for\r\n/internal/search/timelineSearchStrategy exceeded threshold
of...\" and so\r\nI tried to refactor all of the functions in place as
much as possible,\r\nkeeping the apis similar, most of the unit tests,
etc, but removing as\r\nmany as possible of the Promise.alls, reduce
within reduce, etc. This\r\nhas lead to a substantial improvement in
performance, as you can see\r\nbelow, and with larger result sets, I
think the difference would only be\r\nmore noticeable.\r\n\r\nAfter
fix:\r\n~40 ms for formatTimelineData with ~1000 docs\r\n<img
width=\"1470\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/c664f940-aa37-4335-9204-2a9300fbafa0\">\r\nBefore
fix:\r\n~18000 ms for formatTimelineData with ~1000 docs\r\n<img
width=\"1464\"
alt=\"image\"\r\nsrc=\"17825606/chrome_profile_timeline_fast.cpuprofile)\r\nI've
attached the chrome devtools profiles for each, the time was\r\nmeasured
with the function:\r\n\r\n```\r\nasync function measureAwait<T>(promise:
Promise<T>, label: string): Promise<T> {\r\n const start =
performance.now();\r\n try {\r\n const result = await promise;\r\n const
duration = performance.now() - start;\r\n console.log(`${label} took
${duration}ms`);\r\n return result;\r\n } catch (error) {\r\n const
duration = performance.now() - start;\r\n console.log(`${label} failed
after ${duration}ms`);\r\n throw error;\r\n }\r\n}\r\n```\r\n\r\nWrapped
around the call to formatTimelineData
in\r\nx-pack/plugins/timelines/server/search_strategy/timeline/factory/events/all/index.ts\r\n\r\n\r\n###
Checklist\r\n\r\n- [ ] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"30fb8dd5bb97b5001030ed9eed355ab4fffc9070"}}]}]
BACKPORT-->
Co-authored-by: Kevin Qualters <56408403+kqualters-elastic@users.noreply.github.com>