Exemplo n.º 1
0
        internal static void SingleThreadAccess()
        {
            var tree = new ConcurrentTree<string>();

            var root = tree.Root;
            root.AddChild(tree.NewTreeNode("0", "testContent"));

            var node = tree.FindByPath("");
            Console.WriteLine("Founded '{0}'", node.Name);

            node = tree.FindByPath(String.Format("{0}{1}", TreeNode.PathSeparator, "0"));
            Thread.Sleep(1);
            Console.WriteLine("Founded {0}", node.Name);

            node.AddChild(tree.NewTreeNode("0", "testContent"));
            node.AddChild(tree.NewTreeNode("1", "testContent"));
            node.AddChild(tree.NewTreeNode("2", "testContent"));

            node = tree.FindByPath(String.Format("{0}{1}{0}{2}", TreeNode.PathSeparator, "0", "2"));
            Console.WriteLine("Founded {0}", node.Name);

            node.AddChild(tree.NewTreeNode("0", "testContent"));

            node = tree.FindByPath(String.Format("{0}{1}{0}{2}{0}{3}", TreeNode.PathSeparator, "0", "2", "0"));
            Console.WriteLine("Founded {0}", node.Name);

            tree.Dispose();
        }
Exemplo n.º 2
0
        internal static void MultiThreadAccess(string templatePath, int threadsCount)
        {
            if (!Directory.Exists(templatePath))
            {
                Console.WriteLine(templatePath + " does not exist, please choose an existing directory!");
            }

            _counter = 0;
            var tree = new ConcurrentTree<string>();
            var rootDir = new DirectoryInfo(templatePath);
            DirectoryInfo[] dirs = rootDir.GetDirectories("*", SearchOption.AllDirectories);
            threadsCount = Math.Min(dirs.Length, threadsCount);

            var threads = new Thread[Math.Min(dirs.Length, threadsCount)];
            var ticksElapsed = new long[Math.Min(dirs.Length, threadsCount)];

            for (int i = 0; i < threadsCount; i++)
            {
                tree.Root.AddChild(tree.NewTreeNode(dirs[i].Name, String.Empty));
                threads[i] = new Thread(ReplicateDir);
            }
            var sw = new Stopwatch();
            sw.Start();
            for (int i = 0; i < threadsCount; i++)
            {
                threads[i].Start(new ThreadParameter
                    {
                        Info = dirs[i],
                        RootPath = TreeNode.PathSeparator + dirs[i].Name,
                        Tree = tree,
                        Id = i,
                        Threads = threads,
                        TicksElapsed = ticksElapsed
                    });
            }

            long ticksTotal;
            long elements;
            bool continueAll = true;
            while (continueAll)
            {
                ticksTotal = 0;
                for (int i = 0; i < threadsCount; i++)
                {
                    var elap = Interlocked.Read(ref ticksElapsed[i]);
                    ticksTotal += elap;
                }
                elements = Interlocked.Read(ref _counter);
                Console.WriteLine("Elaborated {0} files/directories in {1} ms. Total latency: {2} ms.",
                                  elements,
                                  sw.ElapsedMilliseconds,
                                  elements > 0 ? (int)( (ticksTotal / threadsCount)/TimeSpan.TicksPerMillisecond) : 0);

                Thread.Sleep(1000);
                continueAll = false;
                foreach (var th in threads)
                {
                    if (th != null) continueAll = true;
                }
            }
            sw.Stop();

            ticksTotal = 0;
            elements = Interlocked.Read(ref _counter);
            for (int i = 0; i < threadsCount; i++)
            {
                ticksTotal += Interlocked.Read(ref ticksElapsed[i]);
            }
            Console.WriteLine("Elaborated {0} files/directories in {1} ms. Average latency: {2} ms.",
                              elements,
                              sw.ElapsedMilliseconds,
                              elements > 0 ? (int)((ticksTotal / threadsCount)/TimeSpan.TicksPerMillisecond) : 0);
            Console.WriteLine("Done!");
        }