[英]How to improve the performance of below code?
I have written a code to ping 300 systems in a network and update the status(offline/online) in to the Access database file. 我已经编写了代码来ping网络中的300个系统,并将状态(脱机/联机)更新到Access数据库文件中。
I'm using task class for that. 我为此使用任务类。 Pinging 300 systems taking only less than a second but inserting those status in to database file taking nearly 30 to 40 seconds.
对300个系统执行Ping操作仅需不到一秒钟的时间,但将这些状态插入数据库文件所需的时间将近30到40秒。
It reducing my application's performance, please take a look in to my code. 它降低了我的应用程序的性能,请查看我的代码。 If
如果
Main method 主要方法
private static void Main(string[] args)
{
#region Reading IpAdddress
List<string> address = new List<string>();
Task t = Task.Run(() =>
{
var reader = new StreamReader(File.OpenRead(Environment.CurrentDirectory + @"\address.csv"));
while (!reader.EndOfStream)
{
var lines = reader.ReadLine();
var values = lines.Split(';');
address.Add(values[0]);
}
});
#endregion
Stopwatch timeSpan = Stopwatch.StartNew();
t.Wait();
AsyncPingTask(address).Wait();
Console.WriteLine("Update Completed");
Console.WriteLine(timeSpan.ElapsedMilliseconds);
Console.ReadLine();
}
Ping task ping任务
private static async Task AsyncPingTask(List<string> ipaddress)
{
try
{
Console.WriteLine("Ping Started");
var pingTasks = ipaddress.Select(ip =>
{
return new Ping().SendTaskAsync(ip);
}).ToList();
var replies= await Task.WhenAll(pingTasks);
Console.WriteLine("Ping Completed");
int online, offline;
online = 0;
offline = 0;
Console.WriteLine("Update in progress...");
foreach (var pingReply in replies)
{
var status = "";
if (pingReply.Reply.Status.ToString() == "Success")
{
online++;
status = "Online";
}
else
{
status = "Offline";
offline++;
}
Program p = new Program();
Parallel.Invoke(() =>
{
p.UpdateSystemStatus(pingReply.Address, status);
});
}
Console.WriteLine("Online Systems : {0}", online);
Console.WriteLine("Offline Systems : {0}", offline);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
}
Update status method 更新状态方法
private void UpdateSystemStatus(string ipAddr, string status)
{
using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\/Topology.accdb"))
{
string query = "UPDATE SystemStatus SET SystemStatus=@SystemStatus WHERE IP='" + ipAddr + "'";
OleDbCommand cmd = new OleDbCommand(query, con);
con.Open();
cmd.Parameters.AddWithValue("@SystemStatus", status);
cmd.ExecuteNonQuery();
}
}
This is not my area of expertise but my guess is that there is a lot of overhead in creating a new OleDbConnection
for each record that is updated. 这不是我的专业领域,但是我的猜测是,为每个更新的记录创建新的
OleDbConnection
产生很多开销。 I could imagine that it at least has to open the access database in the file system, check permissions, parse a bunch of stuff out, etc... An approach where the connection was created once would probably work much better. 我可以想象它至少必须在文件系统中打开访问数据库,检查权限,解析出很多东西,等等。。。创建一次连接的方法可能会更好。 I also see that there is some Transaction mechanism that could possibly improve performance.
我也看到有些事务机制可能会提高性能。
using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\/Topology.accdb"))
{
con.Open();
var trans = con.BeginTransaction();
foreach (var pingReply in replies)
{
var status = "";
if (pingReply.Reply.Status.ToString() == "Success")
{
online++;
status = "Online";
}
else
{
status = "Offline";
offline++;
}
string query = "UPDATE SystemStatus SET SystemStatus=@SystemStatus WHERE IP='" + ipAddr + "'";
OleDbCommand cmd = new OleDbCommand(query, con);
cmd.Transaction = trans;
cmd.Parameters.AddWithValue("@SystemStatus", status);
cmd.ExecuteNonQuery();
}
trans.Commit();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.