Sunday, 13 November 2016

.NET Concurrent Collections

Oh ConcurrentQueue, where have you been all my life! - Why have I only just found out that you exist?

I know, I know… It seems I’m late to the party.

But it turns out that the new System.Collections.Concurrent namespace was only introduced in .NET Version 4.0

It’s no secret that when it comes to data processing, adding more threads mean you can process data faster. But adding multiple threads means data races. It means you need to make sure you have a mutex/lock setup around the shared data structure. This ensures that all access to this central data is tightly controlled. Essentially, it means only one thread at any one time should be able to access the data.

Now, I have previously rolled my own thread-safe locking queue which is always a scary concept (due to data-race conditions). I have used it in multiple projects and have many variations.

I have written a Blocking Queue which doesn’t spin on a thread. Essentially all threads will wait on a queue instead of having to Thread.Sleep(). I have also written a thread-safe database writer, specific to the database schema needs, where worker threads will pick up work of a certain type (WorkType), then write it to the database in the order it appears in the queue (important). If another thread wakes up and starts taking work from the master work queue it ignores that WorkType as it is being dealt with by the previous worker thread. This means there needs to be intercommunication between threads. It’s tricky code to get right.

Why do I mention all this?

Because these new thread-safe collection classes could simplify a lot of my current code!

I work on many producer/consumer problems where you can have many multiple producers of work and need many consumers trying to keep up! I have a set of utility classes that I bring along with me to every new project.

But the next time I am working on a project, perhaps it’s time to upgrade to .NET 4.0, then refactor my utility classes to use the classes from the new System.Collections.Concurrent namespace.


Contact Me:  ocean.airdrop@gmail.com

Sunday, 9 October 2016

My Winform "HTML Toast Notification" Library

The other day/week/month (gee, where does the time go!), I wanted to include toast style notifications inside a Winform's application I was working on.

Basically, I wanted some kind of on-screen-display (OSD) that notifies the user when an event happens in the app. I also wanted something non-obtrusive so the user didn't have to interact with it (for example, clicking a button to dismiss it).

The only thing was... I couldn't find anything out there that suited my needs.

So, I wrote my own!

After playing around with some web-based toast notifications, I thought it would be neat to be able to display any HTML in a transparent window. After a bit of research, an article on CodeProject helped me to display a transparent window in C#.

From there, I added my own code to display HTML in the window using the excellent HTML Renderer library found here!

This is what I ended up with:

  • You can display any HTML at any location on the screen.
  • You can display any image inside your project.
  • You can display base64 png images inside your HTML.
  • You can specify a timeout for the toast notification.

You can find the code for the sample project on my github page.

Check it out here: https://github.com/OceanAirdrop/WinformsHTMLToastNotification


Contact Me:  ocean.airdrop@gmail.com

Sunday, 5 June 2016

Measuring CPU performance from the CommandLine using PerfMon

In a previous post, I talked about viewing "coarse grained CPU performance" of an application using Process Explorer. It's a great way of checking "at a glance" how a process is performing CPU wise.

However, if you want to get down with the nitty-gritty details of your server's performance, you need to bring out the big guns and use the built-in perfmon utility.

I don't know why, but I always find perfmon fiddly to use. Maybe it's just me! Anyway, in this post I just wanted to describe the batch-file I use to test a server's health.

As you know, you can drive perfmon from the GUI by running the perfmon.exe command from the start menu. When you do, you will be presented with this screen:


To get started, first use the logman create command to setup the perflog. In the instance below I have named the log "ocean_airdrop_log" and assigned 2 counters to it.
logman create counter ocean_airdrop_log -c "\Processor(_Total)\% Processor Time" "\Memory\Pool Paged Bytes"  -f csv -o D:\PerfMonLogs\oceanairdrop 
The two counters I have selected are:
"\Processor(_Total)\% Processor Time"
"\Memory\Pool Paged Bytes"

I am also outputting the results to a .csv file. A .csv file is better for analyzing the output. This .csv file gets outputted to the directory "D:\PerfMonLogs" and starts each file with the name oceanairdrop.

Once you run this command, you see the log created in the perfmon window under "User Defined" collector sets.


The next command sets the sample interval for performance log. In this case I am logging every 60 seconds.
logman update ocean_airdrop_log -si 60
Next, we start the log by running the start command:
logman start ocean_airdrop_log
If you have the perfmon window open, you will see the status of the log change to running.


At this point you will see the log file created in the output directory we have specified.


At this point you can stop here and continue to run this perf log for as long as you need.

In my batch file I make a call to the timeout command. This allows you to pause a batch file for a specified period of time (it's very useful).
timeout /t 300
For example, this command waits for 5 mins (60 secs *5 = 300)


Finally, to stop the perflog from running issue this command:
logman stop ocean_airdrop_log

Full Batch Perf-Script


With that, here's the full batch script:
-- Delete the perflog if it already exists
logman delete ocean_airdrop_log

-- Create the perflog selecting the counters want to include
logman create counter ocean_airdrop_log -c "\Processor(_Total)\% Processor Time" "\Memory\Pool Paged Bytes"  -f csv -o D:\PerfMonLogs\oceanairdrop 

-- Set the sample interval time. (this is every 60 seconds)
logman update ocean_airdrop_log -si 60

-- Start the perflog
logman start ocean_airdrop_log

-- Wait for 5 mins
timeout /t 300

-- Stop the perflog
logman stop ocean_airdrop_log

-- Delete the perflog
logman delete ocean_airdrop_log

Summary


There you have it. Having the above perf-script handy that you can run ad-hoc is very useful when diagnosing server performance issues.
Contact Me:  ocean.airdrop@gmail.com

Sunday, 29 May 2016

Yo! - I'm a Lambda.

I've just been reading up on the new JavaScript/ECMAScript 6 features and in particular the new fat-arrow syntax for lambdas.

It's funny, but whenever I see this syntax () => { } in C# code, my mind always shouts to me "Yo!! - I'm a Lambda". - Okay, I understand that might sound weird... It is weird and I don't know how to stop it!

Let me explain!

What I mean is, as programmers, when we read code our brain automatically identifies the cryptic string of characters and converts them to a mental model of what the code will do on the fly. For example, when scanning code, if you come across a for or while keyword your brain automatically tells you: "oh... there's a loop coming". If you see an if you automatically know there is a condition and a branch coming.

This happens instantaneously without fore-thought. It's like words on a poster. Your mind reads the words even if you're not interested in what the poster is advertising. In fact, put anything in front of yourself with words and your mind just reads it without asking you! It just happens!

In C#, whenever I see the the following syntax () -> {}, my mind always shouts to me "Yo!! - I'm a Lambda" because when I was learning the arrow syntax I engraved that sentence into my sub-conscious. And now... I can't shake it!!

So there I was, reading the new JavaScript syntax for lambda's and it happened again! This is when I thought it would be interesting to write a blog post on lambda's and compare the different lambda syntax's from various languages. You know, to see if my mind would play this phrase across languages.

Quick n Dirty Overview of Lambda's


Way back in the day, when I first saw a lambda I remember thinking, "what the smeg is that?" - It was weird looking code like this: x => { return x + 1; }. I didn't know what the X was doing on the left hand side. It just looked funny... Turns out, it is a lambda!

This is how you read a lambda expression:
  • Everything to the LEFT of the arrow is the function parameters.
  • Everything to the RIGHT of the arrow is the function body.
Remember good old-fashioned functions like this:
int AddOne(int x)
{
   return x + 1;
}
To write that as a lambda it becomes:
(int x) => { return x + 1; }
Notice above, that you can see the outline of the function still. You can still see the function takes an int as a parameter named x. Okay, its on one-line, and the name has gone but the outline is still there. The additional punctuation/syntax that's been added is the => which is where my "Yo!! - I'm a Lambda" gets triggered.

Turns out though that we can simplify this even more. If the compiler can determine the type that's passed in, you don’t need to put it there. So it becomes:
(x) => { return x + 1; }
Now, as this function only takes 1 parameter it turns out you can also leave out the parenthesis which gives you:
x => { return x + 1; }
Finally, you can simplify it even more and leave out the return statement (and remove the parenthesis). Then it becomes:
x => x + 1
This is the transformation:
// From this traditional Function
int AddOne(int x)
{
   return x + 1;
}

// ...to this Lambda expression!
x => x + 1

That's all good and easy to understand but when I used to see the code below it used to throw me.
var sum = () => 1 + 2;
It was the empty parenthesis that did it. But that's just a lambda expression that takes zero parameters and returns the sum of 1 + 2. But it looks odd.

Want to see what lambdas look like in other languages? Turns out, so did I. Read on!

C# Lambda Syntax

Okay, we already know what the C# code looks like. Below is the AddOne function as a lambda expression.

class Program
{
    public delegate int AddOne(int value);

    static void Main(string[] args)
    {
        AddOne lambdaFunc = (int someVar) => { return someVar + 1; };

        lambdaFunc(2);
    }
}

You can test this out with the online C# compiler here: http://csharppad.com/

JavaScript Lambda Syntax

As I mentioned earlier, it was whilst reading up on JavaScript Lambda functions that prompted me to write this blog post. The JavaScript code looks something like this:

var lambdaFunc = (someVar) => someVar + 1;
lambdaFunc(2)

To test this out, simply press F12 in your browser, navigate to the console tab and enter the above code.

C++ Lambda Syntax

In C++ they don't use the fat-arrow. They use the array index operators [] as the lambda operator. This allows you to capture the surrounding variables by reference or by value and is my "Yo!! - I'm a Lambda" trigger. The code looks like this:

int main()
{
    auto lambdaFunc = [&] (int someVar) { return someVar + 1; };
    lambdaFunc(2); // now call the function
}

You can try this out using the online C++ compiler here: http://cpp.sh

Rust Lambda Syntax

This one's a bit weird. The syntax looks funny as they use the pipe operator | which is something I am not familiar with.

fn main() {
    let lambda_func = |some_var: i32| return some_var + 1;
    lambda_func(2);
}

You can try this out using the online rust compiler here: https://play.rust-lang.org. Incidentally, the rust compiler forced me to change all my variables to snake_case which I thought was interesting. I like the way you can enforce variable names to keep the code consistent.

Python Lambda Syntax


I like the way Python is all in your face by putting the keyword lambda front and centre. Here's the code:
lambdaFunc = lambda someVar: someVar + 1
lambdaFunc(2)

Again, you can try this out here: https://repl.it/languages/python3

Ruby Lambda Syntax

Ruby is quite similar to JavaScript, with a couple of subtle differences. Instead of the fat-arrow they use a thin-arrow! Also the thin-arrow comes before the function parameter list, whereas other languages put the arrow operator in the middle. Here's the code:

lambdaFunc = -> (someVar) { someVar + 1 }
lambdaFunc.call(2)

Here is the online compiler to check it out for yourself: https://repl.it/languages/ruby

Swift Lambda Syntax


Unfortunately, I have not tested this code but from the documentation, the Swift definition of a lambda is as follows: Lambdas are typically enclosed in curly braces { } and are defined by a function type () -> (), where -> separates the arguments and the return type, followed by the in keyword which separates the closure header from its body.
{ (params) -> returnType in
  statements
}

Notice, here you define the return type for the function where other languages put the function body. Swift also has the in keyword which proceeds the function statements. Our example would look like this:

{ (someVar) -> Int in
  someVar + 1;
}

Go Lambda Syntax

Strangely it looks like Go doesn't support the lambda syntax and the language designers have no intention to add the functionality to the language.

See here: https://groups.google.com/forum/#!topic/golang-nuts/Kfm4t3TShTY

Wrapping up

If you throw a dart in the direction of a modern programming language today, chances are it will support lambdas! More and more languages are borrowing concepts from functional programming. Lambdas are a great way to pass around functions and simplify code.


Contact Me:  ocean.airdrop@gmail.com

Saturday, 21 May 2016

Windows Command Line Goodies

With the news that Microsoft is bringing Bash to Windows (what a crazy world we live in!), it looks like I'm going to add "bash" to my "one more thing to learn" list.

In the meantime, I thought it would be good to list the common daily windows cmd.exe tools that I use and find useful. You can't beat the command line for performing fast, repeatable, scriptable actions that you might need to schedule or run ad-hoc.

Let's start the list:

Get the hostname of the computer you are on

If you are like me and can be logged into many remote desktop sessions, then it can be useful to find out the hostname of the computer you are on. To do that, the command is: hostname. This means you will never get caught deleting files from the wrong server again! :)

Another simple command is the whoami command which will tell you the user you are logged on as for this session

Change the title of the command prompt window

Okay, you might laugh, but this is more useful than you think. If you have many cmd prompts open doing different tasks, changing the title of the command window helps you quickly identify the window you need.

List all processes running on the local machine

Ensuring that processes are running is essential bread and butter stuff. The tasklist command will give you what you need.

You can also list all processes running and their loaded DLL’s with the -m switch: tasklist -m

List all processes running on a remote machine

The tasklist command above can also list processes that are running on remote machines. Again, very useful. The command is: tasklist /s 10.10.10.10 /u domain\username. You will, of course, be prompted for the user's password

Kill a process running on the local machine

I use this all the time. There is no faster way of closing chrome.exe and all its tabs than from the command line. The command is: taskkill /F /IM pcocessname.exe.

Kill a process running on a remote machine

To kill a process on a remote machine you just need to supply the /s and /u flags to the taskkill command. For example: taskkill /s 10.10.10.10 /u domain\username /IM "appname.exe"

Display all services running on a machine

If you have services that you know should be running, then running net start shows you all services that have been started on your machine.

Pinging and IP addresses

This is the simplest of all diagnostic commands: ping 10.20.30.40. If you want to keep a constant ping going add the -t flag like so: ping 10.109.200.3 -t. I also like the utility fping by Kwakkelflap. You can download it from here.

It allows you to add a date and time to the ping, as well as log the pings to a file. You can even specify the amount of time to wait in-between each ping.

For example, the following command: fping 10.20.30.40 -D -T -c -t 10000 -L hello.txt will ping 10.20.30.40 logging the date and time to a file named hello.txt. It also waits 10 seconds in-between each ping. I like this utility, but one problem I have found is, it doesn't flush the file. This means you need to stop the execution before it writes everything out.

If you want to get your own IP address run ipconfig /all.

Display all IP addresses connected to a machine

If you want to display all IP addresses that are connected to a machine and what process they are connected to then run netstat -a -n. If you want to display all IP addresses connected on a specific port then pipe it through the find command. E.G: netstat -na | find "1234"

PSTools

This should probably be at the top of the list. The PSTools suite is great and my stand out favorite is the psexec tool. It allows you to run any dos command you issue on a remote computer.

For example, if you wanted to run netstat –n to display all IP addresses connected to a remote machine then you could run the following command: psexec.exe \\10.10.10.10 -u domain\username -p password netstat –n

Elevate

Have you ever tried modifying the hosts file only to be greeted with this dialog?

If so, then Elevate is the command line tool you need. This is just one example, but there can be many cases where you need to run something with elevated rights.

Connect to SQL Server database

If you have an SQL server instance you can run queries straight from the command line by using the osql command. For example: OSQL -S 10.10.10.10\SQLExpress -U username -P password -d dbname -Q "select * from [dbname]..[tablename] where columna = 'blah-de-blah'"

You can even send the result to a text file OSQL -S 10.10.10.10\SQLExpress -U username -P password -d dbname -Q "select * from [dbname]..[tablename] where columna = 'blah-de-blah'" >c:\textfile.txt

IP routing and adding a fixed route to your routing table

To view your routing table simply run route print. This will show you all the routes that are setup on your machine. You can add a fixed route to your routing table by running the "route add" command. This can be useful if you want to route traffic in a certain IP address range to a specific destination server. This has been useful to me in OpenVPN environments when I needed to force specific IP packets to a gateway.

To do this, first delete the route before adding it (just to make sure it doesn't already exist). For example: route delete 172.16.0.0. Then add the permanent route by running "route add" with the -p flag set. The -p flag says "make this permanent". For example route -p add 172.16.0.0 MASK 255.255.0.0 10.20.30.40 will create a route whereby and traffic that is destined for the IP range 172.16.x.x will get routed/pushed to the server 10.20.30.40.

Test if a remote server is listening on a specific port number

If you have a server listening on a remote port and you want to check it is accepting incoming connections, then you can use telnet to connect to the server and port. If it fails you will get the following error message back: "Could not open connection to the host, on port 25: Connect failed". This is very useful.

For example, the following command will try and connect to a server on port 80: telnet 10.20.30.40 80

Remote shutdown a SERVER!!!!

Naturally, use this one with caution! It's always a bit scary waiting for the server to come back up. When I do a remote-boot of a server, I keep a constant ping going so I can see when it comes back up

Don't forget, its obligatory when remote booting any server to mutter the Samuel L Jackson line from Jurassic Park: "hold onto your butts!"

Here's the command: shutdown -t 0 -r -f -m \\10.10.10.10

Find out users Logged onto a machine and logging them off

Under certain situations, you might find that you can't rdp onto a server because there are other users logged on. If this is the case you can issue the following command: quser /server:10.10.10.10. This will return a list of username's and and user id's. Following on from this, if you want to log a user off then run the logoff command, like so: logoff 1 /server:10.10.10.10. The 1 here is the user id returned from the quser command.

Time how long it takes a web page to load

cURL is a command-line tool that can communicate over a network using TCP, HTTP, etc. It includes metrics which means you can use it to time how long it takes for a web page to load.

Use it like so: curl -3 -k "https://oceanairdrop.blogspot.com"

Summing up

These commands are all bread and butter stuff but are useful to know to get diagnostic information about your environment. If you are responsible for a number of servers some of these commands can be a life saver. If you are unable to log onto a remote machine (using mstsc.exe) for whatever reason, don't forget about the command line. Being able to run these commands remotely (psexec.exe) is a godsend.

That's about it.... Remember, when issuing any of these commands, don't forget to "hold onto your butts!"


Contact Me:  ocean.airdrop@gmail.com

Sunday, 15 May 2016

Observations on Software Package Managers

It’s interesting to watch the landscape of software development evolve over time.

One of those evolution's is the advent of package managers. As an application developer, having access to a global library of code packages which can be pulled down in an automated way, is such a valuable addition to the development experience.

They are everywhere! For example:

Package managers are here and they definitely increase productivity by making common code easier to include in your project.

When I first uploaded my projects to Github, I mistakenly included all my third party Nuget packages and forgot that one of the other jobs of a package manager is to install/restore missing packages when required.

Beautiful… That, is of course, until it goes wrong…

For example, if you don’t pin a specific version of a library, there is nothing to stop the owner of that third party library from making breaking changes to your code. This means you will not be able to successfully build your project in the future.

For example, in JavaScript with NPM, if you just list all your dependencies (in the package.json file) without specific versions, this will pull down the latest version(s) automatically.

So pinning to a specific version is good practice.

But, on the flip-side, recently I was trying to download a C# project from GitHub that required a Nuget restore. The problem was, the version that the C# project relied on didn't exist in Nuget! Nuget failed to retrieve the version that was pinned. This meant I couldn't build the code.

However, even with these subtle observations/gotcha’s aside, package managers are a great addition to the software developer’s toolkit.

If you are a C# developer and use Nuget you might want to check out the NuGet.CommandLine package by running "Install-Package NuGet.CommandLine". Instructions are here. This can be handy for automated builds and will enable you to run commands like "nuget delete", "nuget restore" and "nuget update"

Now, I would love to see a C++ package manager but it doesn’t look like there is one yet. Maybe thats because it still uses #includes and doesn't have the concept of a package! - Maybe when modules make it into the language there will be some hope there. I don't know if it’s out of scope for the guys over as https://isocpp.org/ but it would be good if there was an official one!

One can hope…

By the way, this website https://libraries.io/ monitors "open source libraries across 32 different package managers", which could be a great way to search for new libraries to include in software projects.


Contact Me:  ocean.airdrop@gmail.com

Wednesday, 9 March 2016

Quick and Easy Analysis of Website AJAX calls using Chrome Net Events

There are times when you need to find out what background "ajax/web endpoint calls" a webpage is making under the covers.

As it happens, the other day was one of those times! - I needed to find out which Web Endpoints an internal website was calling over an HTTPS connection.

Now, we all know we could use Fiddler or the fantastic Wireshark for analysing network traffic but I didn't have either of these installed on the box I was using.

I was just about to install Fiddler when I remembered chrome's internal net events page.

Simply open chrome.exe and navigate to: chrome://net-internals/#events. It's like having Fiddler built into the browser. Now, go pack to your web page and perform your action (in my case it was clicking a button).

The great thing about this is that it works over SSL/TLS connections which means you don't need to install a SSL certificate to decrypt your traffic.

Okay, it's not as powerful as Fiddler or Wireshark, but, if all you need, is to know the names of the endpoints, this will do.

There you have it. Quick, Fast & Easy: chrome://net-internals/#events


Contact Me:  ocean.airdrop@gmail.com

Saturday, 5 March 2016

Modern JavaScript Development

Welcome to Client-Side tales from a Server-Side Developer.

If your background is server-side development like me, then visiting the land of JavaScript can be quite daunting. Don't get me wrong, I'm not talking about the JavaScript language itself. Whilst the language has recently been updated to ES6, I'm talking about everything else that comes along with it.

Honestly, as an outsider looking in it's like the Cambrian Explosion out there. A soup of different language extensions, frameworks, plug-ins, package managers, task runners, et cetera, et cetera.

Where do you start?

So Many Questions

As I dip my toe into today's JavaScript ecosystem, I want to make sure I'm as forward-leaning as possible. I don't want to be writing JavaScript from 1995! But after spending time reading the interwebs you come away with lots of questions. Questions such as:

Seriously, who's coming up with these names?

  • Should I be using SASS or SCSS? What about LESS?
  • Is Jquery still a thing? I've heard that people depend less and less on it?
  • Do I choose Babel or Traceur?
  • What about throwing TypeScript into the mix? And what about FlowType? Where does that fit in?
  • I've heard that React likes FlowType.. But why didn't Facebook just use TypeScript instead of reinventing the wheel?

Then there is the code editors. If you are on Windows surely you'll be installing the free community edition of Visual Studio 2015. But what if you want to master a code-editor and be able to transfer those skills to Linux and the Mac?

Do you choose:

Then there's the frameworks and libraries...

The list truly goes on and on. Fortunately, at this stage I'm not as interested in JS Frameworks and so I haven't explored any of these at this point.

Phew... I'm exhausted!

But choice is Good, right?

I guess choice is good, but if your not in the ecosystem day-to-day then there's a lot to catch up on.

Oh, and by the way... Sorry to break this news to you but if you are reading this post 5 minutes after it was published, then I'm sorry but the above list is probably way out-of-date by now. In fact as you're reading these words 8 more frameworks have just popped into existence and already have a strong following. 😊

My Installation Setup

So, back to my setup. Currently, as it stands, this is the mis-informed JavaScript setup that I have started with:

And these are the things I will probably be taking a look at later down the line:

Let's go over these installs..

Git for Windows

What is it? Git is the popular version control system to host code. A lot of open source JavaScript libraries are hosted on Git.

Why am I installing it? The command line git.exe is needed for you to pull down code and build packages from. Also the Node Package Manager (NPM) uses it.

Node.js and the Node Package Manager (NPM)

What is it? Node allows you to run JavaScript outside of the browser. Simple as that.

Why am I installing it? Node.js allows you to run JavaScript outside the browser which means task-runners like Gulp can run on your system but it also includes the Node Package Manager (NPM). This is the JavaScript equivalent of .NET’s Nuget. It’s easy to see why Package Managers are so popular. If your solution depends on X, Y or Z you can reference them via NPM or Nuget.

Bower

What is it? It's another package manager! Like NPM above

Why am I installing it? But hang on! I've just installed a package manager above (NPM). Why do I need another one? I've found that many JavaScript libraries out there support both NPM and Bower. As an example, take a look at the Moment.js JavaScript library. On its webpage it supports Bower, NPM and NuGet amongst others.

Why is there two? Whats the difference? Is one better than the other? The internet, as always as opinions

https://www.quora.com/Why-use-Bower-when-there-is-npm
http://stackoverflow.com/questions/18641899/what-is-the-difference-between-bower-and-npm

Babel

What is it? Compiles ES6 code down to ES5

Why am I installing it? It's great that ES6 has been released but if you look at this compatibility chart here you will see that a lot of mobile browsers still don't support a lot of ES6 features. That's where Babel comes in. It allows you to write ES6 code today and back-transpile it down to ES5 code so that it still runs on all browsers. It's awesome!

Gulp

What is it? A Task Runner. Something that automates our workflow.

Why am I installing it? Remember that writing the actual code is only half the story. If your using a CSS pre-processor or a language abstraction like CoffeeScript or TypeScript then you need a task runner to compile the SASS to standard CSS and the CoffeeScript to standard JavaScript.

In Visual Studio this is provided inside the box. You can setup pre-build and post-build steps, so that when you press F5 to compile your code it will perform these extra steps. (or use an MSBuild script on the command line)

The equivalent in the JavaScript world is Gulp and it allows you to automate your workflow. For example, as part of deploying your code you might want to:

  1. Run Babel to Transpile from ES6 to ES5
  2. Run HandleBars to bundle up all your HTML templates
  3. Setup your distribution folder (Create folder structure, etc)
  4. Convert your SASS or LESS files to actual CSS that the browser understands
  5. Lint your code for errors
  6. Merge all your JS/CSS into 1 file
  7. Minify the code
  8. Yadda, Yadda...

Gulp allows you to write JavaScript to chain all these events together. Remember Node.js allows you to write JavaScript code outside the browser, which makes this possible.

Okay, if all you are doing is writing simple JavaScript then perhaps you don't need Gulp at this time. As it happens, I haven't installed it as I am just using a simple Batch File for my build steps.

VSCode

What is it? Its a lightweight code editor

Why am I installing it? I spend the majority of my time in the full fat version of Visual Studio and for personal development I use the Community Edition of Visual Studio 2015 (Its free!). But VS Code is fast, supports a whole host of languages and is cross-platform. As of today, it's still in beta but I intend to use it for all my client-side JavaScript development and see how I get on.

Command-Line Install Script

Here is the command-line install script to install the above:

# Step 01: Install Chooloatey (a windows version of apt-get) https://chocolatey.org/ 

c:\> @powershell -NoProfile -ExecutionPolicy Bypass -Command "iex (( new-object net.webclient).DownloadString( 'https://chocolatey.org/install.ps1' ))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin

# Step 02: Install Git (https://chocolatey.org/packages/git.install)

c:\> choco install git.install

# Step 03: Next Install Node.js (https://chocolatey.org/packages/nodejs.install)

c:\> choco install nodejs.install

# you should now be able to run "git" and "node" from the command line (might need to re-open cmd prompt for them to be in the path)

# Step 04: Next Install Bower globally (http://bower.io/)

c:\> npm install -g bower

# Step 05: Next Install Babel globally (https://babeljs.io/)

npm install -g babel-cli

# Step 06: Other Installs

c:\> npm install -g typescript
c:\> npm install -g eslint
c:\> npm install -g yo
c:\> npm install -g qunitjs

That's about the long and short of it!

So, that's my setup and what i'm using for my personal projects. Now its time to write some code...


Contact Me:  ocean.airdrop@gmail.com

Monday, 22 February 2016

Coarse Grained CPU-Profiling for Applications using Process Explorer

Today I wanted to quickly graph an applications CPU performance and compare how different start-up flags affected the CPU performance of the application. I didn't want to open perfmon.exe and go through the rigmarole of selecting the correct perf counters yadda-yadda.. I just wanted something quick and dirty..

As we all know, Process Explorer is the swiss-army knife for all things process based. If you need to do any diagnostics on a process then chances are Process Explorer will be able to help in some way.

Enter Process Explorer with its "Performance Graph" tab and this quick little tip!

With Process Explorer running, go to the process in question and select the "Performance Graph" tab. You will see something like this:

This tab displays the "CPU usage" for the process, the "memory usage" and the "disk I/O". All useful information. As an aside, watching to see if the memory usage increases is a basic way to see if your app has a memory leak.

I was interested in the CPU usage for this process which is the top graph. With the mouse you can hover over the graph to get a time and a CPU usage number but the 3 guidelines show you the 25%, 50% and 75% marks for rough approximation.

As you can see in the image above, the application is behaving quite nicely. For the majority of time it is idling under the 25% mark. Now, let’s alter some of the start-up flags, restart the application and compare the results from the graph below with the graph above.

You can clearly see that these settings increase the CPU usage for this process as the CPU now consistently hovers around 25% mark for the duration of the run.

One thing to be weary of is that these graphs don’t update if Process Explorer is minimized to the tray..

But hey... Like I said, its quick and dirty but that’s good enough for me today!


Contact Me:  ocean.airdrop@gmail.com

Sunday, 21 February 2016

To-do or Not-to-do?.. That is the question.

"Note to Self: Remember to write a to-do list"

As a full-stack, dev-ops software developer we have to juggle a lot of moving parts. We work on multiple projects with multiple time-frames in addition to looking after servers, performing system-administration and keeping up with the constant changes in the tech landscape.

In short, we can have a lot going on at any one time.

Who doesn’t love a good to-do list?

Over the years I have used everything from pen-and-paper to test driving the latest tools of the moment to try and keep up-to-date and on top of what I need to do. Everyone out there might have their own system, but if there is one thing we can all agree on, its the importance of keeping some kind of to-do list.

Choosing what to-do from your to-do list

But writing the to-do list is only half the battle.

We’ve all been there. You finish collating everything you need to do, and then, inevitably you sit back and think "Gee. Which one of these should I be doing first?"

Usually it’s the task you leave until last. The big one. The gnarly one. That’s usually the task you should be starting first.

It’s as simple as this. If you have a good to-do list and are able to prioritise your work then you will be more effective in your job as you will be working on the "right" things.

I can’t count the number of times I have been "busy" working on the wrong things. I’m sure we’ve all done it at some point. Spend ages on a small cosmetic bug while there is a humongous bug just sitting there waiting for us to start it.

Identifying Priority's: The Eisenhower Priority Matrix

The biggest problem I have is identifying "what I should do next". Out of everything I have to do, what is my next highest priority task? That’s where the Eisenhower Priority Matrix comes into play. It identifies tasks based on their importance and their urgency.

If you do a Google search for "Eisenhower principle" you will most probably stumble upon this famous quote of his:

"What is important is seldom urgent and what is urgent is seldom important"

His matrix is simple and it goes like this:

  • If it’s important and urgent you need to do it now.
  • If it’s important but not urgent you should plan in time to do it

As an example, if you have identified that your car tax is due (a very important task) but your house is on fire (a very urgent and important task to resolve), I don’t think many people would have trouble identifying what they need to do first. Quick!!!! Phone the fire brigade immediately and run back into the house to save the old Amiga 500, Nintendo and original Xbox! (Actually, the original Xbox could probably survive a nuclear blast but that’s another story)…

Unfortunately, when we scan through our day-to-day to-do list, our items usually don't jump off the page like a burning house.

I like the definition for the Eisenhower Priority Matrix and how to use it provided on the MindTools website. Go here and have a read [here]

Like I say, there are countless to-do apps out there but if you wanted to incorporate the Eisenhower principal in your own to-do list then below is a simple example.

public double CalculatePriortyValue()
{
    CalculatedPriority = 0;
    if ( this.IsComplete == 1)
        return CalculatedPriority;

    // Minus 1 day so that you finish this task a day before its due
    double daysDueIn = ((DueDate - DateTime.Now).TotalDays - 1);

    if (daysDueIn < 1)
    {
        // This date is in the past. flip the value to a positive.  
        // The further in the past it is the more urgent it is.
        daysDueIn = Math.Abs(daysDueIn);

        if ( Math.Abs(daysDueIn) < 0.1)
            daysDueIn = 1;

        // Multiply importance by Urgency then multiply it by the number of days in the past it is!
        // The further in the past it is the later the task is and bigger the priority.
        CalculatedPriority = (Importance * Urgency) * daysDueIn;
    }
    else
    {
        // Multiply the importance and urgency then 
        // divide it over the number of days
        // we have got to do it in.
        CalculatedPriority = (Importance * Urgency) / daysDueIn;
    }

    return CalculatedPriority;
}

Revising your to-do list

The hardest part of any to-do list is keeping it up-to-date. A to-do list is no good if it’s not updated. I know this from experience. I’ve gone through cycles of using them then forgetting about them. However, for your to-do list to be effective you need to live inside it. You need to update it constantly so you know "what’s next". If that’s ticking boxes on a piece of paper or using the latest fandangled mobile app, if you’re not updating it “you’re not doing it right”.

The "Not" to-do List

This brings me nicely onto the not-to-do list.

You see, the great thing about the software dev landscape is that there is always "something new to learn". However, the bad thing about the software dev landscape is that there is always "something new to learn"!

If you’re like me, you probably have lots of new software dev areas that you want to learn and explore. It’s quite common for me to constantly feel like I am playing “catch up” because the amount of “stuff” to learn out there has exploded.

Let’s face it. It’s simply impossible for you to learn everything. There aren't enough hours in the day!

So you end up carrying around a lot of mental "to-do" cards. Things in the back of your mind that you think you should be doing but in reality haven't got the time for. Truth is, you’re probably never going to get the time to do everything. Time is against all of us.

This is where the not-to-do list comes into play and in my mind is just as important as the to-do list.

Think of the not-to-do list as a way of clearing your mental cache.

Before, you would stick X, Y or Z on your to-do list and it would forever expand. You would feel as if you are drowning under the stuff you need to do. But the truth is your never going to get around to doing everything on that list. The tasks that are “ideas”, “stuff to learn”, “nice-to-have features” all should be added to the “not-to-do list”. If it doesn’t have a due date, stick it on the not-to-do list. It can’t be that important.

Before, you ended up carrying around a lot of mental cards in your head, but by adding these items to your not-to-do list your mental cache is cleared. The fog dissipates and you can focus on the important things. At the same time you haven’t forgotten about these tasks. Because the not-to-do list will keep a record of them for you.

Now.... Note to Self: Remember to write my to-do list"

Contact Me:  ocean.airdrop@gmail.com

Sunday, 7 February 2016

C# Dynamic Types & Expando Object

Let me get this straight!! You want to declare an Object but you don't know what member variables you need ahead of time. Have you gone mad?

JavaScript Land

You've got to love the dynamic nature of JavaScript and how the language allows you to mutate objects at run-time. You don't need to define class definitions of an object upfront. That's so old-skool!

You can just create them... on the fly!
var movie      = new Object();
movie.name     = "Interstellar";
movie.genre    = "Sci-Fi";
movie.director = "Christopher Nolan";
Later, if you want to add a new member variable or a function at run-time.. Just add it!
movie.Rating = 8;
And of course you could do the same thing using the object literal notation:
var movie2 = {
  name: "Aliens",
  genre:"Action",
  director:"James Cameron"
};
and later:
movie2.Rating = 9;

C# Land

In C# land, things are a little different. A bit more traditional. Our objects need to be declared up front with a class definition. And the blueprint we provide for our classes are very hard to change. I guess they don't call them concrete classes for nothing!.

However, there are times when you would like to have the flexibility of JavaScript in C# and be able to "mutate objects on the fly".

With C# Version 4 that became possible with the dynamic keyword and the ExpandoObject.

Check out this C# code sample below. Its eerily similar to the JavaScript above. The only difference is on the 1st line with the dynamic keyword.
dynamic movie  = new ExpandoObject ();
movie.name     = "Interstellar";
movie.genre    = "Sci-Fi";
movie.director = "Christopher Nolan";
In essence, the dynamic ExpandoObject lets you add (expand) and remove (contract) member properties & functions of an object.

However, if you look at the above code, even though we haven't created a traditional "class definition" for the movie object, the fields are still setup at compile-time. The "name", "genre" and "director" fields are typed into the code at compile-time. Not run-time.

This brings us onto the second cool thing about the ExpandoObject.  It allows you to create an object like above using a dictionary. This means we can dynamically modify an object at run-time, adding or removing properties as required.

For example:

dynamic movie  = new ExpandoObject ();
movie.name     = "Interstellar";
movie.genre    = "Sci-Fi";
movie.director = "Christopher Nolan";

// Later on, lets add some more property via a dictionary
var dictionary = movie as IDictionary ;
dictionary.Add("Rating", "PG-13");
dictionary.Add("ReleaseDate", "2014-11-07");
dictionary.Add("BlahBlah", "SomeValue");

// Opps! Lets remove the field BlahBlah.. 
((IDictionary)movie).Remove("BlahBlah");

Example Usage

So armed with this knowledge, how can we make use of this? Well, a perfect use-case for this is when sending JSON data from a sever to the client. Using this dynamic object the server could tailor the data returned to the client, based on its requirements.

For example, this helper function "ConvertToDynObj" will convert any object to a dynamic ExpandoObject

public static dynamic ConvertToDynObj(object data, List fieldsToCopy = null )
{
    var dynObj = new ExpandoObject() as IDictionary;

    foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(data.GetType()))
    {
        if (fieldsToCopy == null )
        {
            dynObj.Add(property.Name, property.GetValue(data));
        }
        else if (fieldsToCopy.Contains(property.Name) == true)
        {
            // we only want to include this field in the new dynamic type
            dynObj.Add(property.Name, property.GetValue(data));
        }
    }

    return dynObj;
}
With the helper function defined above, we can mutate existing objects in our class library. Lets say we have a business object defined in our system that looks like this:
class BusinessDuberie
{
    public int fieldOne { get; set; }
    public string fieldTwo { get; set; }
    public double fieldThree { get; set; }

    public BusinessDuberie()
    {
        fieldOne = 1; fieldTwo = "2"; fieldThree = 3.3; 
    }
}
We can now convert the object to a dynamic object wholesale like this:
BusinessDuberie obj = new BusinessDuberie();

var dynObj = ConvertToDynObj(obj);

// Now we can add properties on the fly

(dynObj as IDictionary).Add("Qwerty", "Berty");

We could also trim the object and only copy the properties we are interested in.

List fieldsToCopy
fieldsToCopy.add("fieldOne");
fieldsToCopy.add("fieldThree");

var dynObj = ConvertToDynObj( obj, fieldsToCopy );
Now that's what I call sweet.

Contact Me:  ocean.airdrop@gmail.com

Thursday, 28 January 2016

Ho-Ho-Ho.... Now I have a Blog!

Come out to the internet... We'll get together, have a few laughs...

Is it me or does anyone else suddenly feel claustrophobic?

Hello World! - I am a software developer with over 15 years of experience, but as we all know, in this industry there is always something new to learn. As software developers, we are constantly learning new skills as the goal posts are forever moving.

Which brings me to this blog!

Its main purpose is to act as an aide-memoire for myself. I hope to chart any personal learning experiences here as well as mention stuff I think is cool.

Later down the line, I will be able to look back and think to myself "huh! Remember when I thought 'doing it that way' was cool!" - Oh, how I will laugh.

It only seems right to christen this blog post with an obligatory piece of code:

namespace OceanAirdrop
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
Contact Me:  ocean.airdrop@gmail.com

Popular Posts

Recent Posts

Unordered List

Text Widget

Pages