.NET 8 编写 LiteDB vs SQLite 数据库 CRUD 接口性能测试(测试篇)
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using LiteDB;
using Microsoft.Data.Sqlite;
using System;
using System.Collections.Generic;
using System.IO;
public class LiteDBVsSQLiteCRUD
{
private string _databaseFile = "litedbtest.db";
private string _connectionString = "Data Source=sqlite.db;Version=3;";
// LiteDB 初始化和清理
[GlobalSetup(Target = nameof(LiteDBInsert))]
public void SetupLiteDB()
{
using (var db = new LiteDatabase(_databaseFile))
{
var col = db.GetCollection<Person>("people");
col.DeleteAll();
}
}
[GlobalCleanup(Target = nameof(LiteDBInsert))]
public void CleanupLiteDB()
{
File.Delete(_databaseFile);
}
// SQLite 初始化和清理
[GlobalSetup(Target = nameof(SQLiteInsert))]
public void SetupSQLite()
{
using (var connection = new SqliteConnection(_connectionString))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = @"
CREATE TABLE people (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER NOT NULL
);";
command.ExecuteNonQuery();
connection.Close();
}
}
[GlobalCleanup(Target = nameof(SQLiteInsert))]
public void CleanupSQLite()
{
using (var connection = new SqliteConnection(_connectionString))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = "DROP TABLE people;";
command.ExecuteNonQuery();
connection.Close();
}
}
[Benchmark]
public void LiteDBInsert()
{
using (var db = new LiteDatabase(_databaseFile))
{
var col = db.GetCollection<Person>("people");
for (int i = 0; i < 100; i++)
{
col.Insert(new Person { Name = "Name" + i, Age = 30 + i });
}
}
}
[Benchmark]
public void SQLiteInsert()
{
using (var connection = new SqliteConnection(_connectionString))
{
connection.Open();
var command = connection.CreateCommand();
for (int i = 0; i < 100; i++
评论已关闭