/// <summary> /// Writes an output structure to a file /// </summary> /// <param name="output">The structure to write</param> /// <param name="outputFileName">The file to write to</param> private static void WriteOutput(OutputStructure output, string outputFileName) { //for example using (var outfile = File.CreateText(outputFileName)) { if (output == null) { return; } outfile.WriteLine(output.CacheServerDescriptions.Length); foreach (var o in output.CacheServerDescriptions) { outfile.Write($"{o.Id} "); foreach (var v in o.VideoIds) { outfile.Write($"{v} "); } outfile.WriteLine(); } } }
public static int Score(InputStructure input, OutputStructure output) { var cacheServers = output.CacheServerDescriptions .ToDictionary( x => x.Id, x => x.VideoIds.ToArray()); long sumSaved = 0; long sumRequests = 0; input.Descriptions .AsParallel() .ForAll(req => { var Rv = req.VideoId; var Re = req.EndpointId; var L = input.Endpoints[Re]; var Ld = input.Endpoints[Re].Latency; var fastestCacheServer = L.Connections .Select(x => new { Latency = x.Latency, Cache = cacheServers[x.Id] }) .Where(x => x.Cache.Contains(Rv)) .OrderBy(x => x.Latency) .FirstOrDefault(); var cacheServerLatency = fastestCacheServer?.Latency ?? int.MaxValue; var Lmin = Math.Min(cacheServerLatency, Ld); var saved = Ld - Lmin; var savedMicro = saved; Interlocked.Add(ref sumSaved, savedMicro * req.NumRequests); Interlocked.Add(ref sumRequests, req.NumRequests); }); return((int)((sumSaved * 1000) / sumRequests)); }