Title: LocationImpl adds performance overheads due to instantiating a stack trace · Issue #2720 · mockito/mockito · GitHub
Open Graph Title: LocationImpl adds performance overheads due to instantiating a stack trace · Issue #2720 · mockito/mockito
X Title: LocationImpl adds performance overheads due to instantiating a stack trace · Issue #2720 · mockito/mockito
Description: I'm trying to use Mockito in a test of a Java class I didn't write. It takes a dependency which I am stubbing out using Mockito. Skipping all of the business logic, the code ends up being rather similar to this: SomeClass mocked = mock(S...
Open Graph Description: I'm trying to use Mockito in a test of a Java class I didn't write. It takes a dependency which I am stubbing out using Mockito. Skipping all of the business logic, the code ends up being rather si...
X Description: I'm trying to use Mockito in a test of a Java class I didn't write. It takes a dependency which I am stubbing out using Mockito. Skipping all of the business logic, the code ends up being r...
Opengraph URL: https://github.com/mockito/mockito/issues/2720
X: @github
Domain: togithub.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"LocationImpl adds performance overheads due to instantiating a stack trace","articleBody":"I'm trying to use Mockito in a test of a Java class I didn't write. It takes a dependency which I am stubbing out using Mockito. Skipping all of the business logic, the code ends up being rather similar to this:\r\n\r\n```java\r\nSomeClass mocked = mock(SomeClass.class, Mockito.withSettings().stubOnly());\r\nwhen(mocked.someParam()).thenReturn(0);\r\n\r\nfor (int i = 0; i \u003c bigNumber; i++) {\r\n mocked.someParam();\r\n}\r\n```\r\n\r\nThis is in Mockito 4.6.1. This is extremely slow, and looking at Mockito this seems to be a problem for two reasons.\r\n\r\nFirstly, when a mock invocation occurs, LocationImpl is instantiated. This instantiates a Throwable, which constructs a stack trace, error message, etc (which is then discarded). In my test, Mockito was generating 70k exceptions per second.\r\n\r\nThe second slowdown that seemed to be affecting me comes from TypeSafeMatching.getArgumentType. This iterates through all of the methods on the argument matcher class to find the appropriate matches method, seemingly also for each method call. By performing this operation, a lot of JVM internal datastructures get copied - it calls Class.getMethods, which copies all the methods on the class. This method could probably be cached on a per-class basis.\r\n\r\nThis dramatically affected my application's allocation rate; Mockito was leading to approx 3.4GiB/sec allocations. In the end, I rewrote my mock using ByteBuddy proxies directly. It took the test runtime from 30 seconds down to 1, with the number of exceptions being created per second going down from 70k to ~0.\r\n\r\nI know that Mockito isn't meant to be a Netty-like high performance library, but wanted to flag these two codepaths as affecting every mock call.","author":{"url":"https://github.com/j-baker","@type":"Person","name":"j-baker"},"datePublished":"2022-08-04T16:01:11.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/2720/mockito/issues/2720"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f438f9bf-13a0-6a7f-b27f-80e006c312e2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A0BC:20E237:27851C3:34A808F:6A6557A5 |
| html-safe-nonce | dae1736eaa992e10fc0e21fb744384420e8660a956d7fc6f9edbe225d11aa251 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMEJDOjIwRTIzNzoyNzg1MUMzOjM0QTgwOEY6NkE2NTU3QTUiLCJ2aXNpdG9yX2lkIjoiMjQwNTUwMTMwMTgzMTMyNTYwNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | eb6b6f9add287f446309bd2ce2077f23dffc3534f60b5265288ac75840966837 |
| hovercard-subject-tag | issue:1328862095 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/mockito/mockito/2720/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bf37098773323bdf5fa54675ff2419edfb6b1220e8c4c62022268912c31af0ee/mockito/mockito/issues/2720 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bf37098773323bdf5fa54675ff2419edfb6b1220e8c4c62022268912c31af0ee/mockito/mockito/issues/2720 |
| og:image:alt | I'm trying to use Mockito in a test of a Java class I didn't write. It takes a dependency which I am stubbing out using Mockito. Skipping all of the business logic, the code ends up being rather si... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | j-baker |
| hostname | github.com |
| expected-hostname | github.com |
| None | 52c76df668885aaff23b50bdca1fa1ea44ac9c1553e888ebc70ff1e4daa4625b |
| turbo-cache-control | no-preview |
| go-import | github.com/mockito/mockito git https://github.com/mockito/mockito.git |
| octolytics-dimension-user_id | 2054056 |
| octolytics-dimension-user_login | mockito |
| octolytics-dimension-repository_id | 6207167 |
| octolytics-dimension-repository_nwo | mockito/mockito |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 6207167 |
| octolytics-dimension-repository_network_root_nwo | mockito/mockito |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 309153364422b3c499922d1a2a6404910a58ed8e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width