iris(go)和.netcore的web速度测试和比拟
近期在开发一个抢口罩的系统,类似于电商常见的秒杀系统。其时选型的的时候筹备在netcore和golang之间选择一个作为系统的开发语言,网上的说法各异,有的说iris快,有的说.netcore快。于是决定本身做下测试。
iris在go的web开发框架中长短常风行的,它本身的介绍是最快的go语言web框架,这个必定有一家之言的身分,但是说它是最快的go框架之一必定没有问题。使用的iris的版本是12.1.8
aspnetcore 就是微软在.netcore中开发的标准框架,不需要用到其他第三方框架。这里使用的.netcore版本是3.0
iris的代码为:
// package main // import "github.com/gin-gonic/gin" // func main() { // r := gin.Default() // r.GET("/ping", func(c *gin.Context) { // c.JSON(200, gin.H{ // "message": "pong", // }) // }) // r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") // } package main import ( "github.com/kataras/iris" "github.com/kataras/iris/context" //"strings" "math/rand" ) const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 " func randomString(n int) string { b := make([]byte, n) for i := range b { b[i] = letterBytes[rand.Intn(len(letterBytes))] } return string(b) } func main() { app := iris.New() //txt := strings.Repeat("g", 10000) app.Get("/api/values/{id}", func(ctx context.Context) { txt := randomString(1024) ctx.WriteString(txt) }) app.Run(iris.Addr(":5000")) }
aspnetcore的代码为:(吐槽一下,微软整的代码太多,让人有点添堵)
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseKestrel(options => { options.Listen(IPAddress.Any, 5080); //HTTP port }); webBuilder.UseStartup<Startup>(); }).ConfigureLogging(a => a.SetMinimumLevel(LogLevel.None)); }
public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { } const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "; static string RandomString(int length) { var random = new Random(DateTime.Now.Millisecond); var bytes = new Char[length]; for (var i = 0; i < length; i++) { bytes[i] = chars[random.Next(chars.Length)]; } return new string(bytes); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapGet("/", async context => { var txt = RandomString(1024); await context.Response.WriteAsync(txt); // await context.Response.WriteAsync("Hello World!"); }); }); } }
windows下的测试:
iris: 通过 go build 进行编译,然后运行。启用 bombardier测试。功效如下
bombardier.exe -c 50 -n 300000 :5000/api/values/id
Bombarding :5000/api/values/id with 300000 request(s) using 50 connection(s)
300000 / 300000 [=================================================================================] 100.00% 8798/s 34s
Done!
Statistics
Avg
Stdev
Max
Reqs/sec
8860.00 1065.69 38087.60
Latency
5.65ms 187.86us 31.91ms
HTTP codes:
1xx - 0, 2xx - 300000, 3xx - 0, 4xx - 0, 5xx - 0
others - 0
Throughput: 10.27MB/s
aspnetcore: dotnet publish -c Release 编译。功效如下
Bombarding :5080 with 300000 request(s) using 50 connection(s)
300000 / 300000 [=================================================================================] 100.00% 34749/s 8s
Done!
Statistics
Avg
Stdev
Max
Reqs/sec
35283.26 6100.68 45069.66
Latency
1.41ms
8.08ms
1.06s
HTTP codes:
1xx - 0, 2xx - 300000, 3xx - 0, 4xx - 0, 5xx - 0
others - 0
Throughput: 40.35MB/s
功效让人大跌眼镜。.netcore比iris 的qps快了4倍以上。(注意上面代码,我移除了.netcore的日志)
因为是windows平台的功效,我怀疑go语言在windows没有优化,所以后来插手了linux amd64的测试
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/30025.html