Ejemplo n.º 1
0
        private static void MeasurePerformance(string caption, ThreadLocalFactory threadLocalFactory)
        {
            for (int cycle = 0; cycle < TestCycles; cycle++)
            {
                Console.Out.Write("{0,-20} ... ", caption);
                Console.Out.Flush();

                var threadDispatchQueue = threadLocalFactory.CreateThreadLocal(
                    () => new Queue <Runnable>());

                // Check access time - when not set
                long timeA = PerformanceObserver.MicroTime;
                for (int ii = 0; ii < TestIterations; ii++)
                {
                    var value = threadDispatchQueue.Value;
                }

                // Check access time - with get or create
                long timeB = PerformanceObserver.MicroTime;
                for (int ii = 0; ii < TestIterations; ii++)
                {
                    threadDispatchQueue.GetOrCreate();
                }

                long timeC = PerformanceObserver.MicroTime;
                Console.WriteLine("{0} -> {1} us -> {2} us", timeA, timeB - timeA, timeC - timeB);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes the <see cref="ThreadLocalManager"/> class.
        /// </summary>
        static ThreadLocalManager()
        {
            // Establishes the default thread local style
            var defaultThreadLocalType = CompatSettings.Default.DefaultThreadLocalType;

            if (String.IsNullOrEmpty(defaultThreadLocalType))
            {
                DefaultThreadLocalFactory = new FastThreadLocalFactory();
                return;
            }

            switch (defaultThreadLocalType.ToUpper())
            {
            case "FAST":
                DefaultThreadLocalFactory = new FastThreadLocalFactory();
                break;

            case "SLIM":
                DefaultThreadLocalFactory = new SlimThreadLocalFactory();
                break;

            case "XPER":
                DefaultThreadLocalFactory = new XperThreadLocalFactory();
                break;

            case "SYSTEM":
                DefaultThreadLocalFactory = new SystemThreadLocalFactory();
                break;

            default:
                throw new ArgumentException("unknown thread local type '" + defaultThreadLocalType + "'");
            }
        }