Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- namespace _13_LoadTimeCalculator
- {
- class LoadTimeCalculator
- {
- static void Main()
- {
- string logData = @"2014-Apr-01 02:01 http://softuni.bg 8.37725
- 2014-Apr-01 02:05 http://www.nakov.com 11.622
- 2014-Apr-01 02:06 http://softuni.bg 4.33
- 2014-Apr-01 02:11 http://www.google.com 1.94
- 2014-Apr-01 02:11 http://www.google.com 2.011
- 2014-Apr-01 02:12 http://www.google.com 4.882
- 2014-Apr-01 02:34 http://softuni.bg 4.885
- 2014-Apr-01 02:36 http://www.nakov.com 10.74
- 2014-Apr-01 02:36 http://www.nakov.com 11.75
- 2014-Apr-01 02:38 http://softuni.bg 3.886
- 2014-Apr-01 02:44 http://www.google.com 1.04
- 2014-Apr-01 02:48 http://www.google.com 1.4555
- 2014-Apr-01 02:55 http://www.google.com 1.977
- ";
- string logLine = null;
- StringReader strReader = new StringReader(logData);
- List<LogObject> log = new List<LogObject>();
- bool loop = true;
- while(loop)
- {
- logLine = strReader.ReadLine();
- if (logLine != null)
- {
- //check if the logLine is empty
- if (!string.IsNullOrWhiteSpace(logLine))
- {
- string[] logLineParsed = logLine.Trim().Split(' ');
- string url = logLineParsed[2];
- double loadTime = double.Parse(logLineParsed[3], CultureInfo.CreateSpecificCulture("en-GB"));
- bool existsInLog = false;
- int logID = 0;
- for (int i = 0; i < log.Count; i++)
- {
- if (log[i].url == url)
- {
- existsInLog = true;
- logID = i;
- break;
- }
- }
- //if the url exist in the log
- if (existsInLog)
- {
- double avTime = (log[logID].loadTime * log[logID].count + loadTime) / (log[logID].count + 1);
- log[logID].loadTime = avTime;
- log[logID].count = log[logID].count + 1;
- }
- else
- {
- LogObject data = new LogObject();
- data.url = url;
- data.loadTime = loadTime;
- data.count = 1;
- log.Add(data);
- }
- }
- }
- else
- {
- //exit from loop
- loop = false;
- }
- }
- PrintResult(log);
- }
- static void PrintResult(List<LogObject> log)
- {
- for (int i = 0; i < log.Count; i++)
- {
- Console.WriteLine("{0} -> {1}", log[i].url, log[i].loadTime);
- }
- }
- }
- public class LogObject
- {
- public string url { get; set; }
- public double loadTime { get; set; }
- public int count { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement