Advertisement
Guest User

Appfabric vs Sqlserver

a guest
Jul 14th, 2011
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication3
  7. {
  8.     using System.ComponentModel;
  9.     using System.Data;
  10.     using System.Data.SqlClient;
  11.     using System.Diagnostics;
  12.     using System.Globalization;
  13.     using System.IO;
  14.     using System.Linq.Expressions;
  15.     using System.Runtime.Serialization.Formatters.Binary;
  16.     using System.Threading;
  17.     using System.Xml.Linq;
  18.  
  19.     using Microsoft.ApplicationServer.Caching;
  20.  
  21.     class Program
  22.     {
  23.         private static readonly DataCache cache = new DataCacheFactory().GetDefaultCache();
  24.  
  25.         private static SqlConnection cn = new SqlConnection("Data Source=localhost; Initial Catalog=Test; Integrated Security=True;");
  26.         private static SqlCommand command = new SqlCommand("Select Value from Cache where [Key] = @key", cn);
  27.  
  28.         private static int maxValue = 100000;
  29.  
  30.         private const string Region = "Test";
  31.  
  32.         private const string data = "dfla jfaslkdfjs;kjsad;fl sopriwurowurpwqi urwrioq weropw ruwqropi wqrop iwrowi urworiu woruiw rowiurw oruiuw rwskldfjsakf,er wq';erkl 'rw;qklr w'ru woeirupqcoinrpo cnopiwur nqwoeri vuqw quw vopwruq wporq wvueporiu pwouri ouq hqerkqw lckbqwrqw oprui2 r2iou3 p2o uiurm2poiur21po3i1 2opui314 mop23ui4214 m129-4m812-480 cm;sokjf pos ciq[w qf'[wfiw'[epiq[riw[tiopq tql hqkljncr123oiu12priu2  3h4jh f'alskf asqwiotu qwuiqwerqwrlkh ";
  33.         static void Main(string[] args)
  34.         {
  35.             cache.CreateRegion(Region);
  36.             cn.Open();
  37.             command.Parameters.Add("@key", SqlDbType.NVarChar);
  38.  
  39.  
  40.             var sw = new Stopwatch();
  41.             sw.Start();
  42.  
  43.             FillFabric();
  44.             sw.Stop();
  45.             Console.WriteLine("Fill cache: " + sw.ElapsedMilliseconds);
  46.  
  47.             sw.Restart();
  48.             FillSql();
  49.             sw.Stop();
  50.  
  51.             Console.WriteLine("Fill sql: " + sw.ElapsedMilliseconds);
  52.  
  53.             sw.Restart();
  54.             RandomRead(ReadCache);
  55.             sw.Stop();
  56.             Console.WriteLine("Read cache: " + sw.ElapsedMilliseconds);
  57.  
  58.  
  59.             sw.Restart();
  60.             RandomRead(ReadSql);
  61.             sw.Stop();
  62.             Console.WriteLine("Read sql: " + sw.ElapsedMilliseconds);
  63.  
  64.             Console.Read();
  65.         }
  66.  
  67.         private static void ReadSql(string key)
  68.         {
  69.             command.Parameters["@key"].Value = key;
  70.             command.ExecuteScalar();
  71.         }
  72.  
  73.         private static void ReadCache(string key)
  74.         {
  75.             cache.Get(key, Region);
  76.         }
  77.  
  78.         private static void RandomRead(Action<string> action)
  79.         {
  80.             for (int i = 0; i < maxValue; i++)
  81.             {
  82.                 var rnd = new Random().Next(0, maxValue);
  83.  
  84.                 action(rnd.ToString());
  85.             }
  86.         }
  87.  
  88.         private static void FillFabric()
  89.         {
  90.             for (int i = 0; i < maxValue; i++)
  91.             {
  92.                 cache.Put(i.ToString(), data + i, Region);
  93.             }
  94.         }
  95.  
  96.         private static void FillSql()
  97.         {
  98.             using (var cmd = new SqlCommand())
  99.             {
  100.                 cmd.CommandType = CommandType.Text;
  101.                 cmd.CommandTimeout = 60000;
  102.                 cmd.Connection = cn;
  103.                 cmd.Parameters.Add("@p1", SqlDbType.NVarChar);
  104.                 cmd.Parameters.Add("@p2", SqlDbType.NVarChar);
  105.                 for (int i = 0; i < maxValue; i++)
  106.                 {
  107.                     cmd.CommandText = "insert into Cache VALUES (@p1, @p2)";
  108.                     cmd.Parameters["@p1"].Value = i.ToString();
  109.                     cmd.Parameters["@p2"].Value = data + i;
  110.                     cmd.ExecuteNonQuery();
  111.                 }
  112.             }
  113.            
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement