Skip to content

Instantly share code, notes, and snippets.

@Liversage
Created August 31, 2018 11:09
Show Gist options
  • Save Liversage/31e016ff0acfc330341858db9c11c7e3 to your computer and use it in GitHub Desktop.
Save Liversage/31e016ff0acfc330341858db9c11c7e3 to your computer and use it in GitHub Desktop.
Reading non-existent documents while debugging is very slow
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="1.9.1" />
</ItemGroup>
</Project>
using System;
using System.Diagnostics;
using System.Net;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
namespace CosmosDBDebugIssue
{
internal class Program
{
private static async Task Main(string[] args)
{
const string serviceEndpoint = "https://localhost:8081/";
const string accountKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
const string databaseId = "Test";
const string documentCollectionId = "Test";
const int documentCount = 500;
var client = new DocumentClient(new Uri(serviceEndpoint), accountKey);
await client.CreateDatabaseIfNotExistsAsync(new Database { Id = databaseId });
Console.WriteLine($"Created database {databaseId}");
await CreateDocumentCollectionAsync(client, databaseId, documentCollectionId);
var (readElapsed1, upsertElapsed1) = await ReadOrCreateDocumentsAsync(client, databaseId, documentCollectionId, documentCount);
Console.WriteLine($"Empty collection: read = {readElapsed1}, upsert = {upsertElapsed1}");
var (readElapsed2, upsertElapsed2) = await ReadOrCreateDocumentsAsync(client, databaseId, documentCollectionId, documentCount);
Console.WriteLine($"Non-empty collection: read = {readElapsed2}, upsert = {upsertElapsed2}");
await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId));
Console.WriteLine($"Deleted database {databaseId}");
Console.ReadKey();
}
private static async Task CreateDocumentCollectionAsync(DocumentClient client, string databaseId, string documentCollectionId)
{
try
{
var documentCollectionUri = UriFactory.CreateDocumentCollectionUri(databaseId, documentCollectionId);
await client.ReadDocumentCollectionAsync(documentCollectionUri);
await client.DeleteDocumentCollectionAsync(documentCollectionUri);
Console.WriteLine($"Deleted document collection {documentCollectionId}");
}
catch (DocumentClientException exception)
{
if (exception.StatusCode != HttpStatusCode.NotFound)
throw;
}
await client.CreateDocumentCollectionAsync(
UriFactory.CreateDatabaseUri(databaseId),
new DocumentCollection { Id = documentCollectionId },
new RequestOptions { OfferThroughput = 10000 }
);
Console.WriteLine($"Created document collection {documentCollectionId}");
}
private static async Task<(TimeSpan ReadElapsed, TimeSpan UpsertElapsed)> ReadOrCreateDocumentsAsync(DocumentClient client, string databaseId, string documentCollectionId, int count)
{
var readElapsedTicks = 0L;
var upsertElapsedTicks = 0L;
for (var i = 0; i < count; i += 1)
{
var documentId = i.ToString();
var readStopwatch = Stopwatch.StartNew();
await ReadDocumentAsync(client, databaseId, documentCollectionId, documentId);
readElapsedTicks += readStopwatch.ElapsedTicks;
var createStopwatch = Stopwatch.StartNew();
await UpsertDocumentAsync(client, databaseId, documentCollectionId, documentId);
upsertElapsedTicks += createStopwatch.ElapsedTicks;
if (i % 100 == 99)
Console.WriteLine($"Processed {i + 1} documents");
}
return (new TimeSpan(readElapsedTicks), new TimeSpan(upsertElapsedTicks));
}
private static async Task<Document> ReadDocumentAsync(DocumentClient client, string databaseId, string documentCollectionId, string documentId)
{
var documentUri = UriFactory.CreateDocumentUri(databaseId, documentCollectionId, documentId);
try
{
var response = await client.ReadDocumentAsync(documentUri);
return response.Resource;
}
catch (DocumentClientException exception)
{
if (exception.StatusCode != HttpStatusCode.NotFound)
throw;
return null;
}
}
private static Task UpsertDocumentAsync(DocumentClient client, string databaseId, string documentCollectionId, string documentId)
{
var documentCollectionUri = UriFactory.CreateDocumentCollectionUri(databaseId, documentCollectionId);
var document = new Document
{
Id = documentId
};
return client.UpsertDocumentAsync(documentCollectionUri, document);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment