package perftest.server

import com.sun.net.httpserver.HttpServer
import com.sun.net.httpserver.{HttpHandler, HttpExchange}
import java.net.InetSocketAddress

import ccf.transport.Response
import ccf.transport.json.JsonFormatter

import java.net.URL

class HttpRequestHandler extends HttpHandler {
  def handle(exchange: HttpExchange) { 
    try {
      val response = Response(Map[String, String](), Some((0 to 1023).map(x => 0).mkString("")))
      val body = JsonFormatter.formatResponse(response)
      exchange.sendResponseHeaders(200, body.length)
      exchange.getResponseBody.write(body.getBytes)
    } finally {
      exchange.getResponseBody.close
    }
  }
}

object Server {
  def run(url: URL)= { 
    val server = HttpServer.create(new InetSocketAddress(url.getHost, url.getPort), 0)
    server.createContext(url.getPath, new HttpRequestHandler)
    server.start
  }
}

package perftest.server

import java.net.InetSocketAddress

import org.eclipse.jetty.server.handler.AbstractHandler
import org.eclipse.jetty.server.{Server => Jetty7Server, Request, Handler, Connector}
import org.eclipse.jetty.server.nio.SelectChannelConnector
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import ccf.transport.Response
import ccf.transport.json.JsonFormatter

import java.net.URL

class HttpRequestHandler extends AbstractHandler {
  override def handle(target: String, req: Request, httpReq: HttpServletRequest, httpResp: HttpServletResponse) { 
    val response = Response(Map[String, String](), Some((0 to 1023).map(x => 0).mkString("")))
    val body = JsonFormatter.formatResponse(response)
    httpResp.setContentType("application/json")
    httpResp.setStatus(HttpServletResponse.SC_OK);
    httpResp.setContentLength(body.length)
    httpResp.getWriter.write(body)
    (httpReq.asInstanceOf[Request]).setHandled(true);
  }
}

object Server {
  def run(url: URL)= { 
    val server = new Jetty7Server(url.getPort)
    val connector = new SelectChannelConnector()
    connector.setPort(url.getPort)
    server.setConnectors(List[Connector](connector).toArray)
    server.setHandler(new HttpRequestHandler())
    server.start
  }
}

Replace Sun HTTP server with Jetty7
perftest: Replace Sun HTTP server with Jetty7

Sun HTTP server exhibits some severe performance issues causing a single
request-response cycle to take more than 200 ms on a Gigabit network
with a Windows client and Linux server. A HTTP server running Jetty7
brings down the request-response cycle time to 5 ms.

Signed-off-by: Harri Salokorpi <eefb23b8deb8c30dca252cd0ae751b67337dc69f@reaktor.fi>
Signed-off-by: Karim Osman <94396faf6cc817be76165477038b3cfa832e89a4@reaktor.fi>
