Owin + Unity container is bad performance

422 Views Asked by At

I Use JMeter to make load to my WebApi. 100 user + 4000 loop for every user.

  1. Simple OWIN: avg 5-10ms, max response 200 ms II. OWIN
  2. Microsoft Unity container: avg 15 ms, max response 9000ms.

Performance charts

enter image description here

I have OWIN self-hosted WebApi + Microsoft unity container.

With VS performance explorer I investigated hot Paths:

 public IDependencyScope BeginScope()
    {
      var childContainer = Container.CreateChildContainer(); //47% CPU

      return new UnityDependencyScope(childContainer);
    }

    public object GetService(Type serviceType)
    {
       if (typeof(IHttpController).IsAssignableFrom(serviceType))
       {
          return Container.Resolve(serviceType); //35% CPU
       }

       try
       {
          return Container.Resolve(serviceType);
       }
       catch
       {
          return null;
       }
   }

    public void Dispose()
    {
        Container.Dispose(); //16%CPU
    }

How to optimize Unity container use or where is problem?

1

There are 1 best solutions below

0
On

If your controller depends on too many objects for construction, it may take some time building all object graph.

For every request that hit your controller, you are going to instantiate all of the dependencies even if you don't need any of them at all.

You can try splitting your one big controller into smaller multiple controllers and refactor your object graph (dependency tree) to keep it minimum per controller.