示例#1
0
        static int Main(string[] args)
        {
            PrintHeader();
            if (args.Length == 0)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Please enter the file you want to process.");
                Console.WriteLine("Usage: CountingWordsConsole <filename>");
                Console.WriteLine(@"");
                Console.ResetColor();
                return 1;
            }
            StateTimeClass StateObj = new StateTimeClass();
            StateObj.Canceled = false;
            StateObj.handler = new PerformanceHandler();
            StateObj.Value = 1;
            System.Threading.TimerCallback TimerDelegate = new System.Threading.TimerCallback(TimerTask);

            // Create a timer that calls a procedure every 200ms
            // Note: There is no Start method; the timer starts running as soon as
            // the instance is created.
            System.Threading.Timer TimerItem = new System.Threading.Timer(TimerDelegate, StateObj, 100, 100);

            // Save a reference for Dispose.
            StateObj.Reference = TimerItem;

            Reducer reducer = new Reducer();
            try
            {
                SystemDetails.ShowCPUDetails();
                Stopwatch sw = new Stopwatch();
                string readText = File.ReadAllText(args[0]);
                Console.WriteLine("Starting reduction");
                sw.Start();
                reducer.MapReduce(readText);
                sw.Stop();
                Console.WriteLine("Reduction completed");
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Elapsed={0}", sw.Elapsed);
                File.WriteAllText("Results.txt", reducer.SortedResults().ToString());
                Console.WriteLine("Done!, processing {0:D} words", reducer.Numwords.ToString("N", CultureInfo.InvariantCulture));
                Console.WriteLine("Please review Results.txt");
                Console.ResetColor();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            // Request Dispose of the timer object.
            StateObj.Canceled = true;

            return 0;
        }
示例#2
0
 public void TestMapReduce()
 {
     Reducer reducer = new Reducer();
     try
     {
         SystemDetails.ShowCPUDetails();
         Stopwatch sw = new Stopwatch();
         string readText = @"word word word word word";
         Console.WriteLine("Starting reduction");
         sw.Start();
         reducer.MapReduce(readText);
         sw.Stop();
         Assert.AreEqual(reducer.SortedResults().ToString(), "word: 5\r\n");
         Assert.AreEqual(reducer.Numwords, 5);
         Assert.Less(sw.Elapsed.Milliseconds, 2000);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }