Skip to content

Instantly share code, notes, and snippets.

View NickCraver's full-sized avatar
:shipit:
Shipping

Nick Craver NickCraver

:shipit:
Shipping
View GitHub Profile
@NickCraver
NickCraver / ForSyntaxHighlights.cs
Last active February 20, 2016 01:09
How to be a jerk using SQL and Dapper
var tb = AppDomain.CurrentDomain
.DefineDynamicAssembly(new AssemblyName("Test"), AssemblyBuilderAccess.Run)
.DefineDynamicModule("DM")
.DefineType("Type with spaces in the name", TypeAttributes.Public);
tb.DefineField("Is💩Post", typeof(bool), FieldAttributes.Public);
var spt = tb.CreateType();
this.Connection.QueryFirstOrDefault(spt, "Select 1 as [Is💩Post]").Dump();
@NickCraver
NickCraver / DapperExample.cs
Last active February 21, 2016 20:53
Query Optimization: Literal Example
// Here's a normal query as you'd expect
db.QuerySingleOrDefault<PostCacheEntry>(@"SELECT
(Select Count(*) From Posts Where PostTypeId = @Question And DeletionDate Is Null) TotalQuestions,
(Select Count(*) From Posts Where PostTypeId = @Question And IsNull(IsAnswered, 0) = 0 And AcceptedAnswerId Is Null And ClosedDate Is Null And DeletionDate Is Null) TotalUnanswered,
(Select Count(*) From Posts Where PostTypeId = @Question And AcceptedAnswerId Is Not NulL And DeletionDate Is Null) TotalAccepted,
(Select Count(*) From Posts Where PostTypeId = @Answer And DeletionDate Is Null) TotalAnswers",
new { PostTypeId.Question, PostTypeId.Answer });
// In this case though, PostTypeId.Question and PostTypeId.Answer are values that never change.
// In this case they're constants, but this same trick works for enums.
@NickCraver
NickCraver / CSharp6GetOnlyOverride.cs
Created March 9, 2016 14:23
Example of get-only overrides in C# 6 and fun side-effects.
void Main()
{
var child = new Child();
child.Prop = false;
child.Prop.Dump();
var propertyInfo = typeof(Child).GetProperty(nameof(Child.Prop));
propertyInfo.GetGetMethod().Dump();
propertyInfo.GetSetMethod().Dump();
}
public class Base
@NickCraver
NickCraver / Readme.md
Created April 10, 2016 19:40
A simple LINQPad script I wrote for load testing SQL Server.

This is a simple LINQPad script I wrote one day to load test some large SQL servers. Maybe it's useful to someone. The basic premise is defining your queries once, including which ID patterns to fetch (at the bottom), and load test a mixture. The script defines everything needed in one place, then fires up the command-line linqpad runner to run many queries at once.

Params up top:

const string LinqPadPath = @"C:\Linqpad\lprun.exe";
const bool runSequential = false;
const int defaultThreads = 1;
const int defaultIterations = 2000;
@NickCraver
NickCraver / BotLovin.cs
Last active January 5, 2023 11:36
Some bot/crawler fun on Stack Overflow. 10 hours of fun, to be precise.
private static readonly string[] tenHoursOfFun =
{
"https://www.youtube.com/watch?v=wbby9coDRCk",
"https://www.youtube.com/watch?v=nb2evY0kmpQ",
"https://www.youtube.com/watch?v=eh7lp9umG2I",
"https://www.youtube.com/watch?v=z9Uz1icjwrM",
"https://www.youtube.com/watch?v=Sagg08DrO5U",
"https://www.youtube.com/watch?v=5XmjJvJTyx0",
"https://www.youtube.com/watch?v=IkdmOVejUlI",
"https://www.youtube.com/watch?v=jScuYd3_xdQ",
@NickCraver
NickCraver / Build.xml
Last active July 7, 2023 16:40
Stack Overflow Build Reference Docs
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="PrepareStaticContent" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!-- Passed in Parameters -->
<configuration></configuration>
<workingDir></workingDir>
<buildNumber></buildNumber>
<buildViews>false</buildViews>
<minifyJs>true</minifyJs>
<TargetsDirectory></TargetsDirectory>
@NickCraver
NickCraver / TeamCity-Dev-Build-Log.txt
Created April 30, 2016 20:02
Stack Overflow Build Log Examples
Build 'Stack Exchange Network :: New York :: SENetwork - Dev' #21007
Started 'Wed Apr 27 23:54:04 UTC 2016' on 'NY-WEB05' by 'Git'
Finished 'Wed Apr 27 23:56:21 UTC 2016' with status 'NORMAL Success'
TeamCity URL https://build/viewLog.html?buildId=477326&buildTypeId=StackExchangeNetwork_NewYork_SENetworkDev
TeamCity server version is 9.1.5 (build 37377)
[23:54:03]i: TeamCity server version is 9.1.5 (build 37377)
[23:54:03]W: bt4 (2m:17s)
[23:54:03] : projectId:project55 projectExternalId:StackExchangeNetwork_NewYork buildTypeId:bt4 buildTypeExternalId:StackExchangeNetwork_NewYork_SENetworkDev
[23:54:03] : Collecting changes in 2 VCS roots (1s)
@NickCraver
NickCraver / Int32ToBase64.cs
Created May 14, 2016 11:52
Base64 Header Example
void Main()
{
"99999999".Dump();
Int32ToBase64(99999999).Dump();
}
private static readonly char[] EqualsChar = { '=' };
private static string Int32ToBase64(int i)
{
try
@NickCraver
NickCraver / QueryPlan.xml
Created June 5, 2016 12:53
SQL 2016 RTM AG Regression Query Plan
<ShowPlanXML xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan" Version="1.5" Build="13.0.1601.5">
<BatchSequence>
<Batch>
<Statements>
<StmtSimple StatementText="CREATE function sys.fn_hadr_is_same_replica(&#xD;&#xA;&#x9;@lag_id uniqueidentifier,&#xD;&#xA; @lag_replica_id uniqueidentifier,&#xD;&#xA;&#x9;@ag_replica_id uniqueidentifier)&#xD;&#xA;returns bit&#xD;&#xA;as&#xD;&#xA;begin&#xD;&#xA; declare @ag_id uniqueidentifier&#xD;&#xA;&#x9; declare @local_replica_id uniqueidentifier&#xD;&#xA;&#x9; declare @ret bit&#xD;&#xA;&#x9; &#xD;&#xA; SELECT @ag_id = group_id &#xD;&#xA; FROM sys.fn_hadr_distributed_ag_replica(@lag_id, @lag_replica_id)" StatementId="1" StatementCompId="3" StatementType="SELECT" RetrievedFromCache="true" StatementSubTreeCost="1.157e-006" StatementEstRows="1" SecurityPolicyApplied="false" StatementOptmLevel="FULL" QueryHash="0x48E1F82013EEA15A" QueryPlanHash="0x3443401473380A77" StatementOptmEarlyAbortReason="GoodEnoughPlanF
@NickCraver
NickCraver / Benchmark.cs
Last active June 9, 2016 12:27
TimeSpan.FromSeconds() benchmark
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnostics.Windows;
using BenchmarkDotNet.Running;
namespace Benchmarks
{
class Program
{