Skip to content

Instantly share code, notes, and snippets.

@normj
Created July 23, 2019 17:09
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 normj/8c492950fc8e289a6a757760f91f89da to your computer and use it in GitHub Desktop.
Save normj/8c492950fc8e289a6a757760f91f89da to your computer and use it in GitHub Desktop.
Attempt to reproduce DDB memory leak with DynamoDBContext
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DataModel;
using DynamoDBZipCodeStressTest.Shared;
namespace DynamoDBZipCodeStressTest.PerfTest
{
class Program
{
static async Task Main(string[] args)
{
try
{
using (var client = new AmazonDynamoDBClient(Amazon.RegionEndpoint.USWest2))
using (var context = new DynamoDBContext(client, new DynamoDBContextConfig { Conversion = DynamoDBEntryConversion.V2 }))
{
var zipCodes = await GetValidZipCodes(context);
var tasks = new List<Task>();
for(int i = 0; i < 50; i++)
{
tasks.Add(ReadZipCodes(context, zipCodes, i));
}
tasks.Add(EndlessScan(context));
await Task.WhenAll(tasks);
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
static async Task<List<string>> GetValidZipCodes(DynamoDBContext context)
{
var asyncSearch = context.ScanAsync<ZipCode>(new List<ScanCondition>());
var results = await asyncSearch.GetRemainingAsync();
var zips = new List<string>();
foreach(var item in results)
{
zips.Add(item.Code);
}
return zips;
}
static async Task ReadZipCodes(DynamoDBContext context, IList<string> zipCodes, int readerCount)
{
try
{
long readCount = 0;
while (true)
{
foreach (var code in zipCodes)
{
await context.LoadAsync<ZipCode>(code);
readCount++;
if (readCount % 1000 == 0)
Console.WriteLine($"Read count from reader {readerCount}: " + readCount);
}
}
}
catch(Exception e)
{
Console.WriteLine($"ERROR: Read {readerCount}, {e.Message}");
throw;
}
}
static async Task EndlessScan(DynamoDBContext context)
{
try
{
long scanCount = 0;
while (true)
{
var asyncSearch = context.ScanAsync<ZipCode>(new List<ScanCondition>());
var results = await asyncSearch.GetRemainingAsync();
scanCount++;
if (scanCount % 5 == 0)
Console.WriteLine("Scans read: " + scanCount);
}
}
catch (Exception e)
{
Console.WriteLine($"ERROR: Scan, {e.Message}");
throw;
}
}
}
}
using System;
namespace DynamoDBZipCodeStressTest.Shared
{
public class ZipCode
{
public string Code { get; set; }
public string ZipType { get; set; }
public string City { get; set; }
public string State { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment