Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View JamesNK's full-sized avatar

James Newton-King JamesNK

View GitHub Profile
@JamesNK
JamesNK / spec.md
Last active June 19, 2023 23:56
System.Net.Http metrics enrichment with callbacks

System.Net.Http:

// Need to support adding multiple callbacks to support adding metrics from multiple places:
// 1. Creating HttpRequestMessage
// 2. During HTTP request pipeline in delegating handlers
// 3. In diaganostic event callbacks
public static class HttpOptionsExtensions
{
    public static void AddCustomMetricsTagsCallback(
@JamesNK
JamesNK / Program.cs
Created May 19, 2023 04:04
Detect HttpClient response end
using System.Diagnostics;
using System.Net;
var stopwatch = Stopwatch.StartNew();
var endCallback = () => Console.WriteLine($"{stopwatch.ElapsedTicks} - Response ended");
var httpClient = new HttpMessageInvoker(new DetectEndRequestHandler(new SocketsHttpHandler(), endCallback));
Console.WriteLine($"{stopwatch.ElapsedTicks} - Send request");
var response = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, "https://www.google.com"), CancellationToken.None);
Console.WriteLine($"{stopwatch.ElapsedTicks} - Response headers received");
BenchmarkDotNet=v0.11.4, OS=Windows 10.0.18363
AMD Ryzen 7 1700, 1 CPU, 16 logical and 8 physical cores
.NET Core SDK=3.1.400-preview-015151
[Host] : .NET Core 2.1.17 (CoreCLR 4.6.28619.01, CoreFX 4.6.28619.01), 64bit RyuJIT
DefaultJob : .NET Core 2.1.17 (CoreCLR 4.6.28619.01, CoreFX 4.6.28619.01), 64bit RyuJIT
Before:
| Method | messageCount | Mean | Error | StdDev | Gen 0/1k Op | Gen 1/1k Op | Gen 2/1k Op | Allocated Memory/Op |
|--------------------------------------------------------------------- |------------- |--------------:|--------------:|--------------:|------------:|------------:|------------:|--------------------:|
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
static class Program
#region Copyright notice and license
// Copyright 2019 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
@JamesNK
JamesNK / ZalgoParameterTransformer.cs
Created September 26, 2018 22:31
ZalgoParameterTransformer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Routing;
namespace MvcSandbox
{
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net461;netcoreapp2.1</TargetFrameworks>
</PropertyGroup>
</Project>
[2018-05-21T23:17:57] [TestLifetime] [Information] Starting test "CanCloseStreamMethodEarly_json_ServerSentEvents_default"
[0.000s] [ServerLogScope] [Information] Server log scope started.
[0.000s] [Microsoft.AspNetCore.SignalR.Client.HubConnection] [Verbose] Waiting on Connection Lock in "StartAsyncCore" ("/_/src/Microsoft.AspNetCore.SignalR.Client.Core/HubConnection.cs":239).
[0.000s] [Microsoft.AspNetCore.SignalR.Client.HubConnection] [Debug] Starting HubConnection.
[0.001s] [Microsoft.AspNetCore.Http.Connections.Client.HttpConnection] [Debug] Starting HttpConnection.
[0.001s] [Microsoft.AspNetCore.Http.Connections.Client.HttpConnection] [Debug] Establishing connection with server at 'http://127.0.0.1:1903/default'.
[0.002s] [Microsoft.AspNetCore.Http.Connections.Client.Internal.LoggingHttpMessageHandler] [Verbose] Sending HTTP request "POST" 'http://127.0.0.1:1903/default/negotiate'.
[0.004s] [SERVER Microsoft.AspNetCore.Server.Kestrel] [Debug] Connection id "0HLDVE3S42R5H" started.
[0.004s] [SERVER Micro
@JamesNK
JamesNK / validate-schema.ps1
Last active July 1, 2021 15:38
Validate Schema from PowerShell
$ErrorActionPreference = "Stop"
$NewtonsoftJsonPath = Resolve-Path -Path "bin\Newtonsoft.Json.dll"
$NewtonsoftJsonSchemaPath = Resolve-Path -Path "bin\Newtonsoft.Json.Schema.dll"
Add-Type -Path $NewtonsoftJsonPath
Add-Type -Path $NewtonsoftJsonSchemaPath
$source = @'
public class Validator
@JamesNK
JamesNK / Program.cs
Created March 24, 2017 00:21
Json.NET double tester - Program.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace ConsoleApp1
{