Thursday, January 05, 2017

Extension method to dump Highlighted search results in Linqpad

Here's an extension method which lets you output the found results as html.

For example, if you wanted to show the result of a search and replace, given:
 var text = "An apple a day keeps the doctor away\n you can say that any day of the year today or tomorrow.";
 var find = "day";
 var replace = "week";
 
 text.HighlightText(find).Dump("Original text");
 text.Replace(find, replace).HighlightText(replace).Dump("updated text");

The result would be:
Original text
An apple a 
day
 keeps the doctor away
 you can say that any 
day
 of the year.
updated text
An apple a 
week
 keeps the doctor away
 you can say that any 
week
 of the year.

See extension method below:

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