Entries Tagged as ''

How To Use Amazon EC2 for offloading CPU intensive tasks

I’ve got some processes that I use to test historical data. I like to optimize them till they are running very fast (as can be expected)…. Normally this will suffice, but on this test I’ve got several variables to test — essentially I am looking at about 200 days of processing to get my results; but I don’t want to wait 200 days.

So.. the solution [a solution] in cases like this is to use something like EC2. There are other solutions, use a bunch of old laptops, or a ec2 competitor, but since all my code is in C# w/ MSSQL server, ec2 is my first choice.

Step 1 (since I am doing this on a small windows instance) the code must be converted to a service. This is so you can launch an instance and have your code start right up. A few things to watch out for: You can’t just throw a timer control on the service and expect it to work, you actually have to write one.

private Timer oTimer;
private int _interval = 1000;
private string currentTask = ""; // Lazy way of making sure I don't step on my own toes...

protected override void OnStart(string[] args)
{
TimerCallback timerDelegate = new TimerCallback(OnTimedEvent);

// create timer and attach our method delegate to it
oTimer = new Timer(timerDelegate, null, 1000, _interval);
}

private void OnTimedEvent(Object state)
{

if (currentTask == "")
{
currentTask = "Working";

//Do your processes

currentTask = "";

}

Don’t forget dependencies (in my case I had to add a dependency for MSSQL$SQLEXPRESS (so it won’t start will the database server is up and running) — I also added a test in the code to make sure I could connect just in case, since in one of my realworld tests I had some racing issues.

Ok… service is all fine and dandy.  You have tested your EC2 instance (windows small w/ sql express is cheapest for this situation at $0.12 per hour). Make sure that you have it set to automatic, and that there are no weird issues on reboot to keep your service from starting how you want.

In this case I have them all communicating with a central Non EC2 database server to track which server is doing what tests and to store the results.  This allows me to start 5, 20, 100, etc instances at once and to just let them run.  [Actually amazon limits you to 20 instances, unless you get special approval -- which doesn't seem hard to get, I just haven't done it yet, since I don't know if I will actually need it]

Now that you have your serer all updated (windows update, and starting it starts up your service and the logging and tracking works, etc.. it is time to make this into an AMI (instance that you can use to start up as many servers as you want)

I left mine at the default size of 30 gigs, but if I were to do it over again, I would research how to lower the size to say 8 or 10 gigs (of the original hard disk) since I don’t need that much space, and I bet amazon is charging me for all that space even though I am not using it.  Amazon ec2 has changed quite a bit over the past 12 months.  I personally use a combination of elasticfox and the AWS web console to manage my instances.  elastic fox doesn’t have a “start” option for a shut down instance, but AWS does, and there are a few other quirks…

Anyways, I would recommend shutting down the server first, then using AWS to make an EBS AMI Instance (right click on your instance).  This is less hassle then doing it via S3 if you really havent bothered to setup S3 yet (like me).

This will take a while (30-60 minutes).  I didn’t keep track, but I know it took more then 20 minutes.  Once that is done, it is just a matter of starting 20 instances of your private AMI.

Then sit back and wait while the results come in.

In N Out opened today in American Fork

Opening day the line is long but moving very fast 2 doubles 1 animal double and some fries

In n Out (its not as far as is looks)

Line is kinda long… but moves way fast.. :)

Almost can pay…. lame… they don’t take AMEX.  I am pretty sure I remember them taking amex in california..

~ $12 later I have had 2 hamburgers, 2 boxes of fries and introduced my spouse to In-N-Out with her own burger.

A good lunch :)

Faster Tablet PC w/ More Ram :)

Memory America made my laptop faster :)

Over a year ago I decided I wanted a 10″ iPod touch.  Well since those didn’t exist I went for the next best thing.  A second hand tablet pc that used to be used in a morgue.  (The weird formaldehyde smell went away after a few weeks)

Enter the HP TC1100.. a great little machine.  Perfect for… well a tablet pc..

Here is the back… before I opened up the memory slot with my super amazingly complex “tiny phillips” screwdriver.

It is open…   the anticipation.. oh the sweet anticipation.

It is in… thats it?  I used the 1 Gig “off brand” memory for my tc1100 here.

Put the back back on and turn it on.

Much snappier response time for just about everything.

Need To Get Your Adwords Campaigns Over to Yahoo Search or MSN adCenter Quickly?

This is an internal tool we’ve been using for a while, with the mass adwords account banning going on I figured people would have a need of this. It runs entirely on your local computer ( windows, linux, or mac), nothing is sent to a server somewhere. It does cool things like KWI from google to yahoo or msn and replacing text during the conversion.

Download link here
http://www.johnhasson.com/index.php/jah-adwords-to-yahoo-or-msn/

Edit/Update
12/11/2009
Yahoo match types are done “correctly” (as correct as you can going from google to yahoo). Broad = Advance, Phrase & Exact = Standard.

Transparent And Glass Console In C#

I’ve got some processes that I run quite a bit that run in C# console applications. Here is how I make them trasnparent (and give them a slight vista glass effect)
(If you use just the glass code writing behind will usually b too blurry to read, but if you combine transparency w/ glass the writing it still slightly glowy/blury but easily readable)

GlassTransparent


using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace GlassExample
{
    class Program
    {
        [DllImport("user32.dll")]
        static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey,
           byte bAlpha, uint dwFlags);

        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern System.UInt32 GetWindowLong(IntPtr hWnd, int nIndex);

        public const int GWL_EXSTYLE = -20;
        public const int WS_EX_LAYERED = 0x80000;
        public const int LWA_ALPHA = 0x2;
        public const int LWA_COLORKEY = 0x1;

        [StructLayout(LayoutKind.Sequential)]
        public struct DWM_BLURBEHIND
        {
            public DwmBlurBehindDwFlags dwFlags;
            public bool fEnable;
            public IntPtr hRgnBlur;
            public bool fTransitionOnMaximized;
        }

        [Flags()]
        public enum DwmBlurBehindDwFlags : uint
        {
            DWM_BB_ENABLE = 0x1,
            DWM_BB_BLURREGION = 0x2,
            DWM_BB_TRANSITIONONMAXIMIZED = 0x4
        }

        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern void DwmEnableBlurBehindWindow(IntPtr hwnd, ref DWM_BLURBEHIND blurBehind);

        static void EnableBlurBehind()
        {
            IntPtr Handle = Process.GetCurrentProcess().MainWindowHandle;
            DWM_BLURBEHIND blur = new DWM_BLURBEHIND();
            blur.dwFlags = DwmBlurBehindDwFlags.DWM_BB_ENABLE;// +DwmBlurBehindDwFlags.DWM_BB_TRANSITIONONMAXIMIZED;
            blur.fEnable = true;
            //blur.hRgnBlur = 0;
            blur.fTransitionOnMaximized = true;// DwmBlurBehindDwFlags.DWM_BB_TRANSITIONONMAXIMIZED;

            DwmEnableBlurBehindWindow(Handle, ref blur);
        }

        static void MakeTransparent(byte pct)
        {
            IntPtr Handle = Process.GetCurrentProcess().MainWindowHandle;
            int newDwLong = ((int)GetWindowLong(Handle, GWL_EXSTYLE)) ^ WS_EX_LAYERED;
            SetWindowLong(Handle, GWL_EXSTYLE, newDwLong);
            SetLayeredWindowAttributes(Handle, 0, pct, LWA_ALPHA);
        }

        static void Main(string[] args)
        {
            EnableBlurBehind();
            MakeTransparent(170);  //0 to 255

            Console.WriteLine("Test");

            Console.ReadKey();

        }

    }
}

(There is zero error checking here… this will probably crash on XP, caveat emptor, etc)