tal*_*z13 6 c# file-io system.data.sqlite network-service
我想我的问题暂时解决了......我将我的服务和测试应用程序更改为SYSTEM帐户而不是NetworkService帐户.更改用户帐户的好处是否会持续存在,或者只是暂时性的,还有待观察.
我注意到我的小型224kB SQLite数据库在我的C#应用程序中打开非常慢,从一些小的ms到1.5秒或更长时间.下面是我的代码,以及我今天下午添加的所有额外调试语句.我把它缩小到调用,cnn.Open();如下面的日志所示:
2014-03-27 15:05:39,864 DEBUG - Creating SQLiteConnection...
2014-03-27 15:05:39,927 DEBUG - SQLiteConnection Created!
2014-03-27 15:05:39,927 DEBUG - SQLiteConnection Opening...
2014-03-27 15:05:41,627 DEBUG - SQLiteConnection Opened!
2014-03-27 15:05:41,627 DEBUG - SQLiteCommand Creating...
2014-03-27 15:05:41,627 DEBUG - SQLiteCommand Created!
2014-03-27 15:05:41,627 DEBUG - SQLiteCommand executing reader...
2014-03-27 15:05:41,658 DEBUG - SQLiteCommand executed reader!
2014-03-27 15:05:41,658 DEBUG - DataTable Loading...
2014-03-27 15:05:41,767 DEBUG - DataTable Loaded!
Run Code Online (Sandbox Code Playgroud)
如您所见,在这种情况下,打开连接需要1.7秒.我试过重复这个,无法预测后续连接是否会立即打开,或者像这样延迟.
我考虑过使用某种形式的连接池,但是对于单实例单线程应用程序来说,这是值得的吗?现在,我正在创建一个SQLiteDatabase类的实例,并为每个查询调用以下函数.
public DataTable GetDataTable(string sql)
{
DataTable dt = new DataTable();
try
{
Logging.LogDebug("Creating SQLiteConnection...");
using (SQLiteConnection cnn = new SQLiteConnection(dbConnection))
{
Logging.LogDebug("SQLiteConnection Created!");
Logging.LogDebug("SQLiteConnection Opening...");
cnn.Open();
Logging.LogDebug("SQLiteConnection Opened!");
Logging.LogDebug("SQLiteCommand Creating...");
using (SQLiteCommand mycommand = new SQLiteCommand(cnn))
{
Logging.LogDebug("SQLiteCommand Created!");
mycommand.CommandText = sql;
Logging.LogDebug("SQLiteCommand executing reader...");
using (SQLiteDataReader reader = mycommand.ExecuteReader())
{
Logging.LogDebug("SQLiteCommand executed reader!");
Logging.LogDebug("DataTable Loading...");
dt.Load(reader);
Logging.LogDebug("DataTable Loaded!");
reader.Close();
}
}
cnn.Close();
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return dt;
}
Run Code Online (Sandbox Code Playgroud)
当然dbConnection是连接字符串,由以下函数设置. inputFile只是要打开的文件名的字符串路径.
public SqLiteDatabase(String inputFile)
{
dbConnection = String.Format("Data Source={0}", inputFile);
}
Run Code Online (Sandbox Code Playgroud)
在这一点上,我认为sql是无关紧要的,因为当cnn.Open()停止时,它没有达到这一点.
好的,我已经做了一些测试.在本地运行测试,它在~5秒内完成1000次迭代循环,每次调用约5ms cnn.Open().从我在本地PC上执行的相同Windows安装程序运行测试,它在大约25分钟内完成,平均每次调用1468ms cnn.Open().
我做了一个小测试程序,只调用TestOpenConn()服务程序中的函数(在Windows服务中运行的相同的代码),针对位于测试目录中的文件副本运行.在服务器或我的本地PC上运行它会产生可接受的性能(服务器上每次呼叫1.95ms,本地PC上每次呼叫4ms):
namespace EGC_Timing_Test
{
class Program
{
static void Main(string[] args)
{
Logging.Init("log4net.xml", "test.log");
var db = new SqLiteDatabase("config.sqlite");
db.TestOpenConn();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是测试功能:
public void TestOpenConn()
{
// TODO: Remove this after testing loop of opening / closing SQLite DB repeatedly:
const int iterations = 1000;
Logging.LogDebug(String.Format("Running TestOpenConn for {0} opens...", iterations));
var startTime = DateTime.Now;
for (var i = 0; i < iterations; i++)
{
using (SQLiteConnection cnn = new SQLiteConnection(dbConnection))
{
Logging.LogDebug(String.Format("SQLiteConnection Opening, iteration {0} of {1}...", i, iterations));
var startTimeInner = DateTime.Now;
cnn.Open();
var endTimeInner = DateTime.Now;
var diffTimeInner = endTimeInner - startTimeInner;
Logging.LogDebug(String.Format("SQLiteConnection Opened in {0}ms!", diffTimeInner.TotalMilliseconds));
cnn.Close();
}
}
var endTime = DateTime.Now;
var diffTime = endTime - startTime;
Logging.LogDebug(String.Format("Done running TestOpenConn for {0} opens!", iterations));
Logging.LogInfo(String.Format("{0} iterations total:\t{1}", iterations, diffTime));
Logging.LogInfo(String.Format("{0} iterations average:\t{1}ms", iterations, diffTime.TotalMilliseconds/iterations));
}
Run Code Online (Sandbox Code Playgroud)
我想我的问题暂时解决了......我将我的服务和测试应用程序更改为SYSTEM帐户而不是NetworkService帐户.更改用户帐户的好处是否会持续存在,或者只是暂时性的,还有待观察.
| 归档时间: |
|
| 查看次数: |
2554 次 |
| 最近记录: |