title: How to Improve WCF Data Services Performance
body: <p>I'm new to WCF Data Services so I've been playing. After some initial tests I am disappointed by the performance of my test data service.</p>
  
  <p>I realize that because a WCF DS is HTTP-based there is overhead inherent in the protocol but my tests are still way slower than I would expect:</p>
  
  <p>Environment:</p>
  
  <ul>
  <li>All on one box: Quad core 64-bit laptop with 4GB RAM running W7. Decent machine.</li>
  <li>Small SQL database (SQLExpress 2008 R2) with 16 tables... the table under test has 243 rows.</li>
  <li>Hosted my test service in IIS with all defaults.</li>
  </ul>
  
  <p>Code:</p>
  
  <ul>
  <li>I've created a Entity Framework model (DataContext) for this database (stock codegen by VS2010).</li>
  <li>I've created a data-service based on this model.</li>
  <li>I've created a client which has a direct service reference (ObjectContext) for this service (stock codegen by VS2010)</li>
  <li>In the client I am also able to call the EF model directly and also use Native SQL (ADO.NET SqlConnection)</li>
  </ul>
  
  <p>Test Plan:</p>
  
  <ul>
  <li>Each iteration connects to the database (there is an option to reuse connections), queries for all rows in the target table ("EVENTS") and then counts them (thus forcing any deferred fetches to be performaed).</li>
  <li>Run for 25 iterations each for Native SQL (SqlConnection/SqlCommand), Entity Framework (DataContext) and WCF Data Services (ObjectContext).</li>
  </ul>
  
  <p>Results:</p>
  
  <ul>
  <li>25 iterations of Native SQL: 436ms</li>
  <li>25 iterations of Entity Framework: 656ms</li>
  <li>25 iterations of WCF Data Services: 12110ms</li>
  </ul>
  
  <p>Ouch. That's about 20x slower than EF.</p>
  
  <p>Since WCF Data Services is HTTP, there's no opportunity for HTTP connection reuse, so the client is forced to reconnect to the web server for each iteration. But surely there's more going on here than that.</p>
  
  <p>EF itself is fairly fast and it's the same EF code/model is reused for both the service and the direct-to-EF client tests. There's going to be some overhead for Xml serialization and deserialization in the data-service, but that much!?! I've had good performance with Xml serialization in the past.</p>
  
  <p>I'm going to run some tests with JSON and Protocol-Buffer encodings to see if I can get better performance, but I'm curious if the community has any advice for speeding this up.</p>
  
  <p>I'm not strong with IIS, so perhaps there are some IIS tweaks (caches, connection pools, etc) that can be set to improves this?</p>