2011-02-04 61 views
1

在我的网站中,我使用线程在后台执行重复过程。现在该过程需要大约30到45秒才能更新数据库。如何优化ASP.NET中后台运行的线程的性能?

基本上该功能执行以下操作:

1)从网站下载CSV。

2)解析CSV并更新数据库表。

我想优化性能并将时间减少到15秒。

我该怎么做?

编辑:

这里是代码:

 // MAIN FUNCTION IN THE THREAD 
     private static void downloadAndParse() 
     { 
      NewHive.MyServ newServe = new NewHive.MyServ(); 
      NewHive.CsvDownload newService = new NewHive.CsvDownload(); 
      //NewHive.MyServ newServe = new NewHive.MyServ(); 
      string downloadSuccess = newService.CsvDownloader(); 
      if (downloadSuccess == "Success") 
      { 
       string parseSuccess = newService.CsvParser(); 

      } 
      newServe.updateOthersInPosition(); 
     } 







    //CSV DOWNLOAD FUNCTION 
    public string CsvDownloader() 
    { 
     Byte[] inBuf = null; 
     // HttpWebRequest wr = Convert.ChangeType(WebRequestFactory.Create("http://finance.yahoo.com/d/quotes.csv?s=RHT+MSFT&f=sb2b3jk"),HttpWebRequest); 
     // HttpWebResponse ws = Convert.ChangeType(wr.GetResponse(),HttpWebResponse); 

     HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("http://download.finance.yahoo.com/d/quotes.csv?s=INDU+INDU+^N225+^GSPC+^GDAXI+^FCHI+^HSI+^IXIC+^STOXX50E+^FTSE&f=l1bd14na"); 
     HttpWebResponse ws = (HttpWebResponse)wr.GetResponse(); 
     Stream str = ws.GetResponseStream(); 
     inBuf = new Byte[100000000]; 
     int bytesToRead = (int)inBuf.Length; 

     int bytesRead=0; 
     while(bytesToRead>0) 
     { 
      int n = str.Read(inBuf,bytesRead,bytesToRead); 
      if(n==0) 
      { 
       break; 
      } 
      bytesRead += n; 
      bytesToRead -= n; 
     } 
     FileStream fstr = new FileStream("D:\\Hosting\\7312812\\html\\News.csv", FileMode.Create, FileAccess.Write); 


     // FileStream fstr = new FileStream("News.csv", FileMode.Create, FileAccess.Write); 
     // FileStream fstr = new FileStream("C:\\VSS Working Folder\\27 Jan 11 NewHive\\NewHive\\CSV\\new.csv", FileMode.Create, FileAccess.Write); 
     fstr.Write(inBuf,0,bytesRead); 
     str.Close(); 
     fstr.Close(); 

     return "Success"; 
    } 







    //CSV PARSER FUNCTION 
    public string CsvParser() 
    { 
     int _nNrRowsProccessed = 0; 

     string connectionString = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + ConfigurationManager.AppSettings["CSVFolder"] + ";"; 

    OdbcConnection conn = new OdbcConnection(connectionString); 

    try 
    { 
     conn.Open(); 

     string strFileName = ConfigurationManager.AppSettings["CSVFile"]; 
     string strSQL = "Select * from " + strFileName; 

     OdbcCommand cmd = new OdbcCommand(); 
     cmd.Connection = conn; 
     cmd.CommandText = strSQL; 
     cmd.CommandType = CommandType.Text; 

     OdbcDataReader reader = cmd.ExecuteReader(); 

     NewHive.MyServ newService = new NewHive.MyServ(); 
     // MasterCalendar_DB.OpenMySQLConnection(); 

     while (reader.Read()) 
     { 
      decimal LastTradePrice; 
      decimal Bid; 
      string MarketOpen; 
      string IndexCode; 
      string Index; 
      decimal Offer; 
      decimal LTPDatabase=0.1M; 

       IndexCode = reader[3].ToString(); 
       String addSQL = "Select LastTradePrice from `jsontest`.`tbl_MarketData` where IndexCode = '" + IndexCode + "'"; 
       MySqlConnection objMyCon = new MySqlConnection(strProvider); 
       objMyCon.Open(); 
       MySqlCommand command = objMyCon.CreateCommand(); 

       command.CommandText = addSQL; 
       MySqlDataReader result = command.ExecuteReader(); 
       //int j = command.ExecuteNonQuery(); 
       while (result.Read()) 
       { 
        LTPDatabase = Convert.ToDecimal(result[0]); 
        // LTPDatabase = Math.Round(LTPTemp, 2); 
       } 
       objMyCon.Close(); 
       decimal LTPTemp = Convert.ToDecimal(reader[0].ToString()); 
       LastTradePrice = Math.Round(LTPTemp, 2); 
       if (reader[1].ToString() != "N/A") 
       { 
        Bid = Convert.ToDecimal(reader[1].ToString()); 
       } 
       else 
       { 
        Bid = 10.0M; 
       } 
       if (LastTradePrice != LTPDatabase) 
       { 
        MarketOpen = "Open"; 
       } 
       else 
       { 
        MarketOpen = "Close"; 
       } 
       Index = reader[4].ToString(); 

       if (reader[5].ToString() != "N/A") 
       { 
        Offer = Convert.ToDecimal(reader[5].ToString()); 
       } 
       else 
       { 
        Offer = 20.0M; 
       } 
      //} 
      // string[] arLine = strLine.Split(';'); 

      // string strAgencyPropertyID = arLine[0]; 
      // DateTime dt = DateTime.Parse(arLine[1]); 
      // Int64 nDate = (Int64)Util.ConvertToUnixTimestamp(dt); 
      // String strAvailability = (arLine[2]); 

      _nNrRowsProccessed++; 

      newService.CSVInsert(IndexCode,Index,MarketOpen,Bid,Offer,LastTradePrice); 
      // MasterCalendar_DB.Insert(strAgencyPropertyID, nDate, strAvailability); 
     }   

    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 
    finally 
    { 
     conn.Close(); 
     // MasterCalendar_DB.CloseMySQLConnection(); 
    } 
    return "Success"; 
    } 
} 
+4

您应该首先分析以确定哪个方面需要最多时间......然后专注于此?没有任何代码或更多细节,这个问题是无法回答的。 – 2011-02-04 04:52:03

+0

您是否已经获得了三个主要流程(文件传输,解析算法,数据访问)的相关时间指标以确定瓶颈? – 2011-02-04 04:53:27

回答

3

你有很多事情你的代码错误:

  • 就会有各种的这些实施IDisposable的对象,而你并没有将它们放入using区块。例如,

    OdbcDataReader reader = cmd.ExecuteReader();

应该

using (OdbcDataReader reader = cmd.ExecuteReader()) 
{ 
    // ... 
} 
  • 不要捕捉你不能处理异常。
相关问题