Wednesday, December 28, 2016

Boilerplate code to compare how long two operations take

This class allows you to wrap the stuff you want timed in a disposable using block. The timer is started when you enter the block and stops when the using clause is disposed.
void Main()
{
 double elapsed1 ;
 double elapsed2 ;
 var iterations = 1e7;
 using (var timedTask = new TimedTask("Method1"))
 {//Write your code here and measure its performance 
  for (var i = 0; i < iterations; i++)
  {
   Method1_DoStuff();
  }
  elapsed1 = timedTask.ElapsedMs();
 }

 
 using (var timedTask = new TimedTask("Method2"))
 {//Write your code here and measure its performance 
  for (var i = 0; i < iterations; i++)
  {
   Method2_DoStuff();
  }
  elapsed2 = timedTask.ElapsedMs();
 }
 
 if (elapsed1 < elapsed2)
  String.Format("method1 is {0:###,###.##}x faster than method 1", elapsed2/elapsed1, elapsed1, elapsed2).Dump();
 else
  String.Format("method2 is {0:###,###.##}x faster than method 2", elapsed1/elapsed2, elapsed1, elapsed2).Dump();

}

string Method1_DoStuff() {
 var x = "hello";
 x = x + " pretty";
 x = x + " world";
 return x;
}

private static string _field;
string Method2_DoStuff() {
 if (_field==null)
  _field = "hello pretty world";
 return _field;
}

class TimedTask : IDisposable
{
 string _taskName;
 Stopwatch sw;
 public TimedTask(string taskName)
 {
  _taskName = taskName;
  sw = new Stopwatch();
  sw.Start();
 }
 public double ElapsedMs()
 {
  return sw.Elapsed.TotalMilliseconds;
 }
 
 public void Dispose()
 {
  sw.Stop();
  string.Concat("completed ", _taskName, " in ", (sw.Elapsed.Ticks / 10000.0).ToString("###,##0.00 ms")).Dump();
 }


}


The result looks something like this:
completed Method1 in 559.24 ms
completed Method2 in 45.65 ms
method2 is 12.25x faster than method 2

download linqpad .linq file

Wednesday, December 21, 2016

Export to cURL from Fiddler

As a long-time user of Fiddler to monitor request traffic. It would be nice to export a request as a cURL command. Easy to save, easy to pass around.  Follow instructions here to add Copy as cURL command to your context options:

Click Rules > Customize Rules (Ctrl+R) and type this right before the closing curly bracket (})

BindUIButton("Copy cURL")
ContextAction("Copy cURL")
public static function doCopyCurl(arrSess: Session[])
{
var oExportOptions = FiddlerObject.createDictionary();
// If you'd prefer to save to a file, set the Filename instead
//oExportOptions.Add("Filename", "C:\\users\\lawrence\\desktop\\out1.bat");
oExportOptions.Add("ExportToString", "true");
  FiddlerApplication.DoExport("cURL Script", arrSess, oExportOptions, null);
var sOutput: String = oExportOptions["OutputAsString"];

Utilities.CopyToClipboard(sOutput);
FiddlerApplication.UI.SetStatusText("Copied Sessions as HAR");
}

code from https://textslashplain.com/2015/12/30/whats-new-in-fiddler-4-6-2/

Monday, December 12, 2016

My experience getting started with .net core with vscode

I created a new folder and opened command interface there. I typed

dotnet new -t Web

 The options for type are:
- { Name = Console, isMsBuild = True }
- { Name = Web, isMsBuild = True }
- { Name = Lib, isMsBuild = True }
- { Name = Mstest, isMsBuild = True }
- { Name = Xunittest, isMsBuild = True }


I hit run and it prompted me to setup launch and tasks json files. I selected .net core and continued. Then I remembered, oh right, I need to restore. so I ran

dotnet restore

This downloaded many dlls and I felt good about the project. Then I hit F5 and...  error! Aaargh!  Why .net why?   



The problem with this error - I don't know how to open the C# output window *YET*. The journey will continue when I figure that out.

Thursday, November 10, 2016

DotNetFiddle for your mvc experimenting needs

Thanks to dotnetrocks better know a framework for bringing this to my scattered attention:

https://dotnetfiddle.net/

enjoy

Tuesday, September 20, 2016

Technology Stack - tail your log files with Baretail

Baretail does a great job of giving you a view of what's going into your log files as data is written to it.

Check it out at http://www.baremetalsoft.com/baretail/

Friday, July 29, 2016

Secret sauce for your webapi - to always return json formatted result

Add this to your App_Start\RegisterWebAPIRoutes.cs file or wherever you happen to register your webapi routes

config.Formatters.Insert(0, new JsonpMediaTypeFormatter());
reference: http://stackoverflow.com/questions/10514047/how-do-i-handle-jsonp-with-webapi