Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 117577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:21:30+00:00 2026-05-11T03:21:30+00:00

In my profiler reports I’m increasingly seeing the results of mock-based testing with dependency

  • 0

In my profiler reports I’m increasingly seeing the results of mock-based testing with dependency injection. Many of the dependencies were static, but because we want to test methods in isolation they are changed to instance members, like the following example:

class ShortLivedThing {    IDependency1 dep1;    IDependency1 dep2;    IDependency1 dep3;    ...     int TheRealData;     // Constructor used in production     public ShortLivedThing() {      dep1 = new Dep1(); dep2 = new Dep2(); dep3 = new Dep3();    }     // DI for testing     public ShortLivedThing(IDependency1 d1, IDependency2 d2, IDependency3 d3) {       dep1 = d1(); dep2 = d2(); dep3 = d3();    } } 

In turn the dependencies most of the time have other dependencies and so on. This results in the instantiation of a tree of (mostly ‘static’) objects every time a method call is done outside of tests. Each of the objects are very small (just a few pointers), but the tree effect turns this into an ever increasing performance hit.

What can we do about it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:21:31+00:00Added an answer on May 11, 2026 at 3:21 am

    It seems to me like you need to leverage the features a proper dependency injection framework can give you. Do not use different construction logic for testing/production.

    With spring, singleton injections are only performed at container startup. Prototype injections are done every time. The full wiring is also done each time you run a unit test, if it’s being wired. So profiling unit tests is generally not a good idea.

    Maybe you’re using too little of the singleton scopes and too much prototype scope ? (Prototype = new instance every time)

    The nice thing about spring injection is that you can use scope proxies, meaning your object graph can look like this:

     A Singleton  |  B Singleton  |  C Prototype (per-invocation)  |  D Singleton  |  E Session scope (web app)  |  F Singleton 

    And each request would only create 1 instance of C and one instance of E per session. A, B, D and F are singletons. If it’s not a webapp you dont have session scope by default, but you can also make custom scopes (a ‘Window’ scope sounds cool for a windowed desktop app). The clue here is that you can ‘introduce’ scopes at any level, effectively you can have ten layers of singleton objects and all of a sudden something session scoped shows up. (This can really revolutionize how you implement some cross-cutting features in a layered architecture but that’s a different story)

    This really gives the minimum object creation possible within a DI model, I think.

    Although this is Spring for Java I believe a number of other DI frameworks should support similar features. Maybe not the most minimalistic ones.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the new MySQL Query Profiler in PHPMyAdmin version 2.11.5, I got following results
Upon putting jasper reports dependency : <dependency> <groupId>jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>3.5.0</version> </dependency> in my pom.xml,
When using the profiler in Visual Studio to track down expensive functions, I've seen
SQL Server 2008 Profiler always profiles all databases. I want to restrict profiling or
SQL Server 2005 Profiler shows that a Stored Procedure (SP) was called and what
Apart from Sql server profiler ,is there any third party user friendly tool available
When I ran the profiler on a JQuery intensive page while tuning for performance,
Is there any profiler sort of tool which can debug which javascript/object in the
Does NetBeans profiler work with Eclipse?
If I run Profiler, then it suggests a lot of indexes like this one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.