-1

I'm using fluent nHibernate as ORM and mapping the classes using auto mapping in fluenthibernate.The classes would be mapped only once as i'm calling them in global.asax .For some reason the CPU is spiking up to high 90% sometimes .I profiled the application using ANTS performance profiler and found that the Configure() property is consuming 5% of cpu and buildsessionfactory() which calls the automapper is consuming 20% of the CPU.I debugged the application and cannot find what the problem is .What might be the problem ?

Here is my code

public static ISessionFactory GetSessionFactory()

{
    lock (factorylock)
    {
        if (_sessionFactory == null)
        {

  string scriptLocation = HttpContext.Current.IsDebuggingEnabled
    ? HostingEnvironment.MapPath( ConfigurationManager.AppSettings["GetDataBaseScript"] )
    : null;

  FluentConfiguration config = Fluently
      //// Start the configuration
    .Configure() **--- This is the place where 5% of memory is being used**
      // Setup the database configuration
    .Database(
      // Configure for MS SQL Server connection
      MsSqlConfiguration
      // use MS SQL Server 2010
        .MsSql2008
      // Specify the connection string
        .ConnectionString(c => c.FromConnectionStringWithKey("DatabaseConnection"))
    )



    // Set up the mappings
    .Mappings( maps =>
      maps
        Setup mappings
        .AutoMappings
        // Load mappings from assembly
        .Add( AutoMap
          // Load auto maps configuration from assembly
          .AssemblyOf<AutoMapConfiguration>(new AutoMapConfiguration() )
          // Specify some custom conventions for Auto Map
          .Conventions.AddFromAssemblyOf<AutoMapConfiguration>()

          // Override some automapping configuration
          .UseOverridesFromAssemblyOf<AutoMapConfiguration>()
        )

    );


  // determine if we need to generate the DB or just build the configuration
  if( !String.IsNullOrEmpty( scriptLocation ) ) {
    // Where to generate the script file
    // Expose the configuration
    config.ExposeConfiguration( c => {
      // Export the schema
      new SchemaExport( c )
        // Specify where the script is to be exported
        .SetOutputFile( scriptLocation )
        .Create( true, false );
    } );
  } else {
    // Build the configuration
    config.BuildConfiguration();
  }

  // Return the NHibernate Session Factory
   _sessionFactory = config.BuildSessionFactory(); **--- This is the place where 20% is being used**
        }
        return _sessionFactory;
    }


}
Yoda
  • 319
  • 2
  • 5
  • 20

1 Answers1

0

Building the SessionFactory is a heavy weight operation which should be only happening once at application startup. If you want to optimize it a possible solution could be to serialize/deserialize it.

Example: http://nhibernate.info/blogs/nhibernate/archive/2009/03/13/an-improvement-on-sessionfactory-initialization.html

hessenmob82
  • 175
  • 12
  • I'm calling it only during the application start up. – Yoda Jan 07 '15 at 00:31
  • Thats what I assumed when I saw your null check on _sessionFactory. If you profile the start up, how much time is spend in System.XML namespace? Maybe this could help you. http://ayende.com/blog/2903/real-world-nhibernate-reducing-startup-times-for-large-amount-of-entities – hessenmob82 Jan 07 '15 at 09:16