Esempio n. 1
0
        public void Next_Base36_Test()
        {
            var snowflakeIdWorker = new SnowflakeIdWorker(1, 1);

            for (int i = 0; i < 1000; i++)
            {
                Console.WriteLine(snowflakeIdWorker.Next());
            }
        }
Esempio n. 2
0
        public void Next_Base36_Decode_Test()
        {
            var snowflakeIdWorker = new SnowflakeIdWorker(1, 1);
            var nextId            = snowflakeIdWorker.NextId();
            var encode            = Base36Converter.Encode(nextId);
            var decode            = Base36Converter.Decode(encode);

            Console.WriteLine(encode);
            Assert.AreEqual(nextId, decode);
        }
Esempio n. 3
0
        /// <summary>
        /// Start
        /// </summary>
        /// <param name="setting"></param>
        /// <param name="serializer"></param>
        public static void Start(CacheSetting setting, ICacheSerializer serializer)
        {
            _serializer = serializer;
            //_threadPools = new SmartThreadPool(180 * 1000, 100, 5);
            _entityQueueTimer = new Timer(OnEntitySyncQueue, null, 60, 100);
            MessageQueueSection section = GetSection();

            _IdWorker = new SnowflakeIdWorker(section?.SnowflakeIdWorkerId ?? 0, section?.SnowflakeIdCenterId ?? 0);
            InitRedisQueue(section);
            InitSqlQueue(section);
            //_threadPools.Start();
        }
Esempio n. 4
0
        public void NextId_Test()
        {
            var worker = new SnowflakeIdWorker(0, 0);

            var id1 = worker.NextId();
            var id2 = worker.NextId();

            Assert.IsTrue(id1 > 0);
            Assert.IsTrue(id2 > 0);
            Assert.IsTrue(id1 != id2);
            Console.WriteLine(id1);
            Console.WriteLine(id2);
        }
Esempio n. 5
0
        public void IDWorkTest()
        {
            long           startTimestmap = (long)(DateTime.UtcNow.AddDays(-20) - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
            IIDWorker      iDWorker       = new SnowflakeIdWorker(startTimestmap, 10, 1);
            int            count          = 300 * 10000;
            HashSet <long> idSet          = new HashSet <long>();
            object         testLock       = new object();
            AutoResetEvent autoResetEvent = new AutoResetEvent(false);
            int            invokeCount    = 0;

            try
            {
                for (int i = 0; i < count; i++)
                {
                    Task.Run(() =>
                    {
                        var id = iDWorker.NextId();
                        lock (testLock)
                        {
                            if (!idSet.Add(id))
                            {
                                autoResetEvent.Set();
                                return;
                            }
                        }

                        Console.WriteLine(id);
                        if (count == Interlocked.Increment(ref invokeCount))
                        {
                            autoResetEvent.Set();
                            Console.WriteLine("go!");
                        }
                    });
                }

                autoResetEvent.WaitOne();
            }
            finally
            {
                autoResetEvent.Dispose();
            }
            Assert.AreEqual(count, idSet.Count);
            Assert.AreEqual(count, invokeCount);
        }
        public void MainTest()
        {
            Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffffff"));
            SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);

            for (int i = 0; i < 1000; i++)
            {
                Trace.WriteLine(string.Format("{0}         {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffffff"), idWorker.NextId()));
            }

            Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffffff"));

            //SequentialGuidGenerator,效率低于SnowflakeIdWorker
            for (int i = 0; i < 1000; i++)
            {
                var id = SequentialGuidGenerator.NewSequentialGuid(SequentialGuidType.SequentialAsString);
                Trace.WriteLine(string.Format("{0}          {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffffff"), id));
            }

            Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffffff"));
        }
Esempio n. 7
0
        public void NextId_Duplicate_issues_2_Test()
        {
            var snowflakeIdWorker  = new SnowflakeIdWorker(1, 1);
            var blockingCollection = new BlockingCollection <long>();
            var tasks = new List <Task>();

            for (int i = 0; i < 100; i++)
            {
                var task = Task.Run(() =>
                {
                    for (int j = 0; j < 10000; j++)
                    {
                        blockingCollection.Add(snowflakeIdWorker.NextId());
                    }
                });

                tasks.Add(task);
            }

            Task.WaitAll(tasks.ToArray());

            Assert.AreEqual(1000000, blockingCollection.Distinct().Count());
        }
Esempio n. 8
0
 public SnowFlakeBenchmarks()
 {
     Worker = new SnowflakeIdWorker(0, 0);
 }
Esempio n. 9
0
 static DataSyncQueueManager()
 {
     _IdWorker   = new SnowflakeIdWorker(0, 0);
     _serializer = new ProtobufCacheSerializer();
     ConfigManager.ConfigReloaded += OnConfigReload;
 }