Skip to content

Instantly share code, notes, and snippets.

@markekraus
Last active December 9, 2017 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markekraus/b16f0991a843d2d2459aa4178dfe55b5 to your computer and use it in GitHub Desktop.
Save markekraus/b16f0991a843d2d2459aa4178dfe55b5 to your computer and use it in GitHub Desktop.
Test scripts and progranm for IRM memory leak troubleshooting
const express = require('express')
const app = express()
app.get('/api', function (req, res)
{
res.send('0')
})
app.listen(5001, function ()
{
console.log('Example app listening on port 5001')
})
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
namespace MemLeakRepro
{
class Program
{
static void Main(string[] args)
{
if(args.Length != 2)
{
System.Console.WriteLine("usage: <Uri> <Count>");
Environment.Exit(1);
}
DateTime start = DateTime.Now;
System.Console.WriteLine("Start: {0}", start);
Uri uri = new Uri(args[0]);
Int32 count;
if(!Int32.TryParse(args[1], out count))
{
count = 500000;
}
for (int i = 0; i < count; i++)
{
HttpClientHandler handler = new HttpClientHandler();
HttpClient client = new HttpClient(handler);
HttpResponseMessage response = client.GetAsync(uri).GetAwaiter().GetResult();
StreamReader reader = new StreamReader(response.Content.ReadAsStreamAsync().GetAwaiter().GetResult());
string result = reader.ReadToEnd();
reader.Dispose();
}
DateTime end = DateTime.Now;
System.Console.WriteLine("End: {0}", end);
TimeSpan time = end - start;
System.Console.WriteLine("Run: {0}", time);
Process currentProcess = Process.GetCurrentProcess();
double memoryInMB = currentProcess.WorkingSet64 / 1048576;
System.Console.WriteLine("Memory: {0:N2} mb", memoryInMB);
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
param(
[Parameter(Mandatory = $true)]
[ValidateSet('irm','httpclient','null','jsonobject','gc')]
[String]
$Test = 'irm',
[int]
$Count = 500000
)
$start = [datetime]::Now
'Start {0}' -f $start
if('irm' -eq $Test) {
foreach($i in 1..$Count){
$result = Invoke-RestMethod -Uri http://localhost:5001/api
}
}
if('gc' -eq $Test) {
foreach($i in 1..$Count){
$result = Invoke-RestMethod -Uri http://localhost:5001/api
[System.GC]::Collect()
}
}
if('httpclient' -eq $Test) {
foreach($i in 1..$Count){
$Handler = [System.Net.Http.HttpClientHandler]::New()
$HttpClient = [System.Net.Http.HttpClient]::New($Handler)
$Response = $HttpClient.GetAsync('http://localhost:5001/api').GetAwaiter().GetResult()
$Stream = $Response.Content.ReadAsStreamAsync().GetAwaiter().GetResult()
$reader = [System.IO.StreamReader]::new($Stream)
$result = $reader.ReadToEnd()
$reader.Dispose()
}
}
if('jsonobject' -eq $Test) {
foreach($i in 1..$Count){
$Handler = [System.Net.Http.HttpClientHandler]::New()
$HttpClient = [System.Net.Http.HttpClient]::New($Handler)
$Response = $HttpClient.GetAsync('http://localhost:5001/api').GetAwaiter().GetResult()
$Stream = $Response.Content.ReadAsStreamAsync().GetAwaiter().GetResult()
$reader = [System.IO.StreamReader]::new($Stream)
$result = $reader.ReadToEnd()
$jobject = [Microsoft.PowerShell.Commands.JsonObject]::ConvertFromJson($result,[ref]$null)
$reader.Dispose()
}
}
if('null' -eq $Test) {
foreach($i in 1..$Count){
$null = Invoke-RestMethod -Uri http://localhost:5001/api
}
}
$end = [datetime]::Now
'End: {0}' -f $end
'Run: {0}' -f ($end - $start)
'Memory {0:N2}mb' -f ([System.Diagnostics.Process]::GetCurrentProcess().WorkingSet64 /1mb)
#!/bin/bash
for i in $(echo "500000 1000000 1500000 2000000")
do
echo " "
echo "======================== $i ==============================="
echo "irm -noni -nop $i:"
pwsh -noni -nop -file ./test.ps1 -Test 'irm' -Count $i
echo " "
echo "null -noni -nop $i:"
pwsh -noni -nop -file ./test.ps1 -Test 'null' -Count $i
echo " "
echo "httpclient -noni -nop $i:"
pwsh -noni -nop -file ./test.ps1 -Test 'httpclient' -Count $i
echo " "
echo "jsonobject -noni -nop $i:"
pwsh -noni -nop -file ./test.ps1 -Test 'jsonobject' -Count $i
echo " "
echo "irm $i:"
pwsh -file ./test.ps1 -Test 'irm' -Count $i
echo " "
echo "null $i:"
pwsh -file ./test.ps1 -Test 'null' -Count $i
echo " "
echo "httpclient $i:"
pwsh -file ./test.ps1 -Test 'httpclient' -Count $i
echo " "
echo "jsonobject $i:"
pwsh -file ./test.ps1 -Test 'jsonobject' -Count $i
echo " "
echo "dotnet $i:"
~/.dotnet/dotnet run http://localhost:5001/api $i
echo "dotnet $i:"
~/.dotnet/dotnet run http://localhost:5001/api $i
echo "======================== $i ==============================="
echo " "
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment