Login
A chronicle of the thoughts, learning experiences, ideas and actions of a tech junkie, .NET, JS and Mobile dev, aspiring entrepreneur, devout Christian and travel enthusiast.

As the tool developer put it
Siege is an http load testing and benchmarking utility. It was designed to let web developers measure their code under duress, to see how it will stand up to load on the internet.
That comes in handy when developing applications to ensure that developed solutions can scale to handle several hundred or thousand concurrent users. This guide would not go into the details of testing but would help get the tool installed on your device which is an important prerequisite.
First, open up the Terminal app or iTerm2 or any other terminal application you use. For this guide, we would be saving the downloaded archive in the Downloads directory.
Run the following commands in the open terminal
cd ~/Downloads
curl -O http://download.joedog.org/siege/siege-latest.tar.gz
tar -xvf siege-latest.tar.gz
cd siege-latest
./configure
make
make install
siege -V
## SIEGE 4.0.4
And you are done.
Cheers.
If you don't know what Hyper is, it is a beautiful, cross-platform terminal app for Windows, Mac OS and Linux built by the nice folks over at zeit.co using electron and React. I naturally have an aversion to apps built with electron because as a general rule, they consume more system resources than a native application would but I could not pass this up and a look at my terminal would give you an inkling I couldn't.

To install Hyper, follow the steps laid down below:
head to the project website at https://hyper.is/ and click on the "Download for Windows" button. This downloads an *.exe installer file to your Downloads folder or wherever you save downloaded files to.
Navigate to the folder using Explorer and double click on the installer. The installer copies the necessary files to the necessary places and notifies you once that is done.
Search for Hyper in the start menu or double click the shortcut on your Desktop and you should see a terminal interface similar to the one shown below.

If you are like me, you would not install Hyper to use CMD so it is time to pimp the terminal up and make bash our default shell for Hyper. My assumption is that you have Git for Windows installed. if you don't, follow this guide. With that done, follow the steps:
hyper binary added to your PATH. Click the top left hamburger menu (☰) => Plugins => Install Hyper CLI command in PATH.
Close and reopen your terminal (this may be necessary to refresh the shell)
Run the command hyper i hyper-afterglow to install the hyper-afterglow theme and give your terminal beautiful colours.
Click the top left hamburger menu (☰) => Edit => Preferences or Ctrl + ,. A file .hyper.js would be opened in Notepad

Change the shell key in the config to the following:
config: {
...
shell: 'C:\\Program Files\\Git\\git-cmd.exe',
...
}
Change the shellArgs key in the config to the following:
config: {
...
shellArgs: ['--command=usr/bin/bash.exe', '-l', '-i'],
...
}
Change the env key in the config to the following:
config: {
...
env: {TERM: 'cygwin'},
...
}
Save the file and close Notepad.
Close and reopen Hyper and see a terminal you'd actually like using.

This is an open-source project and if you want to contribute by fixing bugs, implementing features or just filing issues, you can do that at https://github.com/zeit/hyper
Cheers

image credit: slideshare
I got started with NodeJS about three years ago. As I learnt to build enterprise-grade applications using the runtime, a major drawback I kept coming across in my research was the fact that NodeJS is aggressively single-threaded. What this meant was that no matter how many threads and cores your computer or server had, the NodeJS process was limited to the one thread on which it was running. This major limitation meant a NodeJS web application could not fully utilize the hardware on which it was running.
Enter the cluster module. The cluster module is one of the native (it comes bundled with the NodeJS runtime and doesn't require installing from NPM and what not) modules and it gives developers the ability to utilize the hardware available to their applications by allowing a single NodeJS web application utilize worker processes to help handle requests as well.
After coming across the cluster module, I felt integrating it into my application would require quite a number of changes to make the logic "process-aware" and "process-safe", that turned out to be a wrong assumption as the integration into an existing project was as painless as it could possibly be.
To get started, first require the cluster module as well as the os module (would explain why in a bit) in your server.js file or whatever JavaScript file your HTTP server creation logic is in like so:
...
const cluster = require('cluster');
const os = require('os');
...
Next, we want to add logic to determine what process our code is currently running in, i.e whether or not it is a master or worker process. We can do that by checking the isMaster boolean field on the cluster object.
...
if (cluster.isMaster) {
// this is the master process
} else {
// this is a worker process
}
...
Next, when the master process starts, we want to create worker processes based on the number of cores the current machine has. This is where the os module comes into play, the os module gives access to operating system level information and logic. We create worker/child process like so:
if (cluster.isMaster) {
// this is the master process
const cpuCount = os.cpus().length;
for (let i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
} else {
// this is a worker process
}
...
Now we have successfully created our worker processes, next, we want to give the worker processes the logic they should execute when they are up. For this, we copy the logic we used to set up our web server into the else block. The logic to set up your web server would be different from mine but I'll use mine as an example. I am setting up an express web server which would provide a bunch of RESTful endpoints as well as serve some static files. This is what my web server logic looks like:
...
} else {
const app = express();
const router = express.Router();
const port = process.env.PORT || 3000;
app.use(morgan('dev'));
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
routes.route(router);
app.use('/v1/', router);
app.use('/', express.static('public'), (req, res) => {
res.status(200).sendFile(path.join(__dirname, '/public/index.html'));
});
app.listen(port, () => console.log(`Server started on ${port}`));
}
...
One last step, we need to know when one or more worker processes die or a killed off and replace them. To do that, we listen for the exit event on the cluster and carry out our revival efforts there.
...
cluster.on('exit', function (worker) {
console.log('Worker %d died :(', worker.id);
cluster.fork();
});
With that last step, we are good to go. Your web server should be ready to handle a bunch more requests right off the bat. The final, complete server.js file can be found here
Cheers.

As a quick refresher, MongoDB is an open-source document database and leading NoSQL database. MongoDB uses JSON-like documents with schemata. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License (SSPL).
This guide is meant for everyone, but especially for those who have worked with Entity Framework and are trying their hands at MongoDB. The aim of this guide is to maintain a semblance of familiarity. This means we would not work with Find, FilterDefinition, Cursors and Bson instead, relying on the LINQ methods we have come to know and love. If the keywords mentioned do not mean anything to you, then this guide is for you. If they did, still peruse this article, it may offer some new ideas.
For the purpose of this guide, we would work with a simple POCO (Plain Old Csharp Object). This would define the typed object that we would create, read, update and delete.
using System;
using System.Collections.Generic;
using MongoDB.Bson.Serialization.Attributes;
class Appointment
{
[BsonId]
public int Id {get; set;}
public DateTime StartDateTime {get; set;}
public List<string> Attendees {get; set;}
}
Adding data to a collection is quite simple. You retrieve the collection, create an instance of your data model and insert. The code sample shows just that.
using MongoDB.Driver;
using System;
using System.Collections.Generic;
...
// set up mongo db
var connectionString = "mongodb://..."; // a mongo database url
var mongoClient = new MongoClient(connectionString);
var mongoDatabase = mongoClient.GetDatabase("database-name");
var collection = mongoDatabase.GetCollection<Appointment>("appointments");
...
// instantiate the object with the necessary data
var appointment = new Appointment
{
Id = 12, // ensure no object has this value
StartDateTime = DateTime.UtcNow.AddDays(2),
Attendees = new List<string> { "Jane", "John" }
};
...
// persist the new object to the database
collection.InsertOne(appointment); // or await collection.InsertOneAsync(appointment); if you're all about that async life
It is as simple as that (mostly).
To retrieve data, the MongoDB client package provides the IMongoQueryable interface which allows us to run a number of operations on our data collections. A thing to note is that not all operations available on the IQueryable interface are currently translatable. Most of the staples are translatable though, this includes Skip, Take, First, FirstOrDefault, Count and Where amongst others.
NB: To make LINQ queries, you need to add using System.Linq; and using MongoDB.Driver.Linq; directives otherwise you may have compiler errors.
That said, let's say I want to find all appointments that would be holding two days from now and have a certain John attending, I want the first two results skipped and I want to limit my response to the first seven entries, my code should take the form:
using System;
using System.Linq;
using MongoDB.Driver.Linq;
...
// follow the mongo set up above
...
// retrieve the results to my strangely specific query
var matchedAppointments = collection
.AsQueryable() // gets us that sweet sweet IMongoQueryable
.Where(x => x.StartDateTime == DateTime.Now.Date.AddDays(3) && x.Attendees.Any(y => y == "John"))
.Skip(2)
.Take(7)
.ToList();
Assuming I set the wrong value on an entry in the database collection or I just want to change a property on an entry. I need to follow a three-step process: retrieve the errant entry, update the fields, replace the old entry. The following code sample shows that in action:
using System;
using System.Linq;
using MongoDB.Driver.Linq;
...
// follow the mongo set up above
...
// retrieve the entry
var appointment = collection
.AsQueryable()
FirstOrDefault(x => x.Id == 12);
if (appointment != .null)
{
appointment.StartDateTime = new DateTime(2020, 2, 20);
// save the changes
collection.ReplaceOne(x => x.Id == appointment.Id, appointment); // or preferably, await collection.ReplaceOneAsync(x => x.Id == appointment.Id, appointment);
}
Well, all good things come to an end and the time would come where you would need to remove an entry from a collection. This perhaps is the simplest operation to perform. Following from the code shown above, if I want to delete the entry with the id of `121. I would write:
using System;
using System.Linq;
using MongoDB.Driver.Linq;
...
// follow the mongo set up above
...
// remove the erring entry
collection.DeleteOne(x => x.Id == 12);
I believe the aims of this guide have been accomplished. You should be able to carry basic CRUD operations on your MongoDB database. If you need to handle some more advanced querying, I would recommend this article.
Cheers.
Recently decided to try my hands at ASP.NET Core with an SPA, Angular to be precise. It was pretty simple to set up in fact. It was as simple as running dotnet new angular in the target folder and tada I had a fully formed template in the folder with a few goodies baked in. The folder structure generated is shown below
After making minor tweaks, I decided to deploy it and decided on Heroku. Now, while Heroku does not have official support for .NET Core, there are a few great open source buildpacks out there in the wild. The one I have worked with for over a year is developed and maintained by @jincod over at GitHub. Over the year i have been working with the build pack I have seen several advances made, from support for projects with migrations to the huge reduction in the generate slug size and a host of other things. Therefore, in trying to get this project deployed, I defaulted to that again and hit a few roadblocks which I was able to work through or around and why I am writing this guide.
Before getting into the meat of this guide, I would assume that you have created your heroku app, linked the app to your local git repo and have logged in to heroku on your machine via the heroku CLI. If you have not, please run heroku login and follow the prompt. That done, change into the heroku app directory and lets proceed:
Add the official node JS buildpack to your app:
heroku buildpacks:set --index 1 heroku/nodejs
Add the .NET buildpack to your app
heroku buildpacks:set --index 2 https://github.com/jincod/dotnetcore-buildpack
If you tried deploying your code to heroku at thige.jso9ns point, you would get a build error. That is because the nodejs build pack looks for a package.json file in the root of the repo and since there is none, it fails. To combat that, run
npm init -y
this adds a basic package.json file and we are good to go. Deploy your app by running
git push heroku master
(this assumes that you named your heroku remote heroku). That is all people, now go forth and build awesome things.
Cheers