<commit_before>using System;
using System.Data;

using NHibernate.Connection;
using NHibernate.Driver;
using NHibernate.DomainModel;

using NUnit.Framework;

namespace NHibernate.Test 
{

	/// <summary>
	/// Does some quick & dirty Performance Tests.
	/// </summary>
	/// <remarks>
	/// <para>
	/// The results of these tests are highly dependant upon your log4net settings. NHibernate
	/// logs alot of information and some of the log messages involve string concatenation and 
	/// calling other methods to generate a useful message.  
	/// </para>
	/// <para>
	/// On a production machine, the finest level of detail you should log is WARN.  On my machines
	/// test take about 3 times the amount of time to run with a log level of DEBUG compared to WARN.
	/// </para>
	/// <para>
	/// Currently (2004-05-26) NHibernate adds about 20% overhead with everything on the same machine/6% overhead
	/// with Sql Server on a different machine compared to a straight DataReader when
	/// the exact same sql is issued.  NHibernate's DriverConnectionProvider has not implemented a 
	/// Connection Cache or Prepared IDbCommand cache yet.  No cache was configured for the class Simple for
	/// this test.  So there are optimizations that are yet to be made, even then it is performing
	/// well compared to a DataReader.  I plan on making a seperate csproj to store Performance
	/// Test because NProf has problems with getting performance figures from dlls running in NUnit - or
	/// atleast I have not been able to get it to work. 
	/// </para>
	/// <para>
	/// Also test with a larger amount of data in the tables so they can be compared to a DataSet & Type DataSet
	/// will later be added.
	/// </para>
	/// </remarks>
	[TestFixture]
	public class PerformanceTest : TestCase 
	{
		string driverClass = null;
		IDriver driver = null;
		bool prepareSql;

		[SetUp]
		public void SetUp() 
		{
			ExportSchema( new string[] { "Simple.hbm.xml"} );

			driverClass = (string)cfg.Properties["hibernate.connection.driver_class"];
			if(driverClass.IndexOf(",") < 0) 
			{
				driverClass += ", NHibernate";
			}

			driver = (IDriver)Activator.CreateInstance(System.Type.GetType(driverClass));

			string prepare = (string)cfg.Properties[ Cfg.Environment.PrepareSql ] as string;
			if( prepare=="true" )
			{
				prepareSql = true;
			}

		}

		[Test]
		public void Many() 
		{
			IConnectionProvider cp = ConnectionProviderFactory.NewConnectionProvider( Cfg.Environment.Properties );

			long hiber = 0;
			long adonet = 0;
			
			//for(int n = 0; n < 20; n++) 
			for(int n = 0; n < 5; n++) 
			{
				Simple[] simples = new Simple[n];
				object[] ids = new object[n];

				for(int i = 0; i < n; i++) 
				{
					simples[i] = new Simple();
					simples[i].Init();
					simples[i].Count = i;
					ids[i] = (long)i;
				}

				// allow cache to settle

				ISession s = sessions.OpenSession();
				Hibernate(s, simples, ids, n, "h0");
				s.Close();

				IDbConnection c = cp.GetConnection();
				DirectAdoNet(c, simples, ids, n, "j0");
				cp.CloseConnection(c);

				s = sessions.OpenSession();
				Hibernate(s, simples, ids, n, "h0");
				s.Close();

				c = cp.GetConnection();
				DirectAdoNet(c, simples, ids, n, "j0");
				cp.CloseConnection(c);

				// now do timings

				int loops = 30;
				
				for(int runIndex = 1; runIndex < 4; runIndex++) 
				{
 
					long time = DateTime.Now.Ticks;
					for(int i = 0; i < loops; i++) 
					{
						s = sessions.OpenSession();
						Hibernate(s, simples, ids, n, "h" + runIndex.ToString());
						s.Close();
					}
					hiber += DateTime.Now.Ticks - time;

					time = DateTime.Now.Ticks;<commit_msg>changed to use the keyword "using" for session.<commit_after>using System;
using System.Data;

using NHibernate.Connection;
using NHibernate.Driver;
using NHibernate.DomainModel;

using NUnit.Framework;

namespace NHibernate.Test 
{

	/// <summary>
	/// Does some quick & dirty Performance Tests.
	/// </summary>
	/// <remarks>
	/// <para>
	/// The results of these tests are highly dependant upon your log4net settings. NHibernate
	/// logs alot of information and some of the log messages involve string concatenation and 
	/// calling other methods to generate a useful message.  
	/// </para>
	/// <para>
	/// On a production machine, the finest level of detail you should log is WARN.  On my machines
	/// test take about 3 times the amount of time to run with a log level of DEBUG compared to WARN.
	/// </para>
	/// <para>
	/// Currently (2004-05-26) NHibernate adds about 20% overhead with everything on the same machine/6% overhead
	/// with Sql Server on a different machine compared to a straight DataReader when
	/// the exact same sql is issued.  NHibernate's DriverConnectionProvider has not implemented a 
	/// Connection Cache or Prepared IDbCommand cache yet.  No cache was configured for the class Simple for
	/// this test.  So there are optimizations that are yet to be made, even then it is performing
	/// well compared to a DataReader.  I plan on making a seperate csproj to store Performance
	/// Test because NProf has problems with getting performance figures from dlls running in NUnit - or
	/// atleast I have not been able to get it to work. 
	/// </para>
	/// <para>
	/// Also test with a larger amount of data in the tables so they can be compared to a DataSet & Type DataSet
	/// will later be added.
	/// </para>
	/// </remarks>
	[TestFixture]
	public class PerformanceTest : TestCase 
	{
		string driverClass = null;
		IDriver driver = null;
		bool prepareSql;

		[SetUp]
		public void SetUp() 
		{
			ExportSchema( new string[] { "Simple.hbm.xml"} );

			driverClass = (string)cfg.Properties["hibernate.connection.driver_class"];
			if(driverClass.IndexOf(",") < 0) 
			{
				driverClass += ", NHibernate";
			}

			driver = (IDriver)Activator.CreateInstance(System.Type.GetType(driverClass));

			string prepare = (string)cfg.Properties[ Cfg.Environment.PrepareSql ] as string;
			if( prepare=="true" )
			{
				prepareSql = true;
			}

		}

		[Test]
		public void Many() 
		{
			IConnectionProvider cp = ConnectionProviderFactory.NewConnectionProvider( Cfg.Environment.Properties );

			long hiber = 0;
			long adonet = 0;
			
			//for(int n = 0; n < 20; n++) 
			for(int n = 0; n < 5; n++) 
			{
				Simple[] simples = new Simple[n];
				object[] ids = new object[n];

				for(int i = 0; i < n; i++) 
				{
					simples[i] = new Simple();
					simples[i].Init();
					simples[i].Count = i;
					ids[i] = (long)i;
				}

				// allow cache to settle

				ISession s = sessions.OpenSession();
				Hibernate(s, simples, ids, n, "h0");
				s.Close();

				IDbConnection c = cp.GetConnection();
				DirectAdoNet(c, simples, ids, n, "j0");
				cp.CloseConnection(c);

				s = sessions.OpenSession();
				Hibernate(s, simples, ids, n, "h0");
				s.Close();

				c = cp.GetConnection();
				DirectAdoNet(c, simples, ids, n, "j0");
				cp.CloseConnection(c);

				// now do timings

				int loops = 30;
				
				for(int runIndex = 1; runIndex < 4; runIndex++) 
				{
 
					long time = DateTime.Now.Ticks;
					for(int i = 0; i < loops; i++) 
					{
						using( s = sessions.OpenSession() ) 
						{
							Hibernate(s, simples, ids, n, "h" + runIndex.ToString());
						}
					}
					hiber += DateTime.Now.Ticks - time;

					time = DateTime.Now.Ticks;
{'fraction_non_alphanumeric': 0.12344667486003004, 'fraction_numerical': 0.008466475488187901, 'mean_word_length': 2.7811048012390294, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5649109', 'n_tokens_mistral': 2758, 'n_tokens_neox': 2250, 'n_words': 810}