I will continue blogging on my website http://thatstoday.com/robbanp/blog/archive/1
Please check it out!
Monday, July 20, 2009
Sunday, March 22, 2009
CSharp Performance – Cast vs Interface
Writing high performance applications is a challenge since you can do the same thing in so many ways. Almost everything you code can be done in many ways that are slow but can be improved to be faster. But on the contrary to this article is my experience on building high traffic web sites, is that it is almost never the application layer that is slow but almost always the data access and text/XML parsing.
But anyway, I spend so much time on thinking how code can be faster and more slimmed ☺
Since I develop more and more abstract and generic functionality using techniques such as Generics and Interfaces, I think of how this will improve both readability and performance. What I want to show in this article, is the performance benefits of using a Interface as a input type instead of an Object.
Always when you use Object as the parameter data type, to create an abstract or dynamic method you need to Cast the object into your desired class. This cast will be negative on the execution time and also add risk for errors. I also think it is negative on readability since you can’t really see the desired input type.
Performance test:
In this performance test I have a basic class that implements an interface. I have two methods that execute the test methods doing a cast, and using the interface methods.
The test object is created 1.000.000 times for each method, which are looped 10 times to compare execution times. As you can see the fastest way is to use an Interface to execute the method. On my machine the Interface method takes 7-8ms, and the cast method takes 15-22ms.
Here is the code:
But anyway, I spend so much time on thinking how code can be faster and more slimmed ☺
Since I develop more and more abstract and generic functionality using techniques such as Generics and Interfaces, I think of how this will improve both readability and performance. What I want to show in this article, is the performance benefits of using a Interface as a input type instead of an Object.
Always when you use Object as the parameter data type, to create an abstract or dynamic method you need to Cast the object into your desired class. This cast will be negative on the execution time and also add risk for errors. I also think it is negative on readability since you can’t really see the desired input type.
Performance test:
In this performance test I have a basic class that implements an interface. I have two methods that execute the test methods doing a cast, and using the interface methods.
The test object is created 1.000.000 times for each method, which are looped 10 times to compare execution times. As you can see the fastest way is to use an Interface to execute the method. On my machine the Interface method takes 7-8ms, and the cast method takes 15-22ms.
Here is the code:
#region
using System;
using System.Diagnostics;
#endregion
namespace InterfaceTest
{
internal class Program
{
private static void Main(string[] args)
{
Stopwatch stopWatch = new Stopwatch();
TimeSpan ts;
for (int i = 0; i < 10; i++)
{
stopWatch.Start();
TestCast();
stopWatch.Stop();
ts = stopWatch.Elapsed;
string elapsedTime = String.Format("Casting {0:00}:{1:00}:{2:00}sec.{3:00}ms",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds/10);
Console.WriteLine(elapsedTime, "RunTime Cast");
stopWatch.Reset();
stopWatch.Start();
TestInterface();
stopWatch.Stop();
ts = stopWatch.Elapsed;
elapsedTime = String.Format("Interface {0:00}:{1:00}:{2:00}sec.{3:00}ms",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds/10);
Console.WriteLine(elapsedTime, "RunTime Interface");
}
Console.ReadKey();
}
private static void TestInterface()
{
for (int i = 0; i < 1000000; i++)
{
TestClass tc = new TestClass();
DoInterface(tc);
}
}
private static void DoInterface(ITest tc)
{
tc.Read("hello speed test");
tc.DoIt("hello world");
}
private static void TestCast()
{
for (int i = 0; i < 1000000; i++)
{
TestClass tc = new TestClass();
DoCast(tc);
}
}
private static void DoCast(object obj)
{
TestClass tc = obj as TestClass;
tc.Read("hello speed test");
tc.DoIt("hello world");
}
#region Nested type: ITest
public interface ITest
{
bool DoIt(string testString);
void Read(string str);
}
#endregion
#region Nested type: TestClass
public class TestClass : ITest
{
private string _testString;
public TestClass()
{
_testString = "empty";
}
#region ITest Members
public void Read(string str)
{
_testString = str;
}
public bool DoIt(string testString)
{
if (testString == _testString)
{
return true;
}
else
{
return false;
}
}
#endregion
}
#endregion
}
}
Labels:
.net,
c#,
performance
Friday, March 20, 2009
Cached JavaScript minification on the fly – ASP.NET and HttpHandler
If you have a lot of external JavaScript files you should always have a minified version of them when you go public. All popular JS frameworks such as JQuery and Prototype have minified versions, often on a CDN. But how do you do when you have a lot of your own code that you need to minify? There are a lot of code libraries that handles the minification such as JSMin and YUI Compressor that compress your .js files to minified .js files so you can upload them to your server.
My problem is that I have a lot of JavaScript files that are often updated and I don’t want to manually minify the files that are updated. So I figured: Why not do it on the fly? Map all *.js requests on the server to a HttpHandler that minifies the file, and returns the new version. Also add a little cache so it only does this once.
So now I don’t need to worry about a thing when it comes to minify JavaScript files, the server takes care of that automatically!
Here is the code
The HttpHandler called JSMinify is invoked on all requests to *.js files. Activate the handler I web.config:
Here is the Handler. As you can see I use the Request.PhysicalPath as the path to the file.
Here is my FileCache class that looks for a file on disk, and adds it to the web cache. The method GetTextFile is overloaded to handle a parser that implements IFileParser. I created IFileParser so I can have custom text parsing before I add the text into the cache. I use the file path as the cache key, and the cache settings are so that the cache will expire when the files is updated.
In the class JSMinifyParser I have a modified version of the popular library called JSMin. The original JSMin converts one file to another, but I want it to read a file and return the minified string version of that file. So what I did was basically to change the StreamWriter to a TextWriter, and made the method return the string.
So that is basically it! What you need to think about is to map *.js file types to be handled ASP.NET in the IIS settings, otherwise the HttpHandler won’t be triggered. Also there are some differences how you add the handler in web.config for IIS6 and IIS7. Here is a lot of usefull info on IIS7 http://learn.iis.net/page.aspx/26/installing-and-configuring-iis-70/
My problem is that I have a lot of JavaScript files that are often updated and I don’t want to manually minify the files that are updated. So I figured: Why not do it on the fly? Map all *.js requests on the server to a HttpHandler that minifies the file, and returns the new version. Also add a little cache so it only does this once.
So now I don’t need to worry about a thing when it comes to minify JavaScript files, the server takes care of that automatically!
Here is the code
The HttpHandler called JSMinify is invoked on all requests to *.js files. Activate the handler I web.config:
<httpHandlers>
<add verb="*" path="*.js" type="Portal.Web.API.Handlers.JSMinify,Portal.Web"/>
</httpHandlers>
Here is the Handler. As you can see I use the Request.PhysicalPath as the path to the file.
using System.Web;
namespace Portal.Web.API.Handlers
{
public class JSMinify : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpResponse objResponse = context.Response;
string file = context.Request.PhysicalPath;
objResponse.Write(PCache.FileCache.GetTextFile(file,new Parsers.JSMinifyParser()));
}
public bool IsReusable
{
get
{
return true;
}
}
}
}
Here is my FileCache class that looks for a file on disk, and adds it to the web cache. The method GetTextFile is overloaded to handle a parser that implements IFileParser. I created IFileParser so I can have custom text parsing before I add the text into the cache. I use the file path as the cache key, and the cache settings are so that the cache will expire when the files is updated.
using System.IO;
using System.Web.Caching;
using Portal.PCache.Parsers;
namespace Portal.PCache
{
public static class FileCache
{
/// <summary>
/// Gets the text file.
/// </summary>
/// <param name="path">The path.</param>
/// <returns></returns>
public static string GetTextFile(string path)
{
if(Exists(path))
{
return Get(path).ToString();
}
else
{
string data = ReadFile(path);
Add(data,path);
return data;
}
}
/// <summary>
/// Gets the text file using a file parser.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="parser">The parser.</param>
/// <returns></returns>
public static string GetTextFile(string path, IFileParser parser)
{
if (Exists(path))
{
return Get(path).ToString();
}
else
{
string data = parser.Parse(path);
Add(data, path);
return data;
}
}
/// <summary>
/// Reads the file form disk.
/// </summary>
/// <param name="path">The file path.</param>
/// <returns></returns>
private static string ReadFile(string path)
{
TextReader s = new StreamReader(path);
string data = s.ReadToEnd();
s.Close();
s.Dispose();
return data;
}
/// <summary>
/// Adds the specified cache object.
/// </summary>
/// <param name="cacheObject">The cache object.</param>
/// <param name="keyName">Name of the key.</param>
private static void Add(object cacheObject, string keyName)
{
System.Web.HttpContext.Current.Cache.Insert(keyName, cacheObject, new CacheDependency(keyName));
}
/// <summary>
/// Check if object exists in cache
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
private static bool Exists(string key)
{
if (System.Web.HttpContext.Current.Cache[key] != null)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// remove object from cache
/// </summary>
/// <param name="key"></param>
private static void Remove(string key)
{
System.Web.HttpContext.Current.Cache.Remove(key);
}
/// <summary>
/// get object from cache
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static object Get(string key)
{
return System.Web.HttpContext.Current.Cache[key];
}
}
}
In the class JSMinifyParser I have a modified version of the popular library called JSMin. The original JSMin converts one file to another, but I want it to read a file and return the minified string version of that file. So what I did was basically to change the StreamWriter to a TextWriter, and made the method return the string.
//Parser interface
namespace Portal.PCache.Parsers
{
public interface IFileParser
{
string Parse(string s);
}
}
//Parser class
using Portal.PCache.Parsers;
namespace Portal.Web.API.Parsers
{
internal class JSMinifyParser : IFileParser
{
#region IFileParser Members
public string Parse(string s)
{
JavaScriptMinifier mini = new JavaScriptMinifier();
string outs = mini.Minify(s);
return outs;
}
#endregion
}
}
//JSMin (modified)
using System;
using System.IO;
/* Originally written in 'C', this code has been converted to the C# language.
* The author's copyright message is reproduced below.
* All modifications from the original to C# are placed in the public domain.
*/
/* jsmin.c
2007-05-22
Copyright (c) 2002 Douglas Crockford (www.crockford.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
namespace Portal.Web.API
{
public class JavaScriptMinifier
{
const int EOF = -1;
StreamReader sr;
StringWriter sw;
int theA;
int theB;
int theLookahead = EOF;
//static void Main(string[] args)
//{
// if (args.Length != 2)
// {
// Console.WriteLine("invalid arguments, 2 required, 1 in, 1 out");
// return;
// }
// new JavaScriptMinifier().Minify(args[0], args[1]);
//}
public string Minify(string src) //removed the out file path
{
using (sr = new StreamReader(src))
{
using (sw = new StringWriter()) //used to be a StreamWriter
{
jsmin();
return sw.ToString(); // return the minified string
}
}
}
/* jsmin -- Copy the input to the output, deleting the characters which are
insignificant to JavaScript. Comments will be removed. Tabs will be
replaced with spaces. Carriage returns will be replaced with linefeeds.
Most spaces and linefeeds will be removed.
*/
void jsmin()
{
theA = '\n';
action(3);
while (theA != EOF)
{
switch (theA)
{
case ' ':
{
if (isAlphanum(theB))
{
action(1);
}
else
{
action(2);
}
break;
}
case '\n':
{
switch (theB)
{
case '{':
case '[':
case '(':
case '+':
case '-':
{
action(1);
break;
}
case ' ':
{
action(3);
break;
}
default:
{
if (isAlphanum(theB))
{
action(1);
}
else
{
action(2);
}
break;
}
}
break;
}
default:
{
switch (theB)
{
case ' ':
{
if (isAlphanum(theA))
{
action(1);
break;
}
action(3);
break;
}
case '\n':
{
switch (theA)
{
case '}':
case ']':
case ')':
case '+':
case '-':
case '"':
case '\'':
{
action(1);
break;
}
default:
{
if (isAlphanum(theA))
{
action(1);
}
else
{
action(3);
}
break;
}
}
break;
}
default:
{
action(1);
break;
}
}
break;
}
}
}
}
/* action -- do something! What you do is determined by the argument:
1 Output A. Copy B to A. Get the next B.
2 Copy B to A. Get the next B. (Delete A).
3 Get the next B. (Delete B).
action treats a string as a single character. Wow!
action recognizes a regular expression if it is preceded by ( or , or =.
*/
void action(int d)
{
if (d <= 1)
{
put(theA);
}
if (d <= 2)
{
theA = theB;
if (theA == '\'' || theA == '"')
{
for (; ; )
{
put(theA);
theA = get();
if (theA == theB)
{
break;
}
if (theA <= '\n')
{
throw new Exception(string.Format("Error: JSMIN unterminated string literal: {0}\n", theA));
}
if (theA == '\\')
{
put(theA);
theA = get();
}
}
}
}
if (d <= 3)
{
theB = next();
if (theB == '/' && (theA == '(' || theA == ',' || theA == '=' ||
theA == '[' || theA == '!' || theA == ':' ||
theA == '&' || theA == '|' || theA == '?' ||
theA == '{' || theA == '}' || theA == ';' ||
theA == '\n'))
{
put(theA);
put(theB);
for (; ; )
{
theA = get();
if (theA == '/')
{
break;
}
else if (theA == '\\')
{
put(theA);
theA = get();
}
else if (theA <= '\n')
{
throw new Exception(string.Format("Error: JSMIN unterminated Regular Expression literal : {0}.\n", theA));
}
put(theA);
}
theB = next();
}
}
}
/* next -- get the next character, excluding comments. peek() is used to see
if a '/' is followed by a '/' or '*'.
*/
int next()
{
int c = get();
if (c == '/')
{
switch (peek())
{
case '/':
{
for (; ; )
{
c = get();
if (c <= '\n')
{
return c;
}
}
}
case '*':
{
get();
for (; ; )
{
switch (get())
{
case '*':
{
if (peek() == '/')
{
get();
return ' ';
}
break;
}
case EOF:
{
throw new Exception("Error: JSMIN Unterminated comment.\n");
}
}
}
}
default:
{
return c;
}
}
}
return c;
}
/* peek -- get the next character without getting it.
*/
int peek()
{
theLookahead = get();
return theLookahead;
}
/* get -- return the next character from stdin. Watch out for lookahead. If
the character is a control character, translate it to a space or
linefeed.
*/
int get()
{
int c = theLookahead;
theLookahead = EOF;
if (c == EOF)
{
c = sr.Read();
}
if (c >= ' ' || c == '\n' || c == EOF)
{
return c;
}
if (c == '\r')
{
return '\n';
}
return ' ';
}
void put(int c)
{
sw.Write((char)c);
}
/* isAlphanum -- return true if the character is a letter, digit, underscore,
dollar sign, or non-ASCII character.
*/
bool isAlphanum(int c)
{
return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') || c == '_' || c == '$' || c == '\\' ||
c > 126);
}
}
}
So that is basically it! What you need to think about is to map *.js file types to be handled ASP.NET in the IIS settings, otherwise the HttpHandler won’t be triggered. Also there are some differences how you add the handler in web.config for IIS6 and IIS7. Here is a lot of usefull info on IIS7 http://learn.iis.net/page.aspx/26/installing-and-configuring-iis-70/
Labels:
asp.net,
c#,
dotnet,
httphandler,
javascript,
minify
Friday, February 27, 2009
Type safe web cache wrapper - ASP.NET
While building a URL mapping routine for my portal, I needed to optimize the loading of member specific URL keys.
For this I use the HttpContext.Current.Cache object that is easily used in the ASP.NET environment. Whats "ugly" with the Cache object is that it takes a object as a input parameter and of course return the cached object as the type of object.
So instead of do alot of type casting, I created a small wrapper class that use generics to handle the object types.
Take a look:
And to use the class:
For this I use the HttpContext.Current.Cache object that is easily used in the ASP.NET environment. Whats "ugly" with the Cache object is that it takes a object as a input parameter and of course return the cached object as the type of object.
So instead of do alot of type casting, I created a small wrapper class that use generics to handle the object types.
Take a look:
#region
using System;
using System.Web;
using System.Web.Caching;
#endregion
namespace Portal.PCache
{
/// <summary>
/// Type safe object cache
/// </summary>
public class ObjectCache
{
private const int TIMEOUT = 60;
/// <summary>
/// Adds the specified cache object. it will last for max 1hr from last access
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="cacheObject">The cache object.</param>
/// <param name="keyName">Name of the key.</param>
public static void Add<T>(T cacheObject, string keyName)
{
HttpContext.Current.Cache.Insert(keyName, cacheObject, null, Cache.NoAbsoluteExpiration,
TimeSpan.FromMinutes(TIMEOUT));
}
/// <summary>
/// Removes object with the specified key.
/// </summary>
/// <param name="key">The key.</param>
public static void Remove(string key)
{
HttpContext.Current.Cache.Remove(key);
}
/// <summary>
/// Check if object with the specified key exists.
/// </summary>
/// <param name="key">The key.</param>
/// <returns></returns>
public static bool Exist(string key)
{
return HttpContext.Current.Cache[key] != null;
}
/// <summary>
/// Gets the object with the specified key.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key">The key.</param>
/// <returns></returns>
public static T Get<T>(string key) where T : class
{
return HttpContext.Current.Cache[key] as T;
}
}
}
And to use the class:
MyObject obj = new MyObject();
string key = "myobject1";
bool exists = ObjectCache.Exist(key);
if(!exists)
{
ObjectCache.Add<MyObject>(obj, key);
}
MyObject another = ObjectCache.Get<MyObject>(key);
Wednesday, February 04, 2009
ASP.NET Variables in link - Solved
When I implemented an anti-cache function for my website i discovered that ASP.NET somehow parses <link> tags and makes it impossible to add parameters to the url.
I pass an version number to the url so that I can force new loads of the js and CSS, like this:
Somehow ASP.NET parse the <link> tag and create this ugly code:
The only solution for this was to include CSS like this;
JavaScript includes works perfect, it's just the css links that is parsed :/
I pass an version number to the url so that I can force new loads of the js and CSS, like this:
<script src="/script/portal.js?v=<%=ScriptVersion%>" type="text/javascript"></script>
<link href="/css/portal.css?v=<%=ScriptVersion%>" rel="stylesheet" type="text/css" media="screen" /> <!-- does not work -->
Somehow ASP.NET parse the <link> tag and create this ugly code:
<link href="/css/portal.css?v=<%=ScriptVersion%>" rel="stylesheet" type="text/css" media="screen" /> <!-- does not work -->
The only solution for this was to include CSS like this;
<style type="text/css" media="screen">
@import "/css/portal.css?v=<%=ScriptVersion%>";
</style>
JavaScript includes works perfect, it's just the css links that is parsed :/
Labels:
asp.net,
css,
javascript,
link
Saturday, January 31, 2009
Syntax Highlighting on Blogger - A guide that actually works.
I started to look for Syntax Highlighting code today to implement on this blog. Since I code in all kind of languages (JS,C#,AS3,SQL, etc.) I need something that can handle different kind of syntax and also looks good. I found SyntaxHighlighter from Alex Gorbatchev, which looks awesome.
How do can you implement this great code in your Blogger pages? I found a few guides that did not really work that well, so I'll now explain how to do it in five easy steps.
1. Go to your Blogger blog, log in and go to Layout -> Edit HTML.
2. Open another Browser Tab and copy the CSS from this url: http://syntaxhighlighter.googlecode.com/svn/trunk/Styles/SyntaxHighlighter.css
3. In the edit window on Blogger go to the last line of the inline CSS (located in the HEAD tag) and paste the copied CSS code.
4. Copy this JS code last in the HEAD tag:
(When posting HTML code, remember to replace
5. At the end of the edit window just before the /BODY tag, paste this code:
6. Save! And we're done!
How do can you implement this great code in your Blogger pages? I found a few guides that did not really work that well, so I'll now explain how to do it in five easy steps.
1. Go to your Blogger blog, log in and go to Layout -> Edit HTML.
2. Open another Browser Tab and copy the CSS from this url: http://syntaxhighlighter.googlecode.com/svn/trunk/Styles/SyntaxHighlighter.css
3. In the edit window on Blogger go to the last line of the inline CSS (located in the HEAD tag) and paste the copied CSS code.
4. Copy this JS code last in the HEAD tag:
(When posting HTML code, remember to replace
<
with <
, and replace >
with >
)
<script language='javascript' src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shCore.js' />
<script language='javascript' src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCpp.js'/>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushJScript.js' type='text/javascript'/>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCSharp.js' type='text/javascript'/>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCss.js' type='text/javascript'/>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushJava.js' type='text/javascript'/>
5. At the end of the edit window just before the /BODY tag, paste this code:
<script language='javascript'>
dp.SyntaxHighlighter.BloggerMode();
dp.SyntaxHighlighter.HighlightAll('code');
</script>
6. Save! And we're done!
Labels:
blogger,
code,
javascript,
syntax highliting
Testing Syntax Highlighting
I'm finally planning to post some code examples from my portal, so here is a start with some syntax highliting :)
JS
JS
var test = new Test();
int i = 0; // commenting test
Tuesday, January 20, 2009
SvD vs DN vs Nyhetskanalen
Sorry this one is in swedish...
Under tiden jag jobbar med att lansera vår nya sajt, vars innehåll till stor del innehåller feeds från olika nyhetssajter så ser man det varierande kvalitén i dessa feeds från några av landets största sajter.
Jag har en sida som innehåller widgets med feeds från bland annat Svenska Dagbladet, Dagens Nyheter och TV4's Nyhetskanalen, och som ni ser på bilden vinner klart SvD. Varför? Jo, till att börja med så har DN i den sin senaste artikel en font tagg! Vilken seriös sajt använder 2009 font? Enligt obsoleteskills.com blev den obsolete för 8 år sedan.
Vi går vidare till Nyhetskanalen.se, där vi ser den senaste, absolut hetaste nyheten: Bågjakt blir tillåten i Sverige. Vad hände med beskrivningen? Tomt.
Samma sak tre nyheter ner: Kvinna återfunnen efter 12 år. Här var det heller inte värt att ha någon beskrivning. Dåligt TV4.
SvD åt andra sidan har hyffsade nyheter i sin feed. Den enda kritiken är att de är något korta och helt utan bilder.
Subscribe to:
Posts (Atom)