使用Scala 2.12.2,Akka 2.5.1,Akka Http 10.0.6
def main(args: Array[String]): Unit = {
implicit val actorSystem = ActorSystem("system")
implicit val actorMaterializer = ActorMaterializer()
val route =
pathSingleSlash {
get {
complete {
"Hello World!"
}
}
}
val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)
val serverBinding = Await.result(bindingFuture, Duration.Inf)
println(s"Bind complete. Server running and listening at ${serverBinding.localAddress}")
scala.io.StdIn.readLine()
Await.result(serverBinding.unbind(), Duration.Inf)
Await.result(actorSystem.terminate(), Duration.Inf)
print("Exiting.")
}
我使用wrk bencharking工具
wrk -t4 -c64 -d60s --latency http://localhost:8080
Running 1m test @ http://localhost:8080
4 threads and 64 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 6.14ms 29.25ms 826.31ms 96.70%
Req/Sec 13.86k 4.88k 43.81k 69.37%
Latency Distribution
50% 835.00us
75% 1.24ms
90% 11.57ms
99% 87.31ms
3278911 requests in 1.00m, 484.69MB read
Requests/sec: 54607.92
Transfer/sec: 8.07MB
性能不错,但其他框架和语言做得更好。有没有办法改善这个简单案例的Akka Http表现?
我看到一个非常类似的线程,它以阻塞Thread.sleep操作为中心。我现在专注于更简单的非阻塞用例。
为了比较,这个简单的go程序:
package main
import (
"io"
"net/http"
)
func hello(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello World!")
}
func main() {
http.HandleFunc("/", hello)
http.ListenAndServe(":8080", nil)
}
更快:
wrk -t4 -c64 -d60s http://localhost:8080 --latency
Running 1m test @ http://localhost:8080
4 threads and 64 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.04ms 2.12ms 151.99ms 99.92%
Req/Sec 16.02k 1.32k 19.55k 58.33%
Latency Distribution
50% 0.96ms
75% 1.10ms
90% 1.16ms
99% 1.34ms
3824550 requests in 1.00m, 470.51MB read
Requests/sec: 63740.53
Transfer/sec: 7.84MB
这些数字在我的MacBook上运行。我在运行Debian 8的AWS c4.4xlarge
EC2实例上运行了相同的测试,并且包括akka,Go和其他一些组合在内的所有REST测试都比我的笔记本电脑运行得更快(我希望如此)。我没有方便的数字。