ããªãŒã©ã³ã¹ã«åŸäºã ã©ãããããããç§ã®å人ãèªåè»åŠæ ¡ã®ææè ãšäžç·ã«ç§ãé£ããŠããŠãããã§åœŒãã¯ç§ã®ãã±ããã®ã³ã¬ã¯ã·ã§ã³ã圢æããããã°ã©ã ãäœæããããã«ç§ã«é Œã¿ãŸããã 圌ã®SDAãã±ããããã¯ã®ãããªãã®ã ãã¹ãŠã®è³ªåã¯MySQL DBMSã®3ã€ã®ããŒãã«ã«ä¿åãããŸãã1ã€ã®è³ªåã®ã³ã¬ã¯ã·ã§ã³ããããã®è³ªåã®å¥ã®äž»é¡ãããã³è³ªåã«å¯ŸããæåŸã®åçã§ãã ãã®çµæã800åã®è³ªåïŒåçããïŒããããŸãã ãã©ãŒã¡ãŒã·ã§ã³ã®æåã®ããŒãžã§ã³ã¯ãå éšã«è³ªåãé 次圢æãããåŸæ¥ã®æ¹æ³ã§ããã ãã«ãã¹ã¬ããããã°ã©ã ã®å®è¡ã®ãããã¯ã«éåžžã«èå³ãããã®ã§ãå®å šã«æ©èœããã¡ãœãããäœæããåŸããã¹ãŠããã䟿å©ã«ãããšåæã«ããµã³ããªã³ã°é床ãè¿œå ããããšã«ããŸããã
ãŸããSDAãã±ããã®ããŒãžã§ã³ãäœæããããã«ãåé¡ã®ãããã¯ãèæ ®ããå¿ èŠããããŸãã ãŸãããããã¯ã¯12åã®ã¿ã§ãã æåã®8ã€ã®ãããã¯ã¯40ã®è³ªåã§æ§æãããæ®ãã®120ã¯è³ªåã§ããåãããã¯ã«ã¯ãã±ããã®ç¹å®ã®çªå·ããããæåã®8ã€ã®ãããã¯ã¯ãã±ããã®1ã€ã®è³ªåã«å¯Ÿå¿ããŸãã çæããã質åã®ããŒãžã§ã³ãä¿åããããã«ãèŸæžèŸæžã䜿çšãããŸããåèŸæžã«ã¯ãç¹å®ã®ãããã¯ã«é¢ãã質åã®ãªã¹ããä¿åãããŸãã ããã«ã質åã®é åºã¯åžžã«ç°ãªã£ãŠããå¿ èŠããããŸãã 0ãã40ãŸã§ã®æ°åã®ã·ãŒã±ã³ã¹ãç¹°ãè¿ããªãã§çæããã¡ãœãããå¿ èŠã§ãããã®ããããã±ãããšããã«å¿ãã質åãéžæããããšã«ãªããŸãã ããããã¹ãŠã念é ã«çœ®ããŠããã¹ãŠã®è³ªåãçæããããã®æ¬¡ã®ã¢ã«ãŽãªãºã ãååŸããŸãã
- æã£ãŠãããã¹ãŠã®ãããã¯ãååŸããŸãã
- ãããã¯ãããåãããã¯ã®è³ªåçªå·ã®ãªã¹ããäœæããŸãã
- ä¹±æ°ãçæããŸã;
- 質åã®ãªã¹ããšä¹±æ°ã®ã·ãŒã±ã³ã¹ã«åºã¥ããŠãéžæãè¡ããäž»é¡ã«é¢ãã質åã®ãªã¹ããäœæããŸãã
- èŸæžã«åãªã¹ããå ¥åããŸãã
æåã®2ã€ã®ã¹ãããã®å®è£ ãšæäœã¯ããžãã¯ã«äŸåããç§ã®æèŠã§ã¯ããµã³ããªã³ã°ã®èŠæ±ã«å¿ããŠ1åå®è¡ã§ããŸãã 3çªç®ã®ã¹ãããã¯ã質åãã©ãã ãç°ãªããããããåãããã¯ã®åã«æ°äžä»£ãè¡ããããã¹ãŠã®ãããã¯ã«å¯ŸããŠ1åè¡ããã«ãã£ãŠç°ãªããŸãã ç§ã¯äžåºŠã ãçæããããšãéžæããŸããããç§ã®æèŠã§ã¯ããã§ååã§ãã 4çªç®ã®ã¹ãããã¯æãé »ç¹ã«æ©èœãããã®å Žæã§ã¯ãµã³ããªã³ã°æã«æãæéããããã¯ãã§ãã æåŸã®ã¹ãããã¯ç°¡åã§ãã¹ãããããŠãã ããã
DBæ¥ç¶
ããŒã¿ããŒã¹ãžã®æ¥ç¶ãæ€èšããŠãã ããã ãããè¡ãã«ã¯ãæ¥ç¶æååConnectionããã³DataReaderãæ ŒçŽããæœè±¡ã¯ã©ã¹ã䜿çšããŸãã
public abstract class SqlBase { protected static readonly String Connect; protected readonly MySqlConnection SqlConnection; protected MySqlDataReader SqlDataReader; static SqlBase() { Connect = String.Format("Database={0};Data Source={1};User ID={2};Password={3};CharSet=utf8", Settings.Default.Database, Settings.Default.DataSource, Settings.Default.UserId, Settings.Default.Password); } protected SqlBase() { try { this.SqlConnection = new MySqlConnection(Connect); this.SqlConnection.Open(); } catch (Exception ex) { throw new Exception(ex.Message, ex); } } }
æœè±¡ã¯ã©ã¹ã奜ããªã®ã¯ã䟿å©ãªäœæ¥ããžãã¯ãèªåã§äœæããŠé ãããšãã§ãã掟çç©ãä»ã®è©³çŽ°ã«å®å šã«éäžããä»ã®è©³çŽ°ãå¿é ããã«ãåºæ¬ã¯ã©ã¹ã«è²¬ä»»ãå²ãåœãŠãããšãã§ããããã§ãã ãã®å Žåã掟çã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ãäœæãããšãããŒã¿ããŒã¹ãæäœããããã«å¿ èŠãªæ¡ä»¶ãèªåçã«ååŸãããã¯ã©ã¹èªäœã®ããžãã¯ãå®è£ ããŠè³ªåã®ãµã³ãã«ãäœæããããšãã§ããŸãã
public sealed class SqlPerceptionQuestions : SqlBase { public Dictionary<Int32, List<Question>> GetQuestion() { Generation(); GetTheme(); return Request(); } private Dictionary<Int32, List<Question>> Request() { var _collectionDictionary = new Dictionary<Int32, List<Question>>(); for(int ctr = 0; ctr < 12; ctr++) { using (var _questions = new SqlQuestions()) { if (ctr < 8) { _collectionDictionary[ctr] = _questions.GetQuestionSmall((Int16)ctr); } else { _collectionDictionary[ctr] = _questions.GetQuestionGreat((Int16)ctr); } } return _collectionDictionary; } private async void GetTheme() { // } // 40 private List<Question> GetQuestionSmall(Int16 numTheme) { var _listQuestions = new List<Question>(); for (Int16 numCard = 0; numCard < 40; numCard++) { _listQuestions.Add(GetQuestion(numCard, numTheme)); } return _listQuestions; } // 120 private List<Question> GetQuestionGreat(Int16 numTheme) { var _listQuestions = new List<Question>(); for (Int16 numQuestion = 0; numQuestion < 3; numQuestion++) for (int numCard = 0; numCard < 40; numCard++) { _listQuestions.Add(GetQuestion(numQuestion, numTheme, numQuestion)); } return _listQuestions; } // private Question GetQuestion(Int16 numCard, Int16 numTheme, Int16 numQuestion = 0) { // } // private List<String> GetResponse(Int32 questions_id) { // } }
ããã¯æãã·ã³ãã«ã§æãåæããããŒãžã§ã³ã§ãçŽ2ç§ãš200ã400ããªç§ã§åäœããŸããããã«ããããã®éãã£ãšãŠãŒã¶ãŒã€ã³ã¿ãŒãã§ã€ã¹ããããã¯ãããŸãã éåžžã«æåã®å®è£ ãããªãã®æéïŒçŽ6ç§ïŒåãããããããã¯è¯ãåäœããŒãžã§ã³ã§ãã æ¹ååŸãçŽ2ç§ããåºãŸããã§ããã
éåæãµã³ããªã³ã°ããŒãžã§ã³ã®äœæ
ãã¹ãŠãæ£åžžã§ããããã¹ãŠããã§ã«æ©èœããŠããŸãããæ¬æ¥ããã¹ãç¶æ ã§ããïŒ çµå±ã®ãšãããã³ã³ãœãŒã«ã¢ããªã±ãŒã·ã§ã³ã§ã¯ãªããåæã¡ãœããïŒããããã³ã°ïŒããããŸãã æ£ããå®å šã«æ©èœããããã°ã©ã ãå¿ èŠã§ããããã¯0.5ç§ã§ããããã¯ãããŸããããã©ã®ãããªè² è·ã§ãé©åã«æ©èœããŸãã ãããè¡ãã«ã¯ããŸããGetQuestionïŒïŒã¡ãœãããæžãæããŸãã TAPïŒTask-based Asynchronous PatternïŒãã¿ãŒã³ã«åŸã£ãŠéåæã«ããŸãããã 誰ãæ°ã«ãããŠããããã€ã³ã¿ãŒãããã§èªãã§ããããç§ãæ¬åœã«å¥œããªããªãè¯ãæ¬ãæã£ãŠããŸã-ãã®ãããã¯ãéåžžã«ãã説æãããŠããAlex Davisã«ããCïŒ5.0ã®éåæããã°ã©ãã³ã°ããŸãã¯ãããèŠãŠãã ãã ã æžãæãããšã次ã®ããã«ãªããŸãã
public async Task<Dictionary<Int32, List<Question>>> GetQuestionAsync() { return await Task.Factory.StartNew(() => { Generation(); GetTheme(); return Request(); }, TaskCreationOptions.LongRunning); }
ãã®ã¡ãœããã§æãèå³æ·±ããã®ãèããŠã¿ãŸãããïŒTask.Factory.StartNewïŒïŒã ããŒãžã§ã³.NET 4.5以éã§ã¯ãTask.RunïŒïŒã®ããŒãžã§ã³ã䜿çšã§ããŸããããã¯ãäœææã®ãã©ã¡ãŒã¿ãŒãå°ãªãåçŽãªå®£èšã«ãã£ãŠä»¥åã®ãã®ãšã¯ç°ãªããŸãã æ¬è³ªçã«ãTask.RunïŒïŒã¯Task.Factory.StartNewïŒïŒããã䟿å©ãªã·ã§ã«ã§ãããéåæã¿ã¹ã¯ã®åçŽãªäœæã«éåžžã«é©ããŠããŸãããåæã«å¶åŸ¡ã®æè»æ§ãããå£ããŸãã ã©ã®ã¹ããªãŒã ãèšç®ãå®è¡ãããããŸãã¯ã©ã®ããã«èšç»ãããããããæ£ç¢ºã«å¶åŸ¡ããå¿ èŠãããå Žåã¯ãTask.Factory.StartNewïŒïŒã䜿çšããŸãã èå³ãããã°ã ãã¡ãããã§ã㯠ãã®å ŽåãTaskCreationOptions.LongRunningãªã©ã®ãã©ã¡ãŒã¿ãŒãæå®ããããããã®ãªãã·ã§ã³ã䜿çšããŸããããã®ãã©ã¡ãŒã¿ãŒã¯ããã®ã¿ã¹ã¯ãé·æéãšããŠããŒã¯ãããã®ã¯ãŒã¯ã¢ã€ãã ãé·æéå®è¡ãããä»ã®ã¯ãŒã¯ã¢ã€ãã ããããã¯ããå¯èœæ§ãããããšãæå³ããŸãã ãã®ãã©ã°ã¯ãéå°ãªãµãã¹ã¯ãªãã·ã§ã³ãäºæ³ãããTaskSchedulerã®æ å ±ãæäŸããŸããããã«ããã䜿çšå¯èœãªããŒããŠã§ã¢ã¹ã¬ããã®æ°ãããå€ãã®ã¹ã¬ãããäœæã§ããŸãã ãã®ãã©ã¡ãŒã¿ãŒã䜿çšãããšãã°ããŒãã«ããã³ããŒã«ã«ãã¥ãŒãå«ãThreadPoolãå®å šã«åé¿ã§ããŸãã ãã¿ã¹ã¯ã¹ã±ãžã¥ãŒã©ãã®è©³çŽ°ãã芧ãã ããã
ãããã£ãŠãããŒã¿åéçæã®éåæå®è¡ã¯æ¢ã«ååŸãããŠãããã¡ã€ã³ã¹ããªãŒã ã¯ãããã¯ãããŸããã
ãµã³ãã«ã®äžŠåå
ãã®åŸã䞊ååã«é²ã¿ãŸããããã«ãããããŒã¿ãµã³ããªã³ã°ã®é床ãåäžããŸãã ãã ãããã®ããã«ã¯ãã¡ã€ã³ã¯ã©ã¹ãå°ãããçŽããåºæ¬ã¯ã©ã¹ãå€æŽããã«ãŒãã®äžŠååäœã確ä¿ããããã«å¥ã®ã¯ã©ã¹ãè¿œå ããŠãç°ãªãã¹ã¬ããã®ããŒã¿ç«¶åãã¯ãªãã£ã«ã«ã»ã¯ã·ã§ã³ãå€æ°ã®åé¡ãåé¿ããå¿ èŠããããŸãã
æåã«ã SqlPerceptionQuestionsã¯ã©ã¹ã«æ°ãããã¹ããããã¯ã©ã¹SqlQuestionsãäœæããŸãã 質åãšåçãååŸããã¡ãœããã転éããSqlBaseãã掟çãããŸããå€éšã¯ã©ã¹ã§ã¯ãããããååŸããããã«ã¡ãœãããæ®ããäžåºŠã ãåŒã³åºããŠãäžé£ã®æ°åã圢æããŸãã
SqlQuestionsã¯ã©ã¹ã³ãŒã ïŒ
internal sealed class SqlQuestions : SqlBase { internal List<Question> GetQuestionSmall(Int16 numTheme) { var _listQuestions = new List<Question>(); for (Int16 numCard = 0; numCard < 40; numCard++) { _listQuestions.Add(GetQuestion(numCard, numTheme)); } return _listQuestions; } internal List<Question> GetQuestionGreat(Int16 numTheme) { var _listQuestions = new List<Question>(); for (Int16 numQuestion = 0; numQuestion < 3; numQuestion++) for (int numCard = 0; numCard < 40; numCard++) { _listQuestions.Add(GetQuestion(numQuestion, numTheme, numQuestion)); } return _listQuestions; } private Question GetQuestion(Int16 numCard, Int16 numTheme, Int16 numQuestion = 0) { // } private List<String> GetResponse(Int32 questions_id) { // } }
ã«ãŒãã䞊ååããã«ã¯ãParallel.ForïŒïŒã䜿çšããŸãã ããã¯ãããŒã¿ã®ããŒããè€æ°ã®ã¹ããªãŒã ã«æŽçããããã®éåžžã«äŸ¿å©ãªæ¹æ³ã§ãã ãã ããããã«ã¯ã1ã€ã®Connectionã§1ã€ã®DataReaderãåŠçã§ãããããå°ãªããšãè€æ°ã®ããŒã¿ããŒã¹æ¥ç¶ãäœæããå¿ èŠããããšããäºå®ã䌎ããŸãã .RequestïŒïŒã¡ãœãããæžãæããŸãïŒ
private Dictionary<Int32, List<Question>> Request() { var _collectionDictionary = new Dictionary<Int32, List<Question>>(); var _po = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }; Parallel.For(0, 12, _po, ctr => { using (var _questions = new SqlQuestions()) { if (ctr < 8) { _collectionDictionary[ctr] = _questions.GetQuestionSmall((Int16)ctr); } else { _collectionDictionary[ctr] = _questions.GetQuestionGreat((Int16)ctr); } } }); return _collectionDictionary; }
ãããŠãæ¥ç¶ãéããåŸããããéããå¿ èŠããããŸãã ããã«ãããã¯ãã¹ãŠãµã€ã¯ã«ã§è¡ãããŸãã ããããã¹ãŠå®è£ ããããã«ãåå¥ã®æŽŸçã¯ã©ã¹SqlQuestionsãäœæããããšã«ããŸããã æ¥ç¶ãéããã«ã¯ã.DisposeïŒïŒãåŒã³åºããŸãããã®äžã§ãéãããšãã«è¡ãå¿ èŠãããããšãæžãçããŸãã ãããè¡ãã«ã¯ãæåã«åºæ¬ã¯ã©ã¹ã§.DisposeïŒïŒã¡ãœããã宣èšããŸãã
public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected abstract void Dispose(Boolean disposing);
ãããŠã掟çã¯ã©ã¹ã§ç°ãªãæ¹æ³ã§å®è£ ããŸãã ãªããã ãŸããããŒã¿ããŒã¹æ¥ç¶ãäœæããŠéããå Žåã¯ã©ããªããŸããïŒ æ¥ç¶ãéããŠããéãããšããã°ããã®éïŒçŽ3åéïŒMySQLæ¥ç¶ããŒã«ã«é 眮ãããŸããæ°ããæ¥ç¶ãéããšãæ¥ç¶ã¯ããŒã«ããååŸãããŸããã€ãŸããåãªãŒãã³ã®ããã®æéãšãªãœãŒã¹ã¯äœ¿çšãããŸããã æ°ããã¡ãœãããèµ·åããŠãããŒã¿ããŒã¹æ¥ç¶ãéãã®ã«ãããæéã確èªããŸãããããã®ãããæ¥ç¶ãéãããŠããã³ãŒãã®åºæ¬ã¯ã©ã¹Stopwatchã«æ¿å ¥ããåºåã«ãããã®ã確èªããŸãã ã³ãŒãïŒ
protected SqlBase() { Stopwatch st = new Stopwatch(); st.Start(); try { this.SqlConnection = new MySqlConnection(Connect); this.SqlConnection.Open(); } catch (MySqlException ex) { throw new Exception(ex.Message, ex); } st.Stop(); Debug.WriteLine(" : " + st.Elapsed.Seconds.ToString() + " " + st.Elapsed.Milliseconds.ToString() + " "); }
é通æé
æåã®æ¥ç¶ã¯æãé·ãã SqlPerceptionQuestionsã¯ã©ã¹ã®æ¥ç¶ãéããŸããããã¯ãã¡ãœããã®å šæéã«ããã£ãŠéãããŸãã åŸç¶ã®æ¥ç¶ã¯ã SqlQuestionsã¯ã©ã¹ãã€ã³ã¹ã¿ã³ã¹åãããšãã«ã«ãŒãå ã§éãããæ¥ç¶ã§ãã ç§ã®ã³ã³ãã¥ãŒã¿ãŒã®ããã»ããµãŒã®æ°ãèæ ®ã«å ¥ãããšãæ倧4ãµã€ã¯ã«ã§4ã€ã®æ¥ç¶ãéãããŸãã åèšã§5ã€ã®æ¥ç¶ãæåã«éããããµã€ã¯ã«ã§ã¯ããããéãããéãããããŸãã ãããã£ãŠãæåã®5ã€ã®æ¥ç¶ãéãã«ã¯æéãå¿ èŠã§ãããã®åŸããµã€ã¯ã«å ã§å€ãæ¥ç¶ãéããããæ°ããæ¥ç¶ãéããããšãæ¥ç¶ã¯ããŒã«å ã«ãããå¿ èŠã«ãªããã³ã«åçŽã«çºè¡ããããããæéãšãªãœãŒã¹ã¯ããããŸããã ãã®ãããã¯ã©ã¹ã®ã¯ãªãŒã³ã¢ããã¯ãããã«ç°ãªãæ¹æ³ã§å®è£ ãããŸãã SqlPerceptionQuestionsã¯ã©ã¹ã§ã¯ãã¡ãœããã¯æ¬¡ã®ããã«ãªããŸãã
protected override void Dispose(bool disposing) { if (!this.disposed) { if (SqlDataReader != null) { SqlDataReader.Close(); SqlDataReader.Dispose(); } SqlConnection.Close(); SqlConnection.Dispose(); MySqlConnection.ClearAllPools(); } disposed = true; }
SqlQuestionsã¯ã©ã¹ã§ããMySqlConnection.ClearAllPoolsïŒïŒ;ã®è¡ãé€ããŸãã çµå±ã®ãšããããã®ãŸãŸã«ããŠãããšã次ã®ãããªç¶æ³ã«ãªããŸãã
é通æé
ã芧ã®ããã«ãã¹ã¬ããããŒã«ã絶ããã¯ãªãŒãã³ã°ããããšã«ãããæ¥ç¶ãåžžã«éããããã®åŸã®çµæãçããŸãã
MySqlæ¥ç¶ããŒã«
ãã®ç¬éããã詳ããèããŠã¿ãŸãããã Connector / Net MySqlã¯ãæ¥ç¶ããŒãªã³ã°ããµããŒãããŠã䜿çšçã®é«ãã¢ããªã±ãŒã·ã§ã³ããŒã¿ããŒã¹ã®ããã©ãŒãã³ã¹ãšã¹ã±ãŒã©ããªãã£ãåäžãããŸãã ãã®æ©èœã¯ããã©ã«ãã§æå¹ã«ãªã£ãŠããŸãã æ¥ç¶æååãªãã·ã§ã³ã䜿çšããŠãç¡å¹ã«ããããç¹æ§ãå€æŽã§ããŸãã æ¥ç¶æååã¯ãæ¥ç¶ããŒã«ã«é¢ãããªãã·ã§ã³ããµããŒãããŠããŸãã
| | 詳现 |
---|---|---|
ãã£ãã·ã¥ãµãŒããŒã®ããããã£ãCacheServerProperties | ãªã | ããŒã«ããã®æ¥ç¶ãè¿ããããã³ã«ãããã€ãã®ã·ã¹ãã å€æ°ïŒSHOW VARIABLESïŒã®ãã©ã¡ãŒã¿ãŒãæŽæ°ãããã©ããã決å®ããŸãã ãã®ãªãã·ã§ã³ãæå¹ã«ãããšãæ¥ç¶ããŒã«ã³ã³ããã¹ããžã®æ¥ç¶ãé«éåãããŸãã ã¢ããªã±ãŒã·ã§ã³ã«ã¯ãä»ã®æ¥ç¶ã«ãã£ãŠè¡ãããæ§æã®å€æŽã¯éç¥ãããŸããã ãã®ãªãã·ã§ã³ã¯ãããŒãžã§ã³Connector / Net 6.3以éã«è¿œå ãããŸããã |
æ¥ç¶ã©ã€ãã¿ã€ã ãConnectionLifeTime | 0 | æ¥ç¶ãããŒã«ã«è¿ããããšããã®äœææéãçŸåšã®æéãšæ¯èŒãããConnectionLifeTimeå€ãè¶ ãããšããã®æ¥ç¶ã¯ç Žæ£ãããŸãã ããã¯ãéçšãµãŒããŒãšãªã³ã©ã€ã³ãµãŒããŒã®éã§è² è·ãåæ£ããã¯ã©ã¹ã¿ãŒæ§æã§åœ¹ç«ã¡ãŸãã å€ããŒãïŒ0ïŒã®å ŽåãããŒã«å ã®æ¥ç¶ã¯å¯èœãªéãæ倧ã®æéåŸ æ©ããŸãã |
æ¥ç¶ãªã»ãããConnectionReset | åœ | trueã®å ŽåãããŒã«ããååŸããããšãã®æ¥ç¶ç¶æ ããªã»ãããããŸãã ããã©ã«ãå€ã䜿çšãããšããµãŒããŒãæ¥ç¶ãååŸããããã®è¿œå ã®åŠçãµã€ã¯ã«ãåé¿ã§ããŸãããæ¥ç¶ã¹ããŒã¿ã¹ã¯ãªã»ãããããŸããã |
æ倧ããŒã«ãµã€ãºãæ倧ããŒã«ãµã€ãºãMaximumPoolsizeãmaxpoolsize | 100 | ããŒã«å ã«ååšã§ããæ¥ç¶ã®æ倧æ°ã |
æå°ããŒã«ãµã€ãºãæå°ããŒã«ãµã€ãºãMinimumPoolSizeãminpoolsize | 0 | ããŒã«ã«å ¥ããããååç©ã®æå°æ°ã |
ããŒãªã³ã° | æ¬åœ | trueã®å ŽåãMySqlConnectionãªããžã§ã¯ãã¯ããŒã«ããååŸãããå¿ èŠã«å¿ããŠäœæããã察å¿ããããŒã«ã«è¿œå ãããŸãã å®çŸ©ãããŠããå€ã¯ãtrueãfalseãyesãããã³noã§ãã |
ãããã®ãã©ã¡ãŒã¿ãŒã䜿çšããŠãå¿ èŠã«å¿ããŠããŒã«ã管çã§ããŸãã äŸïŒ
- æ¥ç¶ãªã»ããã¯æ¥ç¶ã³ã³ããã¹ãããªã»ããããããã©ã«ãã§äœ¿çšãããŠããªãå ŽåãããŒã«ããæ¥ç¶ãåä¿¡ããæéãå°ãé·ããªããŸãã
- æå°ããŒã«ãµã€ãºã䜿çšãããšãæ¥ç¶æ°ãæå°ããŒã«ãµã€ãºã®å€ä»¥äžã®å Žåãå¿ èŠã«å¿ããŠãç¡æéã«ååšããããŒã«å ã®æ¥ç¶æ°ãèšå®ã§ããŸãã
- ïŒãã®ããã°ã©ã ã®äŸã®ããã«ïŒã¹ã¬ããã®ããŒã«ã絶ããã¯ãªã¢ãããšãããŒãªã³ã°ãç¡å¹ã«ããŠãåãçµæãåŸãããŸã
ããã©ã«ãã§ã¯ãMySQL Connector / Net 6.2以éã3åããšã«å®è¡ããã3å以äžã¹ã¿ã³ãã€ïŒäœ¿çšãããŠããªãïŒã§ããããŒã«ããæ¥ç¶ãåé€ããããã¯ã°ã©ãŠã³ããžã§ãããããŸãã ããŒã«ãã¯ãªã¢ãããšãã¯ã©ã€ã¢ã³ãåŽãšãµãŒããŒåŽã®äž¡æ¹ã§ãªãœãŒã¹ã解æŸãããŸãã ããã¯ãã¯ã©ã€ã¢ã³ãåŽã§ã¯åæ¥ç¶ããœã±ããã䜿çšãããµãŒããŒåŽã§ã¯åæ¥ç¶ããœã±ãããšã¹ããªãŒã ã䜿çšããããã§ãã
䞊ååŒã³åºãã¹ã¿ãã¯
èå³ãåŒãããã«ãããšãã°.GetQuestionïŒïŒã¡ãœããã«ãã¬ãŒã¯ãã€ã³ãã眮ããŠã䞊ååŒã³åºãã¹ã¿ãã¯ãèŠããšã次ã®ããã«ãªããŸãã
䞊åã¹ã¿ãã¯
ã¹ã¯ãªãŒã³ã·ã§ãããããããããã«ãäžæãããŠããã¹ã¬ããã®1ã€ã«ãããã³ãŒã«ã¹ã¿ãã¯ããããã®ã¡ãœããã質åã®å°ããªã³ã¬ã¯ã·ã§ã³ãããŒãããã¡ãœããããåŒã³åºããããšå€æããŸãïŒ40ïŒã ãã®å·ŠåŽã«ã¯ããã«3ã€ã®ã¹ã¬ãããããããã®ãã¡2ã€ã¯ã³ã¬ã¯ã·ã§ã³ã«è³ªåãè¿œå ããããã®è¡ã§ãã®æç¹ã§åæ¢ããŸãããã®ã¹ã¬ããã¯ãå°ããªè³ªåã®ã³ã¬ã¯ã·ã§ã³ãåŠçããŸãã ãããŠæåŸã®ã¹ã¬ãã4ã¯åŠçã«åŸäºããŠããããã®æç¹ã§è³ªåã«å¯ŸããåçãåãåããŸãã倧èŠæš¡ãªã³ã¬ã¯ã·ã§ã³ïŒ120ïŒããã®è³ªåã«å¯ŸããŠã®ã¿ã§ãã ãããã®4ã€ã®ã¹ã¬ããã¯ãã¹ãŠäžŠåã«ãŒãã§äœæãããæ®ãã®ã«ãŒãå埩ã¹ã¬ãããšã»ãŒåæã«åäœããŸãã ãããã®ã¹ã¬ããã¯ãããã°ã©ã å ã®ã¹ã¬ããã®åèšæ°ã«å«ãŸãããã®ãã¡8ã€ã¯æ®ãã®4ã€ãããã°ã©ã ã®ä»ã®ã¿ã¹ã¯ã解決ããŸãã
æåŸã®ä»äžã-äŸå€åŠç
ãããŠæåŸã«ãããã°ã©ã ãæ©èœããããã«ã¯ãäŸå€åŠçãå¿ èŠã§ãã çªç¶ããã£ãŒã«ããŸãã¯ä»ã®ãã©ã¡ãŒã¿ãããŒã¿ããŒã¹ããå€æŽãããããããã°ã©ã èªäœã§äºæããªããšã©ãŒãçºçããŸããã GetQuestionAsyncïŒïŒã¡ãœãããæžãæããŸãïŒ
public async Task<Dictionary<Int32, List<Question>>> GetQuestionAsync() { return await Task.Factory.StartNew(() => { try { Generation(); GetTheme(); return Request(); } catch (AggregateException ex) { throw new AggregateException(ex.Message, ex.InnerExceptions); } catch (Exception ex) { throw new AggregateException(ex.Message, ex.InnerException); } }, TaskCreationOptions.LongRunning); }
AggregateExceptionäŸå€åŠçã¯ãäŸå€ãçºçããå ŽåãParallel.Forã«ãŒãããã®ã¿ã€ãã®ãšã©ãŒãçºçããããšããäºå®ã«é¢é£ããŠãããããåŠçããŠåŒã³åºãå ã«æž¡ãå¿ èŠããããŸãã 䞊åã«ãŒãããã®çš®ã®ãšã©ãŒãçæããã®ã¯è«ççã§ãã ãã®ç¬éããã詳现ã«èããŠã¿ãŸãããããã®ããã.GetQuestionïŒïŒã®Sqlã¯ãšãªãå€æŽããããŒã¿ããŒã¹ããŒãã«ã«ååšããªããã©ã¡ãŒã¿ãŒã®1ã€ãæ æã«èª€ã£ãŠç€ºããŸããã ååŸãããã®ïŒ
ãšã©ãŒ
ããã«ããããã°ãç¶è¡ãããšãåèšã§ãã®äŸå€ã4åçºçããŸãããããã¯éåžžã«è«ççã§ãã 1ã€ã®çç±ã«é¢é£ããŠããã«ããããããã4ã€ãã¹ãŠãåŠçããã«ã¯ãäœããã®æ¹æ³ã§ããããé 眮ããå¿ èŠããããŸããAggregateExceptionãé©ããŠããŸãã
äŸå€åŠçã¯ã.GetThemeïŒïŒã¡ãœããã§äŸå€ãçºçããå ŽåãäŸå€ã1ã€ãããããããã£ããããå¿ èŠããããšããäºå®ã«é¢é£ããŠããŸãã
åŒã³åºãã³ãŒãã¯æ¬¡ã®ãšããã§ãã
private async void Button_Click(object sender, RoutedEventArgs e) { Stopwatch st = new Stopwatch(); st.Start(); try { SqlQuest = new SqlPerceptionQuestions(); collectionQuest = await SqlQuest.GetQuestionAsync(); } catch (AggregateException ex) { ex.ShowError(); } catch(Exception ex) { ex.ShowError(); } finally { if (SqlQuest != null) SqlQuest.Dispose(); } st.Stop(); Debug.WriteLine(" : " + st.Elapsed.Seconds.ToString() + " " + st.Elapsed.Milliseconds.ToString() + " "); Debugger.Break(); }
æåŸã«...
äžè¬ã«ã䞊åããŒãžã§ã³ã®ãµã€ã¯ã«ãæžãããšãã«ãããŒã¿ããŒã¹æ¥ç¶ã®é »ç¹ãªãªãŒãã³ãšã¯ããŒãºãã©ãã»ã©å€§ããªåœ±é¿ãäžãããã«ã€ããŠèããŸããã ãã©ãŒã©ã ã«ã¢ã¯ã»ã¹ããŠè³¢ã人ã«å°ãããšãæ¥ç¶ããŒã«ã«ã€ããŠåŠã³ãŸããã ãããã圌ã¯åœŒèªèº«ã«å°ããŸããããããäœæããå¿ èŠãããããããã¯æé»çã«äœæãããŸããïŒ ç§ã¯å°ãå®éšããŠMySQLã®ããã¥ã¡ã³ããèªã¿ãŸããããã®çµæããã®ããšã¯éåžžã«æåããŠãããšããçµè«ã«éããŸãããæ¥ç¶ããŒã«ã§ãïŒ
å€æŽåŸã®ãã¹ãŠã®ã³ãŒã
// public abstract class SqlBase : IDisposable { protected static readonly String Connect; protected readonly MySqlConnection SqlConnection; protected MySqlDataReader SqlDataReader; protected Boolean disposed; static SqlBase() { Connect = String.Format("Database={0};Data Source={1};User ID={2};Password={3};CharSet=utf8;CacheServerProperties=true", Settings.Default.Database, Settings.Default.DataSource, Settings.Default.UserId, Settings.Default.Password); } protected SqlBase() { Stopwatch st = new Stopwatch(); st.Start(); try { this.SqlConnection = new MySqlConnection(Connect); this.SqlConnection.Open(); } catch (Exception ex) { throw new Exception(ex.Message, ex); } st.Stop(); Debug.WriteLine(" : " + st.Elapsed.Seconds.ToString() + " " + st.Elapsed.Milliseconds.ToString() + " "); } ~SqlBase() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected abstract void Dispose(Boolean disposing); } // public sealed class SqlPerceptionQuestions : SqlBase { public async Task<Dictionary<Int32, List<Question>>> GetQuestionAsync() { return await Task.Factory.StartNew(() => { try { Generation(); GetTheme(); return Request(); } catch (AggregateException ex) { throw new AggregateException(ex.Message, ex.InnerExceptions); } catch (Exception ex) { throw new AggregateException(ex.Message, ex.InnerException); } }, TaskCreationOptions.LongRunning); } private Dictionary<Int32, List<Question>> Request() { var _collectionDictionary = new Dictionary<Int32, List<Question>>(); var _po = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }; Parallel.For(0, 12, _po, ctr => { using (var _questions = new SqlQuestions()) { if (ctr < 8) { _collectionDictionary[ctr] = _questions.GetQuestionSmall((Int16)ctr); } else { _collectionDictionary[ctr] = _questions.GetQuestionGreat((Int16)ctr); } } }); return _collectionDictionary; } private void GetTheme() { } private void Generation() { } protected override void Dispose(bool disposing) { if (!this.disposed) { if (SqlDataReader != null) { SqlDataReader.Close(); SqlDataReader.Dispose(); } SqlConnection.Close(); SqlConnection.Dispose(); MySqlConnection.ClearAllPools(); } disposed = true; } // internal sealed class SqlQuestions : SqlBase { internal List<Question> GetQuestionSmall(Int16 numTheme) { var _listQuestions = new List<Question>(); for (Int16 numCard = 0; numCard < 40; numCard++) { _listQuestions.Add(GetQuestion(numCard, numTheme)); } return _listQuestions; } internal List<Question> GetQuestionGreat(Int16 numTheme) { var _listQuestions = new List<Question>(); for (Int16 numQuestion = 0; numQuestion < 3; numQuestion++) for (int numCard = 0; numCard < 40; numCard++) { _listQuestions.Add(GetQuestion(numQuestion, numTheme, numQuestion)); } return _listQuestions; } private Question GetQuestion(Int16 numCard, Int16 numTheme, Int16 numQuestion = 0) { } private List<String> GetResponse(Int32 questions_id) { } protected override void Dispose(bool disposing) { if (!this.disposed) { if (SqlDataReader != null) { SqlDataReader.Close(); SqlDataReader.Dispose(); } SqlConnection.Close(); SqlConnection.Dispose(); } disposed = true; } } }