公司现在的搜索服务RESTful架构一直是使用java + jetty; jetty的简单和高效一直很让人喜欢,搜索的RESTful架构两年来一直很稳定;但是软负载keepalived上想添加些业务的控制模块,可是keepalived没有专门的开发接口,自己新开发一套?对成本和性能、稳定性要求很高,考虑过scala 、lisp 、c 都觉得不合适,需要开发周期,性能都要考虑进去,go语言出来也两年多了,所以去关注了一下;下面是go对比jetty的性能测试。(注:go 语言的web没有服务容器这个概念,不需要apache 或nginx 等等,至少先阶段是这样)
测试的内容很简单,go 和java都是一个"hello world "的web输出
go代码
package main import ( "fmt" "log" "net/http" "strings" ) func sayhelloName(w http.ResponseWriter, r *http.Request) { r.ParseForm() //参数解析 for k, v := range r.Form { fmt.Println("key:", k) fmt.Println("value:", strings.Join(v, "")) } fmt.Fprintf(w, "hello astaxie") } func main() { http.HandleFunc("/", sayhelloName) err := http.ListenAndServe(":9090", nil) if err != nil { log.Fatal("listenAndServer", err) } }
java代码
package com.search5173.jetty.test; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.server.nio.SelectChannelConnector; import org.eclipse.jetty.util.thread.QueuedThreadPool; public class OneContext { public static void main(String[] args) throws Exception { QueuedThreadPool threadPool= new QueuedThreadPool(); threadPool.setMaxThreads(11); threadPool.setMinThreads(4); Server server = new Server(); server.setThreadPool(threadPool); Connector connector=new SelectChannelConnector(); connector.setPort(8081); server.setConnectors(new Connector[]{connector}); ContextHandler context = new ContextHandler(); context.setContextPath("/t"); context.setResourceBase("."); context.setClassLoader(Thread.currentThread().getContextClassLoader()); server.setHandler(context); Handler handler=new HelloHandler(); context.setHandler(handler); server.start(); server.join(); } public static class HelloHandler extends AbstractHandler { @Override public void handle(String arg0, Request request1, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.getWriter().println("<h1>Hellodfgdfgdfgfd OneContext</h1>"); } } }
测试方式 apache ab, 这个简单方便
测试硬件
Pentium(R) Dula-Core CPU E5200 @2.50GHz
内存:3.25GB
操作系统:32位 (win7)
测试地址
1) go--->http://localhost:9090/rwer
2) java--->http://localhost:8081/t
测试脚本一
1) ab -n 50 -c 10 http://localhost:9090/rwer
2) ab -n 50 -c 10 http://localhost:8081/t
Requests per second: 结果:
jetty | go |
1311.44 | 2265.21 |
1083.40 | 2491.78 |
1423.85 | 1607.61 |
可以说在各方面go都完胜,知道go他强,但是没想到这么强,冲击很大。
再看些其他的
go:
jetty