На моем веб-сайте я использую thread для выполнения повторяющегося процесса в фоновом режиме. Теперь для обновления базы данных процесс занимает около 30-45 секунд.Как оптимизировать производительность потока, выполняющегося в фоновом режиме в ASP.NET?
В основном функция выполняет следующие действия:
1) Загрузить CSV с веб-сайта.
2) Разберите CSV и обновите таблицы базы данных.
Я хочу оптимизировать производительность и сократить время, затрачиваемое на 15 секунд.
Как я могу это сделать?
EDIT:
Вот код:
// 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";
}
}
Возможно, вам следует начать с профилирования, чтобы определить, какой аспект занимает больше всего времени ... затем сосредоточиться на этом? Без какого-либо кода или более подробностей этот вопрос неопровержимо. –
Получили ли вы показатели по относительным таймингам трех основных процессов (передача файлов, алгоритм синтаксического анализа, доступ к данным), чтобы определить узкое место? –
Каков алгоритм/код, который вы используете для загрузки и анализа csv? Какой шаг занимает больше времени? –