コード例 #1
0
ファイル: Program.cs プロジェクト: matan-nativ-sp/newrep
        static void Main(string[] args)
        {
            #region Constants

            string       PROCESS_ID   = Process.GetCurrentProcess().Id.ToString();
            const string COUNTER_NAME = "Event Generator";
            const string COUNTER_HELP = "Event Generator is used to monitor the number of processed events by the event generator.";

            #endregion

            #region Validate arguments count

            // Check number of arguments
            if (args.Length != 3)
            {
                Console.WriteLine("Usage: WBXEventGenerator.exe [filename] [events in send] [delay]\n");
                Console.WriteLine("\tfilename: text file with list of files that contain events");
                Console.WriteLine("\tevents in send: number of events to send in a single send operation");
                Console.WriteLine("\tdelay: number of milliseconds to wait between each event");
                return;
            }

            #endregion

            #region Initialize local variables

            String fileName = args[0];
            int    eventsInSend;
            int    delayTime;

            #endregion

            #region Validations

            // Check file existence
            if (!File.Exists(fileName))
            {
                Console.WriteLine("The file " + fileName + " does not exists\n");
                return;
            }

            // Check the number of events to send
            if (!int.TryParse(args[1], out eventsInSend))
            {
                Console.WriteLine("Invalid events in send number " + args[1]);
                return;
            }
            else
            {
                if (eventsInSend < 1)
                {
                    Console.WriteLine("Invalid events in send number " + args[1] + ", number must be at least 1");
                    return;
                }
            }

            // Check delay time
            if (!int.TryParse(args[2], out delayTime))
            {
                Console.WriteLine("Invalid delay number " + args[1]);
                return;
            }

            #endregion

            #region Create and open the event collector service

            _serviceClient = new EventCollectorServiceClient();

            try {
                _serviceClient.Open();
            }
            catch (Exception ex) {
                Console.Out.Write(
                    "Open Event Collector Service failed with exception: " + ex
                    );
                return;
            }

            #endregion

            #region Read the event files

            IList <string> eventFiles = new List <string>();

            #region Open the event files file

            FileStream fs =
                new FileStream(
                    fileName,
                    FileMode.Open,
                    FileAccess.Read
                    );

            StreamReader sr = new StreamReader(fs);

            #endregion

            #region Read the event file names

            // Start reading events
            String currLine = sr.ReadLine();

            // Read until all events are read
            while (currLine != null)
            {
                try {
                    eventFiles.Add(currLine);

                    // Read the next event
                    currLine = sr.ReadLine();
                }
                #region Exception handling
                catch (Exception ex) {
                    Console.WriteLine(
                        "Error processing the file which contains the event file names.\nError: {0}",
                        ex.Message
                        );
                }
                #endregion
            }

            #endregion

            _threadsCount = eventFiles.Count;

            #endregion

            #region Create performance counters

            // Delete the WhiteOPS category
            PerformanceMonitorHelper.deleteCategory(
                PerformanceMonitorHelper.WBX_CATEGORY_NAME
                );

            //string counterInstanceName = COUNTER_NAME + " " + PROCESS_ID;
            _counters = new Dictionary <string, PerformanceCounter>();

            foreach (string eventFilename in eventFiles)
            {
                _counters.Add(
                    eventFilename,
                    createCounter(
                        COUNTER_NAME,
                        COUNTER_HELP,
                        eventFilename
                        )
                    );
            }

            #endregion

            #region Create a data collector stream writer

            _streamWriters = new Dictionary <string, StreamWriter>();

            foreach (string eventFilename in eventFiles)
            {
                _streamWriters.Add(
                    eventFilename,
                    openFile(
                        eventFilename + "_results_"
                        )
                    );
            }

            #endregion

            ///////////////////////////////////////////////////////
            /// Before starting to handle the events, pause the
            /// process and wait for the user to to hit any key to
            /// continue. This is done is order to let the user
            /// create a new Performance Counter Log.
            ///////////////////////////////////////////////////////

            // Show the user the counter instance name
            Console.WriteLine("New performance counters were created with the file names read\n");
            Console.WriteLine("Press any key to process events...");
            Console.ReadKey(true);
            Console.WriteLine("\nProcessing events, please wait...\n");

            // Create a delegate to the method that will send
            // the events.
            _sendEventsFromFile sendEventsFromFileDelegate =
                new _sendEventsFromFile(sendEventsFromFile);

            // Create a new thread for each events file
            foreach (string eventFilename in eventFiles)
            {
                sendEventsFromFileDelegate.BeginInvoke(
                    eventFilename,
                    _counters[eventFilename],
                    _streamWriters[eventFilename],
                    eventsInSend,
                    delayTime,
                    null,
                    null
                    );
            }

            // Wait for all threads to finished.
            while (_numberOfThreadsFinished != _threadsCount)
            {
                Thread.Sleep(1000);
            }

            #region Remove the counter instance
            if (_counters != null)
            {
                System.Threading.Thread.Sleep(2000);
                Console.WriteLine("\nPress any key to terminate performance instance counter...");
                Console.ReadKey(true);

                foreach (PerformanceCounter counter in _counters.Values)
                {
                    counter.RemoveInstance();
                }

                foreach (StreamWriter sw in _streamWriters.Values)
                {
                    sw.Close();
                }
            }
            #endregion

            #region Display finish message

            Console.WriteLine("Process finished.");

            #endregion
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: matan-nativ-sp/newrep
        static void Main(string[] args)
        {
            #region Constants

            string PROCESS_ID = Process.GetCurrentProcess().Id.ToString();
            const string COUNTER_NAME = "Event Generator";
            const string COUNTER_HELP = "Event Generator is used to monitor the number of processed events by the event generator.";

            #endregion

            #region Validate arguments count

            // Check number of arguments
            if (args.Length != 3) {
                Console.WriteLine("Usage: WBXEventGenerator.exe [filename] [events in send] [delay]\n");
                Console.WriteLine("\tfilename: text file with list of files that contain events");
                Console.WriteLine("\tevents in send: number of events to send in a single send operation");
                Console.WriteLine("\tdelay: number of milliseconds to wait between each event");
                return;
            }

            #endregion

            #region Initialize local variables

            String fileName = args[0];
            int eventsInSend;
            int delayTime;

            #endregion

            #region Validations

            // Check file existence
            if (!File.Exists(fileName)) {
                Console.WriteLine("The file " + fileName + " does not exists\n");
                return;
            }

            // Check the number of events to send
            if (!int.TryParse(args[1], out eventsInSend)) {
                Console.WriteLine("Invalid events in send number " + args[1]);
                return;
            }
            else {
                if (eventsInSend < 1) {
                    Console.WriteLine("Invalid events in send number " + args[1] +", number must be at least 1");
                    return;
                }
            }

            // Check delay time
            if (!int.TryParse(args[2], out delayTime)) {
                Console.WriteLine("Invalid delay number " + args[1]);
                return;
            }

            #endregion

            #region Create and open the event collector service

            _serviceClient = new EventCollectorServiceClient();

            try {
                _serviceClient.Open();
            }
            catch (Exception ex){
                Console.Out.Write(
                    "Open Event Collector Service failed with exception: " + ex
                );
                return;
            }

            #endregion

            #region Read the event files

            IList<string> eventFiles = new List<string>();

            #region Open the event files file

            FileStream fs =
                new FileStream(
                    fileName,
                    FileMode.Open,
                    FileAccess.Read
                );

            StreamReader sr = new StreamReader(fs);

            #endregion

            #region Read the event file names

            // Start reading events
            String currLine = sr.ReadLine();

            // Read until all events are read
            while (currLine != null) {
                try {

                    eventFiles.Add(currLine);

                    // Read the next event
                    currLine = sr.ReadLine();
                }
                #region Exception handling
                catch (Exception ex) {
                    Console.WriteLine(
                        "Error processing the file which contains the event file names.\nError: {0}",
                        ex.Message
                    );
                }
                #endregion
            }

            #endregion

            _threadsCount = eventFiles.Count;

            #endregion

            #region Create performance counters

            // Delete the WhiteOPS category
            PerformanceMonitorHelper.deleteCategory(
                PerformanceMonitorHelper.WBX_CATEGORY_NAME
            );

            //string counterInstanceName = COUNTER_NAME + " " + PROCESS_ID;
            _counters = new Dictionary<string,PerformanceCounter>();

            foreach (string eventFilename in eventFiles) {

                _counters.Add(
                    eventFilename,
                    createCounter(
                        COUNTER_NAME,
                        COUNTER_HELP,
                        eventFilename
                    )
                );
            }

            #endregion

            #region Create a data collector stream writer

            _streamWriters = new Dictionary<string, StreamWriter>();

            foreach (string eventFilename in eventFiles) {

                _streamWriters.Add(
                    eventFilename,
                    openFile(
                        eventFilename + "_results_"
                    )
                );
            }

            #endregion

            ///////////////////////////////////////////////////////
            /// Before starting to handle the events, pause the
            /// process and wait for the user to to hit any key to
            /// continue. This is done is order to let the user
            /// create a new Performance Counter Log.
            ///////////////////////////////////////////////////////

            // Show the user the counter instance name
            Console.WriteLine("New performance counters were created with the file names read\n");
            Console.WriteLine("Press any key to process events...");
            Console.ReadKey(true);
            Console.WriteLine("\nProcessing events, please wait...\n");

            // Create a delegate to the method that will send
            // the events.
            _sendEventsFromFile sendEventsFromFileDelegate =
                new _sendEventsFromFile(sendEventsFromFile);

            // Create a new thread for each events file
            foreach (string eventFilename in eventFiles) {

                sendEventsFromFileDelegate.BeginInvoke(
                    eventFilename,
                    _counters[eventFilename],
                    _streamWriters[eventFilename],
                    eventsInSend,
                    delayTime,
                    null,
                    null
                );

            }

            // Wait for all threads to finished.
            while (_numberOfThreadsFinished != _threadsCount) {
                Thread.Sleep(1000);
            }

            #region Remove the counter instance
            if (_counters != null) {

                System.Threading.Thread.Sleep(2000);
                Console.WriteLine("\nPress any key to terminate performance instance counter...");
                Console.ReadKey(true);

                foreach (PerformanceCounter counter in _counters.Values) {
                    counter.RemoveInstance();
                }

                foreach (StreamWriter sw in _streamWriters.Values) {
                    sw.Close();
                }
            }
            #endregion

            #region Display finish message

            Console.WriteLine("Process finished.");

            #endregion
        }