【发布时间】:2016-10-10 01:33:33
【问题描述】:
我创建了一个小的 asp.net core mvc 项目来测试 asp.net 应用程序在 Linux (Ubuntu 16.04) 上的速度。
我创建了与 AspUser 类(存储在 PostgreSQL 数据库中)一起使用的 CRUD 控制器。当我调用显示存储用户列表的 index 方法时,剃刀渲染非常慢。渲染内容需要 2 秒(这不是第一次调用 - 第一次调用需要 8 秒)。数据库中有 1000 个用户。
wrk 实用程序 wrk -c 256 -t 32 -d 10 http://localhost:5000/aspusers 显示每秒 2,6 个请求。 我在 nodejs 中做了同样的例子,wrk 实用程序每秒显示 20 个请求。
硬件:Amd FX 8150 8 核,3.6 GHz,8 GB RAM
有人知道为什么视图渲染这么慢吗?
型号:
[Table("asp_users")]
public class AspUser
{
[Column("id")]
public long Id { get; set; }
[Column("first_name")]
[Required]
public string FirstName { get; set; }
[Column("last_name")]
public string LastName { get; set; }
[Column("age")]
public int Age { get; set; }
[Column("created_at")]
public DateTime CreatedAt { get; set; }
[Column("updated_at")]
public DateTime UpdatedAt { get; set; }
}
控制器:
public class AspUsersController : Controller
{
public async Task<IActionResult> Index()
{
var users = await _context.AspUsers.OrderBy(a => a.Age).ToListAsync();
return View(users);
}
}
查看:
@model IEnumerable<PostgresTest.Models.AspUser>
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Age)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.CreatedAt)
</th>
<th>
@Html.DisplayNameFor(model => model.UpdatedAt)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@item.Age
</td>
<td>
@item.FirstName
</td>
<td>
@item.LastName
</td>
<td>
@item.CreatedAt
</td>
<td>
@item.UpdatedAt
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
【问题讨论】:
-
在开发机器(Win7 x64,dotnet core tools + SDK)上足够有趣,它运行得非常快,但在 Win Server 2012R2(只安装了 DotNetCore.1.0.1-WindowsHosting)上它非常慢。
标签: asp.net-mvc razor