Пример #1
0
        //Waits for a message and makes new thread to handle when received
        static unsafe void Main(string[] args)
        {
            Writer = new BlockWriter(GetPrevHeader());
            GPUWrapper.main(); //Initialize GPU
            //Initialize 100 transactions
            int index           = 0;
            int testnum         = 10;
            int finalblockcount = testnum / 10;
            //int finalblockcount = testnum;
            List <SafeMessage> msgsjson = JsonConvert.DeserializeObject <List <SafeMessage> >(File.ReadAllText("messages.json"));
            bool      printed           = false;
            Stopwatch sw = new Stopwatch();

            sw.Start();
            for (int i = 0; i < testnum; i++)
            {
                SafeMessage message = msgsjson.ElementAt(index);
                if (message.ConfigSeq != null) //If message exists
                {
                    if (useGPU)
                    {
                        Console.WriteLine(String.Format("Test {0}, HandleMessage posted", i));
                        Message unsafemessage = ConvertToUnsafe(message);
                        int     temp          = i;                                                               //Concurrency issue happens if I don't do this
                        Task    t             = Task.Factory.StartNew(() => HandleMessage(unsafemessage, temp)); //Run message handler
                        t.Wait();
                    }
                    else //Use CPU
                    {
                        Console.WriteLine(String.Format("Test {0}, HandleMessageCPU posted", i));
                        int temp = i;
                        Task.Factory.StartNew(() => HandleMessageCPU(message, temp)); //Run message handler
                    }
                }
                else if (Timer != new DateTime() && (DateTime.Now - Timer).TotalSeconds > Timeout)
                {
                    Console.WriteLine(String.Format("Test {0}, HandleTimer posted", i));
                    Timer = new DateTime(); //Reset timer
                    Task.Factory.StartNew(() => HandleTimer());
                }
                index = (index + 1) % 500;
            }

            while (true)
            {
                if (BlockCount >= (finalblockcount) && !printed)
                {
                    sw.Stop();
                    Console.WriteLine("Time to process " + testnum + " transactions: " + sw.ElapsedMilliseconds + " ms");
                    Console.WriteLine("Transactions per second: " + (1000 * (double)testnum / sw.ElapsedMilliseconds) + " tps");
                    //Console.WriteLine("Avg time to order (no batch returned): " + (OrderTime / (double)(testnum - finalblockcount)) + " ms");
                    //Console.WriteLine("Avg time to fully handle msg: " + (TotalHandleTime / (double)testnum) + " ms"); //total handle time is consistently .01ms more than avg order time
                    //Console.WriteLine("Avg time to order (batch returned): " + (OrderTimeCut / (double)finalblockcount) + " ms");
                    printed = true;
                    //Environment.Exit(0);
                }
            }
        }