private static void PrintFoundWordsToConsole(IEnumerable<string> foundWords, long totalOperations, bool forceWriteFoundWords) { int wordLength = foundWords.Count(); if (wordLength < 64 || forceWriteFoundWords) { foreach (string word in foundWords) { Console.WriteLine(word); } Console.WriteLine(); } double time = RuntimeTimer.CalcAppRuntimeUs() / (double)totalOperations; Log.Information(Utils.Consts.c_logTimingInfo, wordLength, totalOperations, Math.Round(time * 1000, 2)); }
public static void Log(ISearchAlgorithm sortAlgorithm, IArrayGenerator arrayGenerator, int value) { var elapsed = RuntimeTimer.Measure(sortAlgorithm, arrayGenerator, value); var arrayType = arrayGenerator.GetType().Name; var size = arrayGenerator.Length; var algorithmName = sortAlgorithm.GetType().Name; var benchmarkFileName = $"{algorithmName}_{arrayType}.csv"; var benchmarkDirectory = BenchmarksFolder + $"{algorithmName}/"; Directory.CreateDirectory(benchmarkDirectory); var message = $"Elapsed; {elapsed}; Array Size; {size}; Array Type; {arrayType}; {algorithmName};"; Console.WriteLine(message); using var sw = new StreamWriter(benchmarkDirectory + benchmarkFileName, true); sw.WriteLine(message); }
/// <summary> /// Manager that will handle running the game cmdline options and return a status code to the caller. /// </summary> /// <param name="opts">CmdLineOptions object that contains the parsed command line args.</param> /// <returns>Error Code. 0 = Success, 1 = Failure.</returns> public static async Task<int> RunAndReturnExitCodeAsync(CmdLineOptions opts) { if (opts == null) { throw new ArgumentNullException(nameof(opts)); } RuntimeTimer.RegisterAppStart(); m_cmdLineOpts = opts; // Validate Input ValidateCmdLine val = new ValidateCmdLine(); val.IsCmdLineValid(opts); // Load the dictionary from disk await LoadDictFromDiskAsync(); await Task.Run(() => { BoardMaker maker = new BoardMaker(); maker.LoadRandomBoard(opts.SideLength); Board board = maker.GetBoard(); IGameSolver solver = null; if (m_cmdLineOpts.Singlethreaded) { solver = new SinglethreadedGameSolverService(); } else { solver = new AsyncGameSolverService(); } var foundWords = solver.Solve(m_wordDict, board); // This is a little odd. I want to stop timing before I write all the words, which takes a long time RuntimeTimer.RegisterAppEnd(); var totalOperations = solver.GetTotalOperations(); PrintFoundWordsToConsole(foundWords, totalOperations, opts.ForceWriteFoundWords); }); return Utils.Consts.c_exitCodeSuccess; }
/// <summary> /// Async entrypoint for the application. /// </summary> /// <param name="args">Commandline arguments</param> /// <returns>0 = Success; 1 = Error.</returns> static async Task Main(string[] args) { Log.Logger = new LoggerConfiguration() .WriteTo.Console() .CreateLogger(); WriteCopyright(); int exitCode = await Parser.Default.ParseArguments <CmdLineOptions, object>(args) .MapResult( (CmdLineOptions opts) => GameManager.RunAndReturnExitCodeAsync(opts), errs => Task.FromResult(Utils.Consts.c_exitCodeFailure)); Log.Information(Utils.Consts.c_logAppRuntime, RuntimeTimer.CalcAppRuntimeMs()); Log.CloseAndFlush(); Environment.Exit(exitCode); }
public RuntimeTimer LoadTimer(Guid runtimeId) { string timer; using (TransactionScope readUncommittedSupressedScope = PredefinedTransactionScopes.ReadUncommittedSupressedScope) { using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { WorkflowRuntime workflowRuntime = workflowPersistenceModelDataContext.WorkflowRuntimes.FirstOrDefault((WorkflowRuntime wr) => wr.RuntimeId == runtimeId); if (workflowRuntime == null) { workflowRuntime = new WorkflowRuntime { RuntimeId = runtimeId, Timer = string.Empty }; workflowPersistenceModelDataContext.WorkflowRuntimes.InsertOnSubmit(workflowRuntime); } timer = workflowRuntime.Timer; workflowPersistenceModelDataContext.SubmitChanges(); } readUncommittedSupressedScope.Complete(); } if (string.IsNullOrEmpty(timer)) { return(new RuntimeTimer()); } DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(Dictionary <TimerKey, DateTime>)); RuntimeTimer result; using (StringReader stringReader = new StringReader(timer)) { using (XmlReader xmlReader = XmlReader.Create(stringReader)) { result = new RuntimeTimer(dataContractSerializer.ReadObject(xmlReader) as IDictionary <TimerKey, DateTime>); } } return(result); }
public void SaveTimer(Guid runtimeId, RuntimeTimer timer) { DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(Dictionary <TimerKey, DateTime>)); string timer2; using (StringWriter stringWriter = new StringWriter()) { using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter)) { dataContractSerializer.WriteObject(xmlWriter, timer.Timers); xmlWriter.Flush(); timer2 = stringWriter.ToString(); } } using (TransactionScope readUncommittedSupressedScope = PredefinedTransactionScopes.ReadUncommittedSupressedScope) { using (WorkflowPersistenceModelDataContext workflowPersistenceModelDataContext = base.CreateContext()) { WorkflowRuntime workflowRuntime = workflowPersistenceModelDataContext.WorkflowRuntimes.FirstOrDefault((WorkflowRuntime wr) => wr.RuntimeId == runtimeId); if (workflowRuntime == null) { workflowRuntime = new WorkflowRuntime { RuntimeId = runtimeId, Timer = timer2 }; workflowPersistenceModelDataContext.WorkflowRuntimes.InsertOnSubmit(workflowRuntime); } else { workflowRuntime.Timer = timer2; } workflowPersistenceModelDataContext.SubmitChanges(); } readUncommittedSupressedScope.Complete(); } }