Пример #1
0
        static void Main(string[] args)
        {
            var serverCommunicationStatisticsState =
                FolderProcessingModule.TranslateDocumentsInFolderInParallel(
                    "c:\\inputFolder1",
                    "c:\\outputFolder1",
                    Location.A,
                    ServerCommunicationStatistics.Zero());

            Console.WriteLine(
                "Number of times communicated with server 1: "
                + serverCommunicationStatisticsState.NumberOfTimesCommunicatedWithServer1);

            Console.WriteLine(
                "Total time spent communicating with server 1: "
                + serverCommunicationStatisticsState.TotalTimeSpentCommunicatingWithServer1);

            Console.WriteLine(
                "Number of times communicated with server 2: "
                + serverCommunicationStatisticsState.NumberOfTimesCommunicatedWithServer2);

            Console.WriteLine(
                "Total time spent communicating with server 2: "
                + serverCommunicationStatisticsState.TotalTimeSpentCommunicatingWithServer2);
        }
        public static ServerCommunicationStatistics TranslateDocumentsInFolderInParallel(
            string folderPath,
            string destinationFolderPath,
            Location location,
            ServerCommunicationStatistics statisticsState)
        {
            IEnumerable <Document> documentsEnumerable = GetDocumentsFromFolder(folderPath);

            object lockingObject = new object();

            var state = statisticsState;

            Parallel.ForEach(
                documentsEnumerable,
                localInit: () => ServerCommunicationStatistics.Zero(),
                body: (document, loopState, localState) =>
            {
                var result = DocumentTranslationModule.TranslateDocument(
                    document, location, localState);

                WriteDocumentToDestinationFolder(result.document, destinationFolderPath);

                return(result.newState);
            },
                localFinally: (localSum) =>
            {
                lock (lockingObject)
                {
                    state = state.Combine(localSum);
                }
            });

            return(state);
        }
Пример #3
0
 public static ServerCommunicationStatistics Combine(this ServerCommunicationStatistics statistics1, ServerCommunicationStatistics statistics2)
 {
     return(new ServerCommunicationStatistics(
                numberOfTimesCommunicatedWithServer1: statistics1.NumberOfTimesCommunicatedWithServer1 + statistics2.NumberOfTimesCommunicatedWithServer1,
                numberOfTimesCommunicatedWithServer2: statistics1.NumberOfTimesCommunicatedWithServer2 + statistics2.NumberOfTimesCommunicatedWithServer2,
                totalTimeSpentCommunicatingWithServer1: statistics1.TotalTimeSpentCommunicatingWithServer1 + statistics2.TotalTimeSpentCommunicatingWithServer1,
                totalTimeSpentCommunicatingWithServer2: statistics1.TotalTimeSpentCommunicatingWithServer2 + statistics2.TotalTimeSpentCommunicatingWithServer2));
 }
Пример #4
0
        public static (Paragraph paragraph, ServerCommunicationStatistics newState) TranslateParagraph(
            Paragraph paragraph,
            Location location,
            ServerCommunicationStatistics statisticsState)
        {
            var result = TranslateText(paragraph.Text, location, statisticsState);

            return(new Paragraph(result.text, paragraph.Color), result.newState);
        }
Пример #5
0
        public static (Text text, ServerCommunicationStatistics newState) TranslateText(
            Text paragraphText,
            Location location,
            ServerCommunicationStatistics statisticsState)
        {
            var language = DetectLanguage(paragraphText);

            return(language == Language.German
                ? GermanTextTranslationModule.TranslateFromGerman(paragraphText, location, statisticsState)
                : SpanishTextTranslationModule.TranslateFromSpanish(paragraphText, location, statisticsState));
        }
        public static (Text text, ServerCommunicationStatistics newState) TranslateFromSpanish(
            Text text,
            Location location,
            ServerCommunicationStatistics statisticsState)
        {
            bool useServer1 = DateTime.Now.Millisecond < 500;

            if (useServer1)
            {
                Stopwatch stopwatch = Stopwatch.StartNew();

                var result = TranslateFromSpanishViaServer1(text, location);

                var elapsed = stopwatch.Elapsed;

                var newStatisticsState =
                    statisticsState
                    .WithTotalTimeSpentCommunicatingWithServer1(
                        statisticsState.TotalTimeSpentCommunicatingWithServer1 + elapsed)
                    .WithNumberOfTimesCommunicatedWithServer1(
                        statisticsState.NumberOfTimesCommunicatedWithServer1 + 1);

                return(result, newStatisticsState);
            }
            else
            {
                Stopwatch stopwatch2 = Stopwatch.StartNew();

                var resultFromServer2 = TranslateFromSpanishViaServer2(text, location);

                var elapsed2 = stopwatch2.Elapsed;

                var newStatisticsState2 =
                    statisticsState
                    .WithTotalTimeSpentCommunicatingWithServer2(
                        statisticsState.TotalTimeSpentCommunicatingWithServer2 + elapsed2)
                    .WithNumberOfTimesCommunicatedWithServer2(
                        statisticsState.NumberOfTimesCommunicatedWithServer2 + 1);

                return(resultFromServer2, newStatisticsState2);
            }
        }
Пример #7
0
        public static (Document document, ServerCommunicationStatistics newState) TranslateDocument(
            Document document,
            Location location,
            ServerCommunicationStatistics statisticsState)
        {
            var paragraphs = document.Paragraphs;

            List <Paragraph> translatedParagraphs = new List <Paragraph>();

            foreach (var paragraph in paragraphs)
            {
                var result = TranslateParagraph(paragraph, location, statisticsState);

                statisticsState = result.newState;

                translatedParagraphs.Add(result.paragraph);
            }

            return(new Document(document.Title, translatedParagraphs.ToImmutableArray()), statisticsState);
        }
Пример #8
0
 public static ServerCommunicationStatistics WithNumberOfTimesCommunicatedWithServer1(this ServerCommunicationStatistics instance, Int32 newValue)
 {
     return(new ServerCommunicationStatistics(numberOfTimesCommunicatedWithServer1: newValue, numberOfTimesCommunicatedWithServer2: instance.NumberOfTimesCommunicatedWithServer2, totalTimeSpentCommunicatingWithServer1: instance.TotalTimeSpentCommunicatingWithServer1, totalTimeSpentCommunicatingWithServer2: instance.TotalTimeSpentCommunicatingWithServer2));
 }