Skip to content

Instantly share code, notes, and snippets.

View rianjs's full-sized avatar

Rian Stockbower rianjs

View GitHub Profile
@rianjs
rianjs / Results.md
Created July 22, 2023 12:42
Benchmarking string creation in C#

BenchmarkDotNet v0.13.6, macOS Ventura 13.4.1 (c) (22F770820d) [Darwin 22.5.0]
Intel Core i7-8850H CPU 2.60GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores
.NET SDK 7.0.102
  [Host]     : .NET 7.0.2 (7.0.222.60605), X64 RyuJIT AVX2
  DefaultJob : .NET 7.0.2 (7.0.222.60605), X64 RyuJIT AVX2


@rianjs
rianjs / find-tables-by-size.sql
Last active November 15, 2022 21:07
Useful Postgres scripts
SELECT
table_name,
pg_size_pretty(table_size) AS table_size,
pg_size_pretty(indexes_size) AS indexes_size,
pg_size_pretty(total_size) AS total_size
FROM (
SELECT
table_name,
pg_table_size(table_name) AS table_size,
pg_indexes_size(table_name) AS indexes_size,
@rianjs
rianjs / DynamoExtensions.cs
Created August 19, 2022 14:42
ToFormattedCapacityUnits
public static string ToFormattedCapacityUnits(this ConsumedCapacity c)
{
var sb = new StringBuilder();
sb.Append($"{c.ReadCapacityUnits:N1} RCUs");
sb.Append($"; {c.WriteCapacityUnits:N1} WCUs");
sb.Append($"; {c.CapacityUnits:N1} total CUs");
foreach (var (key, value) in c.GlobalSecondaryIndexes ?? new Dictionary<string, Capacity>())
{
sb.Append($"; GSI {key} {value.ReadCapacityUnits:N1} RCUs, {value.WriteCapacityUnits:N1} WCUs, {value.CapacityUnits:N1} total CUs");
@rianjs
rianjs / ReadCSV.cs
Created August 5, 2022 15:56
Read text file that's an embedded resource
var csvName = Assembly.GetExecutingAssembly().GetManifestResourceNames().Single(r => r.EndsWith("us_zipcodes.csv", StringComparison.Ordinal));
using var fileContents = Assembly.GetExecutingAssembly().GetManifestResourceStream(csvName)!;
using var reader = new StreamReader(fileContents);
var lines = new List<string>(41686);
string? line = null;
while ((line = reader.ReadLine()) is not null)
{
lines.Add(line);
}
@rianjs
rianjs / Program.cs
Created June 28, 2022 10:12
Old C# console app template with file-based namespacing and implicit usings
namespace SomeNamespace;
// Because doing anything non-trivial in C# requires the boilerplate anyway
public class Program
{
public static async Task Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
@rianjs
rianjs / MultipleEnumeration1.cs
Created June 19, 2022 11:25
Skip-Take through collection
const int take = 3;
var range = Enumerable.Range(0, 10).ToList();
var ptr = 0;
while (range.Skip(ptr).Take(take).Any())
{
foreach (var element in range.Skip(ptr).Take(take))
Console.Write(element + ", ");
ptr += take;
}
@rianjs
rianjs / bash
Created May 22, 2022 18:42
Reformat large json file
cat ugly.json | python3 -mjson.tool > pretty.json
@rianjs
rianjs / administrative-ops.md
Last active February 6, 2022 14:08
Useful, idempotent T-SQL operations

Find connection string currently in use

select
    'data source=' + @@servername +
    ';initial catalog=' + db_name() +
    case type_desc
        when 'WINDOWS_LOGIN' 
            then ';trusted_connection=true'
 else
@rianjs
rianjs / gitops.md
Last active March 13, 2023 01:20
Git operations

Common tasks

Delete merged branches

  • git remote prune origin prunes tracking branches not on the remote
  • git branch --merged lists branches that have been merged into the current branch
  • xargs git branch -d deletes branches listed on standard input
  • git branch --merged | xargs git branch -d plumbs everything together

Resolve a merge conflict

Let's suppose you have an automated merge failure going from the prod branch (33.0) to the test branch (33.1). So you will want to merge prod into test.

@rianjs
rianjs / JsonSerializerUtils.cs
Created October 11, 2021 13:32
Getting sensible JsonSerializerOptions at runtime
public static class JsonSerializerUtils
{
public static JsonSerializerOptions GetJsonSerializerSettings()
{
#pragma warning disable 162
#if DEBUG
return GetFormattedJsonSettings();
#endif
return GetUnformattedJsonSettings();