Skip to content

Instantly share code, notes, and snippets.

@naraga
naraga / Pipes Yeilds
Created July 26, 2012 21:44
Yeilding objects and arrays to pipe.
function GetFewObjs()
{
New-Object PSObject -p @{
Name = "Bill"
Surname = "Gates"
}
New-Object PSObject -p @{
Name = "Barack"
@naraga
naraga / run_mssql_query.ps1
Created July 27, 2012 22:07
Executes MSSQL query and pushes output (as PSCustomObject objects) into pipe
function SelectFrom-Mssql([string] $selectQuery)
{
. sqlcmd -S "(local)" -d `"mydb`" `-Q "SET NOCOUNT ON $selectQuery" -s "|" -W |
ConvertFrom-CSV -Delimiter "|" |
select -skip 1
}
SelectFrom-Mssql "exec sp_who"
@naraga
naraga / profile.ps1
Created August 2, 2012 21:32
Poor man's Git prompt
function Get-GitRepoPath
{
@( $i=0;`
$pwd.path -split "\\"| `
%{"$(@($pwd.path -split '\\')[0..$i] -join '\')";$i++} | `
? {test-path "$_\\.git"})[0]
}
function Get-GitCurrentBranch
{
@naraga
naraga / write_build_out.ps1
Created August 26, 2012 09:23
Writes build output to the host highlighting error messages in red.
cat build_out.txt | %{switch -regex ($_) {"^err:" {write-host $_ -backgroundcolor "red"} default {write-host $_}}}
@naraga
naraga / PCQueueDemo.cs
Created September 18, 2012 22:08
Producer / Consumer queue optimized for IO bound operations
class Program
{
// PcQueue - demo
// Code copied from "C# 5.0 in a Nutshell: The Definitive Reference" by Joseph Albahari and Ben Albahari
// Modified by @borisbucha to efficiently support IO bound operations (simply adding TaskCreationOptions.LongRunning to consumer tasks)
// which sugests to scheduler to allocate dedicated thread to enqued items instead of poluting Threadpool.
static void Main()
{
Console.WriteLine("press any key when ready");
Console.ReadLine();
@naraga
naraga / Tasks - LongRunning.cs
Created September 18, 2012 22:33
Long running task effect on threads allocation
class Program
{
// TaskCreationOptions.LongRunning - demo
static void Main(string[] args)
{
Console.WriteLine("press any key when ready (run ProccessExplorer 's proccess Threads tab)");
Console.ReadLine();
for (int i = 0; i < 1000; i++)
{
@naraga
naraga / BlockingCollectionDemo.cs
Created September 18, 2012 22:37
BlockingCollection demo
class Program
{
// BlockingCollection - demo (approaching Q3)
static void Main(string[] args)
{
var coll = new BlockingCollection<string>();
Task.Run(() =>
{
@naraga
naraga / ConnPoolOverflowDemo.cs
Created September 18, 2012 22:40
What happens when sql connections runs out? You are gonna wait
class Program
{
// connection pool "overflow" demo
private const int ConnSize = 150;
static void Main(string[] args)
{
var connections = new SqlConnection[ConnSize];
for (int i = 0; i < ConnSize; i++)
{
@naraga
naraga / ConnPoolOverflowDemo.cs
Created September 18, 2012 22:40
What happens when sql connections runs out? You are gonna wait
class Program
{
// connection pool "overflow" demo
private const int ConnSize = 150;
static void Main(string[] args)
{
var connections = new SqlConnection[ConnSize];
for (int i = 0; i < ConnSize; i++)
{
@naraga
naraga / LinearStreamProccessingWithPlinq.cs
Created September 20, 2012 19:31
Linear stream proccessing with PLINQ
class Program
{
private const int RecordsCount = 3000;
private static readonly Stopwatch Stopwatch = new Stopwatch();
private const string InputDataFile =
"C:\\Users\\Boris\\Documents\\Visual Studio 2012\\Projects\\LinearStreamProccessingWithPlinq\\PlinqTests\\DataFile1.txt";
static void Main(string[] args)
{