Permalink
Browse files

Added Stopwatch Tick Provider

  • Loading branch information...
1 parent c7523bc commit cf8c9623621c0ef39d31cd7201b7518380936c69 @phatboyg committed Jan 18, 2012
Showing with 46 additions and 15 deletions.
  1. +21 −14 src/NewId.Tests/NewId_Specs.cs
  2. +1 −1 src/NewId/NewId.cs
  3. +1 −0 src/NewId/NewId.csproj
  4. +23 −0 src/NewId/Providers/StopwatchTickProvider.cs
@@ -3,54 +3,61 @@
using System;
using System.Diagnostics;
using NUnit.Framework;
+ using Providers;
[TestFixture]
public class Using_the_newid_generator
{
[Test]
- public void Should_generate_unique_identifiers_with_each_invocation()
+ public void Should_generate_sequential_ids_quickly()
{
+ NewId.SetTickProvider(new StopwatchTickProvider());
NewId.Next();
- var timer = Stopwatch.StartNew();
-
- int limit = 1024 * 1024;
+ int limit = 10;
- NewId[] ids = new NewId[limit];
+ var ids = new NewId[limit];
for (int i = 0; i < limit; i++)
{
ids[i] = NewId.Next();
}
- timer.Stop();
-
for (int i = 0; i < limit - 1; i++)
{
Assert.AreNotEqual(ids[i], ids[i + 1]);
+ Console.WriteLine(ids[i]);
}
-
- Console.WriteLine("Generated {0} ids in {1}ms ({2}/ms)", limit, timer.ElapsedMilliseconds,
- limit/timer.ElapsedMilliseconds);
}
[Test]
- public void Should_generate_sequential_ids_quickly()
+ public void Should_generate_unique_identifiers_with_each_invocation()
{
NewId.Next();
- int limit = 10;
+ Stopwatch timer = Stopwatch.StartNew();
- NewId[] ids = new NewId[limit];
+ int limit = 1024*1024;
+
+ var ids = new NewId[limit];
for (int i = 0; i < limit; i++)
{
ids[i] = NewId.Next();
}
+ timer.Stop();
+
for (int i = 0; i < limit - 1; i++)
{
Assert.AreNotEqual(ids[i], ids[i + 1]);
- Console.WriteLine(ids[i]);
+ string end = ids[i].ToString().Substring(32, 4);
+ if (end == "0000")
+ {
+ Console.WriteLine("{0}", ids[i].ToString());
+ }
}
+
+ Console.WriteLine("Generated {0} ids in {1}ms ({2}/ms)", limit, timer.ElapsedMilliseconds,
+ limit/timer.ElapsedMilliseconds);
}
}
}
View
@@ -73,7 +73,7 @@ static IWorkerIdProvider WorkerIdProvider
static ITickProvider TickProvider
{
- get { return _tickProvider ?? (_tickProvider = new DateTimeTickProvider()); }
+ get { return _tickProvider ?? (_tickProvider = new StopwatchTickProvider()); }
}
public int CompareTo(object obj)
@@ -43,6 +43,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Providers\DateTimeTickProvider.cs" />
<Compile Include="Providers\NetworkAddressWorkerIdProvider.cs" />
+ <Compile Include="Providers\StopwatchTickProvider.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@@ -0,0 +1,23 @@
+namespace NewId.Providers
+{
+ using System;
+ using System.Diagnostics;
+
+ public class StopwatchTickProvider :
+ ITickProvider
+ {
+ DateTime _start;
+ Stopwatch _stopwatch;
+
+ public StopwatchTickProvider()
+ {
+ _start = DateTime.UtcNow;
+ _stopwatch = Stopwatch.StartNew();
+ }
+
+ public long Ticks
+ {
+ get { return _start.AddTicks(_stopwatch.ElapsedTicks).Ticks; }
+ }
+ }
+}

0 comments on commit cf8c962

Please sign in to comment.