Entity Framework vs Linq to SQL vs Repository Factory: Performance

by Jesse 12. July 2010 23:04

It has been long time since my last post. It really dues to the heavy workload in my job(I hope my manager can see thisTongue out) and my health problem. And with the release of VS2010 and ADO.NET 4.0, I am very interesting to know what is the "best" "orm" I can use in .net world now. I have quoted "best", because I dont want to argue with anyone which one is best for you. I also quoted "orm", because repository factory is not really an orm.

First I'd like to say a little bit of my background in using ORM. In the old days(before microsoft causes any chaos by releasing two unfinished two productions, linq to sql and entity framework), I was really interested in nhibernate, the biggest reason is that it supports many databases and mono(.net framework on linux). But honest speaking, I dont believe nhibernate is a good choice in any medium to large commercial projects. At least your DBA will never let the dynamic queries generated by nhibernate to select, insert or delete tables. Especaily in the production environment of SQL Server, stored procedures are pretty much everything you can access from code. It basically makes nhibernate useless. However, in certain projects, it does add a owo-factor, such as CMS. I created a nhibernate CMS in my previous job about 3 years, it heavily reduced the cost of hosting by using linux mono + mysql, and it also gives marketing people a strong sale point. Because it can be easily fitted into clients' exist IT infrastructure, using exist server, no matter linux or window, using exist db with many choice. If you comes to this page to find a CMS, you may interest to have a look at two open-source nhibernate CMS. Cuyahoga and Eucalypto.

Other ORM I looked at, NBear is another good one. But it apparently dead, the last update in sourceforge for this project was two years ago. What a shame!!! There are many other ORMs, I dont want to list them all, because as you know, I always can find a reason for no using it.Smile

Back to our main story, repository factory is a microsoft product (kind of). And released by Microsoft pattern & practice team. It is a great light-weight "ORM". After all, everything I need is a mapping system using stored procedure. Support different database? nope, but the chance of my company changing database is very low, especially after spending 50k+ on license. Support mono? nope, but maybe nobody else knows what mono project is. :p. So it is from microsoft and it does the job. It is a perfect little tool!! Until VS2008 coming out. The GUI tools did not support vs2008. But a separate team release a vs2008 version, and I have to compile from source code in order to fix some bugs. Now vs2010 is released, I think it is a good time to rethink DAL and possible a different ORM to replace. 

Linq to SQL and Entity Framework becomes two top choices to me. The reason is simple, firstly I dont want to pay, this push PLINQO (Linq to SQL extension, need CodeSmith, paidware!!!) and XPO out my choice. Secondly they comes from microsoft(it may not be a good reason, but keep the technologies used from the heads of same group people wont be a bad idea). I did a little search on google, and I found out there were quite a lot of poeple discussing the performance of these two ORM. So how much performance penalty will I have for using them? I decided to do a small test by myself. 

The test project is simple. A simple database with three columns, TSQL code shown below:

USE [TestDb]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Table_1](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[Description] [ntext] NULL,
CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

 

Five store procedures for insert, select on primary key, select on index key, update and delete. 

Create L2S dbml, EFv4 edmx using vs2010. I did not do any optimisations, there are a few performance tips you can find from google. But I personally wont bothered to use them in this test. AS repository factory GUI tool does not work with vs2010, I copied the code from previous project and amend it.

The test result is VERY interesting!

1. I know the first call to EF is slow due to generate views, but the first call to repository factory(RF) is almost as slow as EF

2. I read some post said L2S is slow on inserting, but actually it is not bad

3. L2S and EF on inserting are almost same

4. L2S selecting is slowest

5. RF and EF on selecting are almost same, which takes almost 2.5 times more time comparing with ADO.NET

Here is the result from two runs:

Run 1:
RF Insert Test: 1
Time used: 115
EF Insert Test: 1
Time used: 114
L2S Insert Test: 1
Time used: 14
ADO Insert Test: 1
Time used: 8
RF Insert Test: 1000
Time used: 797
EF Insert Test: 1000
Time used: 994
L2S Insert Test: 1000
Time used: 897
ADO Insert Test: 1000
Time used: 412
RF Select Test(PK): 1000
Time used: 259
EF Select Test(PK): 1000
Time used: 230
L2S Select Test(PK): 1000
Time used: 396
ADO Select Test(PK): 1000
Time used: 101
RF Select Test(Index): 1000
Time used: 270
EF Select Test(Index): 1000
Time used: 236
L2S Select Test(Index): 1000
Time used: 406
ADO Select Test(Index): 1000
Time used: 118

Run 2:
RF Insert Test: 1
Time used: 59
EF Insert Test: 1
Time used: 88
L2S Insert Test: 1
Time used: 11
ADO Insert Test: 1
Time used: 1
RF Insert Test: 1000
Time used: 725
EF Insert Test: 1000
Time used: 877
L2S Insert Test: 1000
Time used: 800
ADO Insert Test: 1000
Time used: 538
RF Select Test(PK): 1000
Time used: 246
EF Select Test(PK): 1000
Time used: 241
L2S Select Test(PK): 1000
Time used: 381
ADO Select Test(PK): 1000
Time used: 100
RF Select Test(Index): 1000
Time used: 263
EF Select Test(Index): 1000
Time used: 245
L2S Select Test(Index): 1000
Time used: 393
ADO Select Test(Index): 1000
Time used: 118

Unit: ms

In summary, I think the overall performance of EF is not as bad as I think. I know it is a still a simple test, with a lot more complex object and mapping, it may comes a totally different result. But this test still give me a little bit confidence to try EF in next project.

I will attach the test code later, and I do like to know what's in your mind. Please do comment.

 

Install Windows 7 x64 on iMac 24

by Jesse 24. October 2009 00:42

Preorder windows 7 when its price is only £99, and happily received it yesterday (Have to travel 15 miles to pick it up local depot of CityLink though). I tried to install it on my iMac today, but as I expected, it has the same issue mentioned in my previous blog.

http://elian.co.uk/post/2009/05/11/Installing-VISTA-x64-on-iMac.aspx

Update:
I can confirm the old solution still works, just little note that, you need to disable UAC or use Run as administrator to install BootCamp drivers.

Rollback to iPod Touch Firmware 3.0 (From 3.1.1)

by Jesse 26. September 2009 01:33

After a long and stressful week, maybe I am retarded I just hits the firmware upgrade button in my iTune when iPod touch connected. The result is possibly the reason you comes to see this page.

I google a little bit, but not many people say anything about how to rollback to 3.0 from 3.1 or 3.1.1. I just have to take a little risk to downgrade it. Luckily I havent upgrade itune to 9, always a wise choice to stay with latest-1 version ^_^. Anyway if you have itune 9, just leave this page, I cannot help you with it (I heard that even uninstall itune 9, you wont be enable to push 8.2 back on, so maybe the only way is reinstall OS!! That's hours work). First I try shift+upgrade firmware, it DOESN'T work, and put my iPod touch in recover mode. I have to force the device into DFU mode (Device Firmware Upgrade mode?) by holding power+home, when everything gone, release power botton while home button is still down and plug USB cable. You will see some windows pop up, open itune, shift+retore. Yeah, it works. It is not really suprise me actually. Because DFU restore method should be able to let you rollback to any version.

If you have any question to ask or to discuss or to complain (I did have compains from some one, and that's why some posts were removed!!!)
Please leave comment or use my contact page.

Jesse 

Snow Leopard released

by Jesse 28. August 2009 10:43

Good news, no it is a great news.

New Mac OS X 10.6, also known as Snow Leopard is released today. Yeah!!!!

Tags: , ,
Categories: OS | Mac OS

Bouncy Castle C#

by Jesse 29. July 2009 11:54

It has been really long time since my last post. And finally I get a few minutes to write this post on using BouncyCacle encryption in C#, hopefully it can help some one.

BouncyCastle is a encryption library widely used in Java, and has been porting to C# ages ago. But due to lack of documentation, I believe many people including myself have much trouble on implement any encrypt/decrypt functions by using it. Right, let's start.

 

First of all, you need to get BouncyCastle library from its website on http://www.bouncycastle.org/csharp/. It is not necessary to get the source code. Also, there are two type of binary format libraries you can download. One is with IDEA, and another is not. If you dont know which one you need, just download the one without IDEA. As IDEA is patented in many country, so you must be more careful when choosing to use.

My implementation consist of two  parts: block cipher encryption engine, and encryption/decryption interface.

BCEngine class (Block cipher engine)

public class BCEngine
    {
        private readonly Encoding _encoding;
        private readonly IBlockCipher _blockCipher;
        private PaddedBufferedBlockCipher _cipher;
        private IBlockCipherPadding _padding;

        public BCEngine(IBlockCipher blockCipher, Encoding encoding)
        {
            _blockCipher = blockCipher;
            _encoding = encoding;
        }

        public void SetPadding(IBlockCipherPadding padding)
        {
            if(padding != null)
                _padding = padding;
        }

        public string Encrypt(string plain, string key)
        {
            byte[] result = BouncyCastleCrypto(true, _encoding.GetBytes(plain), key);
            return Convert.ToBase64String(result);
        }

        public string Decrypt(string cipher, string key)
        {
            byte[] result = BouncyCastleCrypto(false, Convert.FromBase64String(cipher), key);
            return _encoding.GetString(result);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="forEncrypt"></param>
        /// <param name="input"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        /// <exception cref="CryptoException"></exception>


        private byte[] BouncyCastleCrypto(bool forEncrypt, byte[] input, string key)
        {
            try
            {
                _cipher = _padding == null ? new PaddedBufferedBlockCipher(_blockCipher) : new PaddedBufferedBlockCipher(_blockCipher, _padding);
                byte[] keyByte = _encoding.GetBytes(key);
                _cipher.Init(forEncrypt, new KeyParameter(keyByte));
                return _cipher.DoFinal(input);
            }
            catch (Org.BouncyCastle.Crypto.CryptoException ex)
            {
                throw new CryptoException(ex);
            }
        }
    }

 

The encryption/decryption interface:

public string AESEncryption(string plain, string key, bool fips)
        {
            BCEngine bcEngine = new BCEngine(new AesEngine(), _encoding);
            bcEngine.SetPadding(_padding);
            return bcEngine.Encrypt(plain, key);
        }

        public string AESDecryption(string cipher, string key, bool fips)
        {
            BCEngine bcEngine = new BCEngine(new AesEngine(), _encoding);
            bcEngine.SetPadding(_padding);
            return bcEngine.Decrypt(cipher, key);
        }

 

You can easily change the BouncyCastle engine to Blowfish, DES, TripleDES, TwoFish, etc.

Firefox 3.5 supports HTML 5

by Jesse 1. July 2009 16:16

Yesterday 30/06/2009, one of the most famous internet browser, and my favourite browser, released a new version 3.5 and becomes one of very first browser supports HTML 5 <video> and <audio> tag.

Following is the new features in FF 3.5 release note.

    *  Available in more than 70 languages. (Get your local version!)
    * Support for the HTML5 <video> and <audio> elements including native support for Ogg Theora encoded video and Vorbis encoded audio. (Try it here!)
    * Improved tools for controlling your private data, including a Private Browsing Mode.
    * Better web application performance using the new TraceMonkey JavaScript engine.
    * The ability to share your location with websites using Location Aware Browsing. (Try it here!)
    * Support for native JSON, and web worker threads.
    * Improvements to the Gecko layout engine, including speculative parsing for faster content rendering.
    * Support for new web technologies such as: downloadable fonts, CSS media queries, new transformations and properties, JavaScript query selectors, HTML5 local storage and offline application storage, <canvas> text, ICC profiles, and SVG transforms.

I have already downloaded and installed the new version, to be honst, you cannot really tell any different, and there are only few HTML 5. The Video Bay may be the most popular one because of the success of its founder TPB.

Tags: , , , , , ,
Categories: General | IT | News | Me | Web

Paypal bugged their logon

by Jesse 24. June 2009 11:03

To all 150 million Paypal users (The number is what they say on their homepage):

Can you believe a company can make your money better than their website? Tried to logon this morning during 9:50 - 10:20, keep getting error message showing below:

 

 

Welcome any comments on problem using Paypal!!! Maybe we should let them know what WE, the customers, think.

Tags: , , ,
Categories: IT | Security | News

TTPlayer dll

by Jesse 19. June 2009 15:08

上传一个TTPlayer的DLL, 想知道它是干什么的吗? 请用正确的email留言, 我会Email给你所有的细节. 现在所能说的就是, 它与TTPlayer的安全漏洞有关, 相关讨论可以在

http://bbs.chinaunix.net/viewthread.php?tid=1340433&extra=&page=2
http://bbs.chinaunix.net/viewthread.php?tid=1341275

Jesse

 

lrcsh.rar (19.00 kb)

Got iPhone OS 3 installed

by Jesse 18. June 2009 01:18

Finally get iPhone OS 3 installed. Nothing exciting, most new features on adv are only on 3GS. Will play with it longer and write a personally review later.

Jesse

Tags: , , ,
Categories: Apple | iPhone | iPhone OS 3

2.5 hours before iPhone OS 3 release

by Jesse 16. June 2009 21:23

The year 2009 is big, UK is still in recession, everything is getting cheaper, well including the pay! Many good movices comes and will come out, they all with BIGGGGG names, and always labelled as boys-must-see. Laughing

At the big year and with many big names, Apple decides to release iPhone OS 3 and 3GS. I know many people may or may not excited as me, the level of exciting, basically, defined the level of you either Apple fans or AA (Anti-Apple). The arguments in my office about Apple, Mac, iPhone, I believe, will last forever. However, I did see some AAs put their fat ass in front of a Mac and started to play iPhone. Apple fans, we will win the battle!!!!

Right, after 2.5 hours, I will put myself in the queue, no matter how long it will. and install OS 3 on my love iPhone 3G. ^_^

 

Update:

17:44 BST, with a little bit disappoint, iPhone os 3 is still not availble on iTune.

18:51 BST, iPhone OS 3 IS in iTune now!!!!!! Downloading......

Tags: , , ,
Categories: Apple | iPhone

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen | Modified by Mooglegiant