Permalink
Browse files

some test changes

  • Loading branch information...
1 parent 0818390 commit 7b7b7dc3106dbf3c3581e080088e7d9ffe62e201 yangming committed Oct 1, 2012
@@ -1,13 +1,9 @@
using System;
-using System.Collections.Generic;
-using System.Linq;
+using System.Diagnostics;
using System.Text;
-using System.Net.Sockets;
using System.Threading;
-using System.Diagnostics;
using Statsd;
-
namespace TestPerformance
{
class TransportMock : ITransport
@@ -36,18 +32,17 @@ class ThreadPoolStatsd
private string bucketPrefix;
- public ThreadPoolStatsd(ManualResetEvent doneEvent, string bucketPrefix)
+ private IStatsdClient statsd;
+
+ public ThreadPoolStatsd(ManualResetEvent doneEvent, string bucketPrefix, IStatsdClient statsd)
{
this.doneEvent = doneEvent;
this.bucketPrefix = bucketPrefix;
+ this.statsd = statsd;
}
public void SendMessages(Object threadContext)
{
- StatsdPipe statsd = new StatsdPipe();
- statsd.Transport = new TransportMock();
- statsd.Strategy = new GeyserStrategy(500);
-
int threadIndex = (int)threadContext;
for (int i = 0; i < 100; i++)
{
@@ -72,7 +67,7 @@ static void TestUnderPressure()
Console.WriteLine("TestUnderPressure");
StatsdPipe statsd = new StatsdPipe();
statsd.Transport = new TransportMock();
- statsd.Strategy = new GeyserStrategy(500);
+ statsd.Strategy = new BufferedStrategy(500);
statsd.Increment(bucketPrefix + "pressure.multiple1");
try
@@ -101,61 +96,77 @@ static void TestMultiThreading()
ManualResetEvent[] doneEvents = new ManualResetEvent[10];
for (int i = 0; i < doneEvents.Length; i++)
{
+ StatsdPipe statsd = new StatsdPipe();
+ statsd.Transport = new TransportMock();
+ statsd.Strategy = new BufferedStrategy(500);
doneEvents[i] = new ManualResetEvent(false);
- ThreadPoolStatsd tps = new ThreadPoolStatsd(doneEvents[i], bucketPrefix);
+ ThreadPoolStatsd tps = new ThreadPoolStatsd(doneEvents[i], bucketPrefix, statsd);
ThreadPool.QueueUserWorkItem(tps.SendMessages, i);
}
WaitHandle.WaitAll(doneEvents);
}
- static void TestMailSlotStreaming()
- {
- Console.WriteLine("TestMailSlotStreaming");
- StatsdPipe statsd = new StatsdPipe();
- statsd.Strategy = new GeyserStrategy(2000);
-
- for (int i=0; i<10; i++)
- {
- //Thread.Sleep(r.Next(5));
- Thread.Sleep(1000);
- statsd.Timing(bucketPrefix + "newmailslot.timer", 16);
- statsd.Gauge(bucketPrefix + "newmailslot.gauge", 8);
- statsd.UpdateCount(4, bucketPrefix + "newmailslot.counter");
- statsd.UpdateCount(2, bucketPrefix + "newmailslot.counter");
- statsd.Increment(bucketPrefix + "newmailslot.counter");
- statsd.Decrement(bucketPrefix + "newmailslot.counter");
- }
- }
-
static void ShowExample(IStatsdClient statsd)
{
statsd.Gauge(bucketPrefix + "gauge", 500);
statsd.Gauge("test|Gauge(string message, string key, int value)", bucketPrefix + "gauge", 500);
- statsd.Timing(bucketPrefix + "timer", 500);
statsd.Timing("test|Timer(string message, string key, int value)", bucketPrefix + "timer", 500);
statsd.Increment(bucketPrefix + "counter");
statsd.Decrement(bucketPrefix + "counter");
statsd.UpdateCount(2, bucketPrefix + "counter");
- statsd.UpdateCount(3, 0, bucketPrefix + "counter", bucketPrefix + "counter2");
- statsd.UpdateCount(4, 2, bucketPrefix + "counter", bucketPrefix + "counter2");
- statsd.UpdateCount("test|UpdateCount(string message, string key, int value)", 5, bucketPrefix + "counter", bucketPrefix + "counter2");
- statsd.UpdateCount("UpdateCount(string message, string key, int value)|test", 6, 1, bucketPrefix + "counter");
+ statsd.UpdateCount(4, 0, bucketPrefix + "counter", bucketPrefix + "counter2");
+ statsd.UpdateCount(8, 2, bucketPrefix + "counter", bucketPrefix + "counter2");
+ statsd.UpdateCount("test|UpdateCount(string message, string key, int value)", 16, bucketPrefix + "counter", bucketPrefix + "counter2");
+ statsd.UpdateCount("UpdateCount(string message, string key, int value)|test", 32, 1, bucketPrefix + "counter");
}
+ static void SendingWithGeyserStrategy()
+ {
+ Console.WriteLine("SendingWithGeyserStrategy");
+ StatsdPipe statsd = new StatsdPipe();
+ statsd.Strategy = new BufferedStrategy(5000);
+ while (true)
+ {
+ Thread.Sleep(1);
+ statsd.Increment(bucketPrefix + "mailslot");
+ }
+ }
+
+ static void SendingWithoutGeyserStrategy()
+ {
+ Console.WriteLine("SendingWithoutGeyserStrategy");
+ StatsdPipe statsd = new StatsdPipe();
+ int i = 0;
+ StringBuilder sb = new StringBuilder();
+ sb.Append("a");
+ for (i = 0; i < 10; i++)
+ {
+ sb.Append(sb.ToString());
+ }
+ Console.WriteLine(sb.Length);
+ statsd.UpdateCount(sb.ToString(), 1, "a");
+ statsd.Strategy = new BufferedStrategy(5000);
+ while (true)
+ {
+ Thread.Sleep(1);
+ statsd.Increment(bucketPrefix + "mailslot");
+ }
+
+ }
+
static void TestBasic()
{
- Console.WriteLine("TestBasic");
+ Console.WriteLine("SendingWithoutGeyserStrategy");
StatsdPipe statsd = new StatsdPipe();
- statsd.Strategy = new GeyserStrategy(5000);
while (true)
{
Thread.Sleep(1000);
-
ShowExample(statsd);
}
}
+
static void TestTimeStamp()
{
Debug.WriteLine(TimestampHelper.Now);
@@ -164,11 +175,12 @@ static void TestTimeStamp()
static void Main(string[] args)
{
- TestUnderPressure();
- TestMultiThreading();
- TestMailSlotStreaming();
- ShowExample(new StatsdPipe());
- TestBasic();
+ //TestUnderPressure();
+ //TestMultiThreading();
+ //ShowExample(new StatsdPipe());
+ //SendingWithGeyserStrategy();
+ SendingWithoutGeyserStrategy();
+ //MailSlotTest.TestMailSlot();
}
}
}
@@ -0,0 +1,69 @@
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Runtime.InteropServices;
+using System.Text;
+using Microsoft.Win32.SafeHandles;
+
+namespace Statsd
+{
+ public class MailSlotTest
+ {
+ public static void TestMailSlot()
+ {
+ //int i = 0;
+ MailSlotTransport transport = new MailSlotTransport();
+ //StringBuilder sb = new StringBuilder();
+ //while (true)
+ //{
+ // if (transport.Ready)
+ // {
+ // sb.Append("|" + sb.Length);
+ // bool retval = transport.Send("csharp.test.mailslot:1|c|" + sb.ToString());
+ // Console.WriteLine("return " + retval);
+ // }
+ //}
+ string SLOTNAME = @"\\.\mailslot\afcollectorapi";
+ SafeFileHandle slotHandle = NativeMailSlot.CreateFile(SLOTNAME,
+ (uint)FileAccess.Write,
+ (uint)FileShare.ReadWrite,
+ 0,
+ (uint)FileMode.Open,
+ (uint)FileAttributes.Normal,
+ 0);
+ FileStream fs = new FileStream(slotHandle, FileAccess.Write);
+
+ UnicodeEncoding encoding = new UnicodeEncoding();
+ string data_string = string.Format("{0}:{1}:{2}", Process.GetCurrentProcess().Id, 3, "csharp.test.mailslot:1|c|");
+ byte[] data_bytes = encoding.GetBytes(data_string);
+ int byteCount = encoding.GetByteCount(data_string);
+ fs.Write(data_bytes, 0, byteCount);
+ fs.Flush();
+
+ int size = 0, nextsize = 0, count = 0, timeout = 0;
+ bool succeeded = false;
+ if (slotHandle.IsInvalid)
+ {
+ Console.WriteLine("MailSlot handle is invalid");
+ return;
+ }
+ try
+ {
+ succeeded = NativeMailSlot.GetMailslotInfo(slotHandle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
+ }
+ catch (Exception e)
+ {
+ Console.WriteLine(e.StackTrace);
+ }
+ if (succeeded)
+ {
+ Console.WriteLine(String.Format("size {0}, nextsize {1}, count {2}, timeout {3}",
+ size, nextsize, count, timeout));
+ }
+ else
+ {
+ Console.WriteLine(String.Format("can't get mailslot info {0:d}", Marshal.GetLastWin32Error()));
+ }
+ }
+ }
+}
@@ -47,6 +47,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ClientTest.cs" />
+ <Compile Include="MailSlotTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>

0 comments on commit 7b7b7dc

Please sign in to comment.