示例#1
0
        public static int Main(string[] args)
        {
            string     patterns     = null;
            string     logFiles     = null;
            string     outputPath   = null;
            OutputType outputChoice = 0;

            List <string> helps = new List <string>(
                new string[] { "--help", "-h", "/?", "/HELP", "-help" });

            foreach (string arg in args)
            {
                if (helps.Contains(arg))
                {
                    ShowHelp();
                    return(1);
                }
            }

            switch (args.Length)
            {
            case 1:
                // Valid case:
                // plot_complus.exe log_file.txt
                logFiles     = args[0];
                outputChoice = OutputType.Interactive;
                break;

            case 2:
                // Valid case:
                // plot_complus.exe log_file.txt process_filter
                logFiles     = args[0];
                patterns     = args[1];
                outputChoice = OutputType.Interactive;
                break;

            case 3:
                // Valid cases:
                // plot_complus.exe log_file.txt -o out_graph.png
                // plot_complus.exe log_file.txt -a out_dir
                if (args[1] == "-o" || args[1] == "-a")
                {
                    logFiles     = args[0];
                    outputChoice = (args[1] == "-o" ?
                                    OutputType.File : OutputType.Directory);
                    outputPath = args[2];
                    break;
                }
                else
                {
                    Console.WriteLine("Invalid arguments {0}!", args[1]);
                    return(1);
                }

            case 4:
                // Valid case:
                // plot_complus.exe log_file.txt process_filter -o out_graph.png
                if (args[2] == "-o")
                {
                    logFiles     = args[0];
                    patterns     = args[1];
                    outputChoice = OutputType.File;
                    outputPath   = args[3];

                    break;
                }
                else
                {
                    Console.WriteLine("Invalid arguments {0}!", args[2]);
                    return(1);
                }

            default:
                Console.WriteLine("Wrong number of input arguments!");
                return(1);
            }


            IProcessFilter processFilter;

            if (patterns != null)
            {
                processFilter = new ProcessFilter(patterns);
            }
            else
            {
                processFilter = new AllProcesses();
            }

            ILogger      logger       = new ConsoleLogger();
            SeriesFilter seriesFilter = new SeriesFilter();

            List <string[]> tupleList = Settings.DesiredSeries(
                ConfigurationManager.AppSettings);

            foreach (string[] seriesTuple in tupleList)
            {
                seriesFilter.AddDesiredSeries(seriesTuple[0], seriesTuple[1]);
            }

            GraphPlotter plotter = new GraphPlotter(seriesFilter);

            LogParser      parser    = new LogParser(processFilter, seriesFilter, logger);
            List <Process> processes = parser.ParseLogs(logFiles);

            switch (outputChoice)
            {
            case OutputType.Directory:
                outputPath = Path.GetFullPath(outputPath);

                if (!Directory.Exists(outputPath))
                {
                    Directory.CreateDirectory(outputPath);
                }

                Dictionary <string, List <Process> > processGroup =
                    new Dictionary <string, List <Process> >();

                foreach (Process p in processes)
                {
                    if (!processGroup.ContainsKey(p.ProcessName))
                    {
                        processGroup[p.ProcessName] = new List <Process>();
                    }
                    processGroup[p.ProcessName].Add(p);
                }

                foreach (KeyValuePair <string, List <Process> > pair in processGroup)
                {
                    string path = Path.Combine(outputPath, pair.Key + DEFAULT_EXT);
                    plotter.WriteToFile(pair.Value, path);
                }
                break;

            case OutputType.File:
                outputPath = Path.GetFullPath(outputPath);
                string ext = Path.GetExtension(outputPath).ToLower();

                if (ext.Length > 0)
                {
                    if (!VALID_EXTS.Contains(ext))
                    {
                        Console.WriteLine("The graph cannot be saved in {0}!" +
                                          "  Will save it in {1} instead.", ext, DEFAULT_EXT);
                        outputPath = Path.GetFileNameWithoutExtension(outputPath)
                                     + DEFAULT_EXT;
                    }
                }
                else
                {
                    Console.WriteLine("The graph will be saved in {0}.", DEFAULT_EXT);
                    outputPath = Path.GetFileNameWithoutExtension(outputPath)
                                 + DEFAULT_EXT;
                }

                plotter.WriteToFile(processes, outputPath);
                break;

            case OutputType.Interactive:
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1(plotter, processes));
                break;
            }

            return(0);
        }
示例#2
0
        public void SmokeTest()
        {
            // Instantiating the objects.
            ILogger logger = new ConsoleLogger();

            SeriesFilter sf = new SeriesFilter();

            sf.AddDesiredSeries("%ProcessorTime",
                                "PlotComPlus.Series.DefaultSeries");
            sf.AddDesiredSeries("PrivateBytes",
                                "PlotComPlus.Series.MemorySeries");

            IProcessFilter pf = new ProcessFilter("session,userinfo");

            LogParser parser = new LogParser(pf, sf, logger);

            // Setting up the test log file.
            string path = Path.GetTempFileName();

            string[] content = new string[] {
                "Microsoft (R) Windows Script Host Version 5.6",
                "Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.",
                "",
                "Time,CN,PN(ID),%ProcessorTime,%UT,TC,PrivateBytes,WS",
                "4/3/2007 10:00:37 AM,.,Idle(0),100,0,8,0,16384",
                "4/3/2007 10:00:37 AM,.,System(4),0,0,110,28672,28672",
                "4/3/2007 10:00:37 AM,.,RBCWSSession(6520),23,0,31,30208000,37974016",
                ",,,,,,,",
                "4/3/2007 10:00:42 AM,.,RBCWSSession(6520),24,0,31,25071616,34078720",
                "4/3/2007 10:00:42 AM,.,RBCWSUserInfo(10496),11,0,32,13414400,21475328",
                "4/4/2007,.,RBCWSSession(6520),25,0,31,25214976,34222080"
            };
            File.WriteAllLines(path, content);

            // Now we run the smoke test of parsing 1 log file containing
            // 2 processes (RBCWSSession, RBCWSUserInfo) we are interested,
            // with the 2 metrics above that we are interested in.
            List <Process> processes = parser.ParseLogs(path);

            Assert.AreEqual(2, processes.Count);

            Process session = processes[0];

            Assert.AreEqual("RBCWSSession", session.ProcessName);
            Assert.AreEqual(3, session.TimeSeries.Count);

            IList <double> data = session.Get("%ProcessorTime");

            Assert.AreEqual(3, data.Count);
            Assert.AreEqual(true, data.Contains(23f));
            Assert.AreEqual(true, data.Contains(24f));
            Assert.AreEqual(true, data.Contains(25f));

            data = session.Get("PrivateBytes");
            Assert.AreEqual(3, data.Count);
            Assert.AreEqual(true, data.Contains(30.208d));
            Assert.AreEqual(true, data.Contains(25.071616d));
            Assert.AreEqual(true, data.Contains(25.214976d));

            Process userinfo = processes[1];

            Assert.AreEqual("RBCWSUserInfo", userinfo.ProcessName);
            Assert.AreEqual(1, userinfo.TimeSeries.Count);

            data = userinfo.Get("%ProcessorTime");
            Assert.AreEqual(1, data.Count);
            Assert.AreEqual(true, data.Contains(11d));

            data = userinfo.Get("PrivateBytes");
            Assert.AreEqual(1, data.Count);
            Assert.AreEqual(true, data.Contains(13.4144d));

            // Clean up.
            File.Delete(path);
        }