using NewRelic.Api.Agent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using NewRelicApi = NewRelic.Api.Agent.NewRelic;
namespace NRAgentAttributes
public static void Main(string[] args)
RunAsync(int.Parse(args[0])).Wait();
private static async Task RunAsync(int threadCount)
var tasks = new List<Task<long>>(threadCount);
var collectSemaphore = new SemaphoreSlim(0);
var startSemaphore = new SemaphoreSlim(0);
for (int threadId = 1; threadId <= threadCount; threadId++)
int threadIdCopy = threadId;
tasks.Add(Task.Factory.StartNew(() => Transaction(threadIdCopy, collectSemaphore, startSemaphore), TaskCreationOptions.LongRunning));
Console.WriteLine("Created all threads, waiting for all to be ready");
for (int i = 1; i <= threadCount; i++)
await collectSemaphore.WaitAsync();
Console.WriteLine("All threads ready");
startSemaphore.Release(threadCount);
long[] results = await Task.WhenAll(tasks);
Console.WriteLine("Overall Average: " + results.Average());
private static long Transaction(int threadId, SemaphoreSlim collectSemaphore, SemaphoreSlim startSemaphore)
int iterationsCount = 64;
string[] iterations = Enumerable.Range(1, iterationsCount).Select(x => x.ToString()).ToArray();
var sw = new Stopwatch();
collectSemaphore.Release();
for (int i = 0; i < iterationsCount; i++)
NewRelicApi.AddCustomParameter(iterations[i], iterations[i]);
return sw.ElapsedMilliseconds;