示例#1
0
        void NotificationTimer(ref long timerIntervalInMicroSec,
                               ref long ignoreEventIfLateBy,
                               ref bool stopTimer)
        {
            int  timerCount       = 0;
            long nextNotification = 0;

            MicroStopwatch microStopwatch = new MicroStopwatch();

            microStopwatch.Start();

            while (!stopTimer)
            {
                long callbackFunctionExecutionTime =
                    microStopwatch.ElapsedMicroseconds - nextNotification;

                long timerIntervalInMicroSecCurrent =
                    System.Threading.Interlocked.Read(ref timerIntervalInMicroSec);
                long ignoreEventIfLateByCurrent =
                    System.Threading.Interlocked.Read(ref ignoreEventIfLateBy);

                nextNotification += timerIntervalInMicroSecCurrent;
                timerCount++;
                long elapsedMicroseconds = 0;

                // https://www.codeproject.com/Articles/98346/Microsecond-and-Millisecond-NET-Timer?msg=5288003#xx5288003xx
                //while ( (elapsedMicroseconds = microStopwatch.ElapsedMicroseconds)
                //        < nextNotification)
                //{
                //    System.Threading.Thread.SpinWait(10);
                //}
                int wms = (int)timerIntervalInMicroSec / 1000;
                wms = wms - (int)(wms * 0.01);
                _threadTimer.Join(wms);
                while ((elapsedMicroseconds = microStopwatch.ElapsedMicroseconds) < nextNotification)
                {
                    System.Threading.Thread.SpinWait(10);
                }


                long timerLateBy = elapsedMicroseconds - nextNotification;

                if (timerLateBy >= ignoreEventIfLateByCurrent)
                {
                    continue;
                }

                MicroTimerEventArgs microTimerEventArgs =
                    new MicroTimerEventArgs(timerCount,
                                            elapsedMicroseconds,
                                            timerLateBy,
                                            callbackFunctionExecutionTime);
                MicroTimerElapsed(this, microTimerEventArgs);
            }

            microStopwatch.Stop();
        }
        void NotificationTimer(ref long timerIntervalInMicroSec,
                               ref long ignoreEventIfLateBy,
                               ref bool stopTimer)
        {
            int  timerCount       = 0;
            long nextNotification = 0;

            MicroStopwatch microStopwatch = new MicroStopwatch();

            microStopwatch.Start();

            while (!stopTimer)
            {
                long callbackFunctionExecutionTime =
                    microStopwatch.ElapsedMicroseconds - nextNotification;

                long timerIntervalInMicroSecCurrent =
                    System.Threading.Interlocked.Read(ref timerIntervalInMicroSec);
                long ignoreEventIfLateByCurrent =
                    System.Threading.Interlocked.Read(ref ignoreEventIfLateBy);

                nextNotification += timerIntervalInMicroSecCurrent;
                timerCount++;
                long elapsedMicroseconds = 0;

                while ((elapsedMicroseconds = microStopwatch.ElapsedMicroseconds)
                       < nextNotification)
                {
                    //System.Threading.Thread.SpinWait(10);
                    System.Threading.Thread.Sleep(1);
                }

                long timerLateBy = elapsedMicroseconds - nextNotification;

                //if (timerLateBy >= ignoreEventIfLateByCurrent)
                //{
                //    nextNotification += timerIntervalInMicroSecCurrent;
                //}
                //else
                {
                    MicroTimerEventArgs microTimerEventArgs =
                        new MicroTimerEventArgs(timerCount,
                                                elapsedMicroseconds,
                                                timerLateBy,
                                                callbackFunctionExecutionTime);
                    MicroTimerElapsed(this, microTimerEventArgs);
                }
            }

            microStopwatch.Stop();
        }
        void NotificationTimer(long timerInterval,
                               long ignoreEventIfLateBy,
                               ref bool stopTimer)
        {
            int  timerCount       = 0;
            long nextNotification = 0;

            MicroStopwatch microStopwatch = new MicroStopwatch();

            microStopwatch.Start();

            while (!stopTimer)
            {
                long callbackFunctionExecutionTime =
                    microStopwatch.ElapsedMicroseconds - nextNotification;
                nextNotification += timerInterval;
                timerCount++;
                long elapsedMicroseconds = 0;

                while ((elapsedMicroseconds = microStopwatch.ElapsedMicroseconds)
                       < nextNotification)
                {
                    System.Threading.Thread.SpinWait(10);
                }

                long timerLateBy = elapsedMicroseconds - (timerCount * timerInterval);

                if (timerLateBy >= ignoreEventIfLateBy)
                {
                    continue;
                }

                MicroTimerEventArgs microTimerEventArgs =
                    new MicroTimerEventArgs(timerCount,
                                            elapsedMicroseconds,
                                            timerLateBy,
                                            callbackFunctionExecutionTime);
                MicroTimerElapsed(this, microTimerEventArgs);
            }

            microStopwatch.Stop();
        }
示例#4
0
        void NotificationTimer(ref long timerIntervalInMicroSec,
                               ref long ignoreEventIfLateBy,
                               ref bool stopTimer)
        {
            int  timerCount = 0;
            long nextNotification = 0;

            MicroStopwatch microStopwatch = new MicroStopwatch();
            microStopwatch.Start();

            while (!stopTimer)
            {
                long callbackFunctionExecutionTime =
                    microStopwatch.ElapsedMicroseconds - nextNotification;

                long timerIntervalInMicroSecCurrent =
                    System.Threading.Interlocked.Read(ref timerIntervalInMicroSec);
                long ignoreEventIfLateByCurrent =
                    System.Threading.Interlocked.Read(ref ignoreEventIfLateBy);

                nextNotification += timerIntervalInMicroSecCurrent;
                timerCount++;
                long elapsedMicroseconds = 0;

                while ( (elapsedMicroseconds = microStopwatch.ElapsedMicroseconds)
                        < nextNotification)
                {
                    System.Threading.Thread.SpinWait(10);
                }

                long timerLateBy = elapsedMicroseconds - nextNotification;

                if (timerLateBy >= ignoreEventIfLateByCurrent)
                {
                    continue;
                }

                MicroTimerEventArgs microTimerEventArgs =
                     new MicroTimerEventArgs(timerCount,
                                             elapsedMicroseconds,
                                             timerLateBy,
                                             callbackFunctionExecutionTime);
                MicroTimerElapsed(this, microTimerEventArgs);
            }

            microStopwatch.Stop();
        }
示例#5
0
    static StreamWriter logWriter = new StreamWriter(logFile);                                                              // The logging stream

    /*
     * Program entry point
     */
    static void Main(string[] args)
    {
        logWriter.AutoFlush = true;

        // Get access to sql
        string        connectionString = "Data Source=tcp:172.30.100.116;\n;Initial Catalog=GlobalDeviceService;\nUser ID=octopus;Password=octopus";
        SqlConnection cnn = new SqlConnection(connectionString);

        cnn.Open();

        // Load the queries
        string[] graphqlQueries = get_queries(numGraphQLQueries, GraphQLFolder);
        string[] sqlQueries     = get_queries(numSQLQueries, SQLFolder);

        // Process and time the queries for Graphql
        do_logging_writeline("Beginning GraphQL Tests\n");
        for (int i = 0; i < numGraphQLQueries && !String.IsNullOrEmpty(graphqlQueries[i]); i++)
        {
            // Do logging, start timer
            string curQuery = graphqlQueries[i];
            do_logging_writeline("Current Query: ");
            do_logging_write(curQuery + "\n\n");
            MicroLibrary.MicroStopwatch timer = new MicroLibrary.MicroStopwatch();

            // Get the result into the cache
            do_graphql_query_caching(curQuery).GetAwaiter().GetResult();

            // Time the query (cached) and sum the execution times
            long cachedTime = 0;
            for (int j = 0; j < numExecutions; j++)
            {
                // Time the call and aggregate
                timer.Restart();
                do_graphql_query_caching(curQuery).GetAwaiter().GetResult();
                timer.Stop();
                cachedTime += timer.ElapsedMicroseconds;
            }

            // Cleanup
            db.KeyDelete(curQuery);

            // Time the query (uncached) and sum the execution times
            long uncachedTime = 0;
            for (int j = 0; j < numExecutions; j++)
            {
                // Time the call and aggregate
                timer.Restart();
                do_graphql_query_no_caching(curQuery).GetAwaiter().GetResult();
                timer.Stop();
                uncachedTime += timer.ElapsedMicroseconds;
            }

            do_logging_writeline("Uncached execution time (Average of " + numExecutions.ToString() + " runs): " + (uncachedTime / numExecutions).ToString() + "µs");
            do_logging_writeline("Cached execution time (Average of " + numExecutions.ToString() + " runs): " + (cachedTime / numExecutions).ToString() + "µs");
            do_logging_writeline(((double)(uncachedTime / numExecutions) / (double)(cachedTime / numExecutions)).ToString() + " times faster.\n");
            //do_logging_writeline("Size of result set: " + result.Length.ToString() + "\n");
        }

        // Process and time the queries for SQL Server
        do_logging_writeline("\nBeginning SQL Tests\n");
        for (int i = 0; i < numSQLQueries && !String.IsNullOrEmpty(sqlQueries[i]); i++)
        {
            //Do logging, start timer
            string curQuery = sqlQueries[i];
            do_logging_writeline("Current Query: ");
            do_logging_write(curQuery + "\n\n");
            MicroLibrary.MicroStopwatch timer = new MicroLibrary.MicroStopwatch();

            // Get the result into the cache
            do_sql_query_caching(curQuery, cnn).GetAwaiter().GetResult();

            // Time the query (cached) and sum the execution times
            long cachedTime = 0;
            for (int j = 0; j < numExecutions; j++)
            {
                // Time the call and aggregate
                timer.Restart();
                do_sql_query_caching(curQuery, cnn).GetAwaiter().GetResult();
                timer.Stop();
                cachedTime += timer.ElapsedMicroseconds;
            }

            // Cleanup
            db.KeyDelete(curQuery);

            // Time the query (uncached) and sum the execution times
            long uncachedTime = 0;
            for (int j = 0; j < numExecutions; j++)
            {
                // Time the call and aggregate
                timer.Restart();
                do_sql_query_no_caching(curQuery, cnn).GetAwaiter().GetResult();
                timer.Stop();
                uncachedTime += timer.ElapsedMicroseconds;
            }

            do_logging_writeline("Uncached execution time (Average of " + numExecutions.ToString() + " runs): " + (uncachedTime / numExecutions).ToString() + "µs");
            do_logging_writeline("Cached execution time (Average of " + numExecutions.ToString() + " runs): " + (cachedTime / numExecutions).ToString() + "µs");
            do_logging_writeline(((double)(uncachedTime / numExecutions) / (double)(cachedTime / numExecutions)).ToString() + " times faster.\n");
            //do_logging_writeline("Size of result set: " + result.Length.ToString() + "\n");
        }

        // cleanup
        cnn.Close();
    }