Skip to content

Instantly share code, notes, and snippets.

@olga-gorun
Last active October 8, 2016 18:08
Show Gist options
  • Save olga-gorun/825e823027aadb2962b3b7229e4d5d48 to your computer and use it in GitHub Desktop.
Save olga-gorun/825e823027aadb2962b3b7229e4d5d48 to your computer and use it in GitHub Desktop.
spray and akka-http performance decreased on static response length change
package com.anyclip.benchmark_akka_http
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Route
import akka.stream.scaladsl.Sink
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import com.anyclip.openrtb.BidRequest
import com.typesafe.config.{Config, ConfigFactory}
import scala.concurrent.ExecutionContext
trait Core {
implicit val system: ActorSystem = ActorSystem("Suggestion")
implicit val materializer: ActorMaterializer = ActorMaterializer()
implicit val ec: ExecutionContext = system.dispatcher
}
object AkkaHttpStarter extends App{
val staticresponse =
"""
|{
| "id": "4725d328edf011e49ebd11250708394f",
| "bidid": "spotx_4725d328edf011e49ebd11250708394f_10048",
| "seatbid": [
| {
| "bid": [
| {
| "id": "spotx_4725d328edf011e49ebd11250708394f_10048",
| "impid": "1",
| "price": 32,
| "adid": "10040",
| "adm": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><VAST version=\"2.0\"><Ad id=\"10048\"><InLine><AdSystem version=\"1.0\">AnyClip AdManager</AdSystem><AdTitle>VPAID WRAPPER</AdTitle><Creatives><Creative sequence=\"1\"><Linear><Duration>00:00:30</Duration><MediaFiles><MediaFile delivery=\"progressive\" bitrate=\"0\" width=\"400\" height=\"300\" type=\"application/javascript\" apiFramework=\"VPAID\"><![CDATA[//player.anyclip.com/anyclip-widget/qa/v1/js/ac_manager.js?sadpc=0&sessionId=4725d328edf011e49ebd11250708394f&apptst=newacwidget&uid=spotx_4725d328edf011e49ebd11250708394f_10048&adsn=0&adap=1&adm=ac&purl=http%3A%2F%2Fcbs.com&w=300&h=250&ad_tag=$[ad_tag]&campaign=1550&ad=10048&creative=$[creative]&publisherId=sx&bidValue=32000&price=32000&bidRequest=4725d328edf011e49ebd11250708394f&adpl=%5B%7B%22src%22%3A%22http%3A%2F%2Fcdn1.anyclip.com%2F0kUP7uJmhJun.mp4%22%2C%22poster%22%3A%22%24%5Bcreative_poster%5D%22%2C%22link%22%3A%22http%3A%2F%2Fwww.google.com%22%2C%22type%22%3A%22mp4%22%7D%5D&userid=0ee5d1d1d1be290e269ceec71f45b5e7&networkUserId=0ee5d1d1d1be290e269ceec71f45b5e7&thrdp=%7B%22AdImpression%22%3A%22%22%2C%22AdVideoFirstQuartile%22%3A%22%22%2C%22AdVideoMidpoint%22%3A%22%22%2C%22AdVideoThirdQuartile%22%3A%22%22%2C%22AdVideoComplete%22%3A%22%22%2C%22AnyClipDMP%22%3A%22http%3A%2F%2Fbcp.crwdcntrl.net%2Fmap%2Fc%3D9642%2Ftp%3DANYC%2Ftpid%3D0ee5d1d1d1be290e269ceec71f45b5e7%22%2C%22AnyClipEventParameters%22%3A%5B%22publisherId%22%2C%22apptst%22%2C%22userid%22%2C%22networkUserId%22%2C%22bidRequest%22%2C%22sessionId%22%5D%2C%22AnyClipImpressionEvent%22%3A%22http%3A%2F%2Fstats.anyclip.com%2Ffictivious.png%3Fcampaign%3D1550%26ad%3D10048%26creative%3D%24%5Bcreative%5D%26action%3Dimp%26publisherId%3Dsx%26bidValue%3D32000%26price%3D%24MBR%26bidRequest%3D4725d328edf011e49ebd11250708394f%26bidResponse%3Dspotx_4725d328edf011e49ebd11250708394f_10048%26apptst%3Dnewacwidget%26userid%3D0ee5d1d1d1be290e269ceec71f45b5e7%26sessionId%3D4725d328edf011e49ebd11250708394f%26PageURL%3Dhttp%253A%252F%252Fcbs.com%26networkUserId%3D0ee5d1d1d1be290e269ceec71f45b5e7%22%7D]]></MediaFile></MediaFiles><AdParameters/></Linear></Creative></Creatives></InLine></Ad></VAST>",
| "adomain": [
| "anyclip.com"
| ],
| "cid": "1550",
| "crid": "3",
| "attr": [],
| "ext": {
| "external-id": 0,
| "priority": 2
| }
| }
| ],
| "seat": "AC-1",
| "group": 0
| }
| ],
| "cur": "USD"
|}
""".stripMargin
val http = new AkkaHttp with Core{
override def config: Config = ConfigFactory.load()
}
http.start()
}
trait AkkaHttp {
this: Core =>
def config: Config
def start() = {
val route: Route = {
path("api" / "static") {
get {
complete(AkkaHttpStarter.staticresponse)
}
} ~
path("api" / "staticsmall"){
get{
complete("response")
}
}
}
Http().bind(
interface = config.getString("frontend.interface"),
port = config.getInt("frontend.port")
).to(Sink.foreach { conn =>
conn.flow.join(route).run()
}).run()
}
}
package com.anyclip.benchmark_scalatra_api.controllers
import org.scalatra.ScalatraServlet
/**
* Created by olga on 10/7/16.
*/
class GreetingController extends ScalatraServlet {
get("/staticsmall") {
"response"
}
get("/static") {
GreetingController.staticresponse
}
}
object GreetingController {
val staticresponse =
"""
|{
| "id": "4725d328edf011e49ebd11250708394f",
| "bidid": "spotx_4725d328edf011e49ebd11250708394f_10048",
| "seatbid": [
| {
| "bid": [
| {
| "id": "spotx_4725d328edf011e49ebd11250708394f_10048",
| "impid": "1",
| "price": 32,
| "adid": "10040",
| "adm": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><VAST version=\"2.0\"><Ad id=\"10048\"><InLine><AdSystem version=\"1.0\">AnyClip AdManager</AdSystem><AdTitle>VPAID WRAPPER</AdTitle><Creatives><Creative sequence=\"1\"><Linear><Duration>00:00:30</Duration><MediaFiles><MediaFile delivery=\"progressive\" bitrate=\"0\" width=\"400\" height=\"300\" type=\"application/javascript\" apiFramework=\"VPAID\"><![CDATA[//player.anyclip.com/anyclip-widget/qa/v1/js/ac_manager.js?sadpc=0&sessionId=4725d328edf011e49ebd11250708394f&apptst=newacwidget&uid=spotx_4725d328edf011e49ebd11250708394f_10048&adsn=0&adap=1&adm=ac&purl=http%3A%2F%2Fcbs.com&w=300&h=250&ad_tag=$[ad_tag]&campaign=1550&ad=10048&creative=$[creative]&publisherId=sx&bidValue=32000&price=32000&bidRequest=4725d328edf011e49ebd11250708394f&adpl=%5B%7B%22src%22%3A%22http%3A%2F%2Fcdn1.anyclip.com%2F0kUP7uJmhJun.mp4%22%2C%22poster%22%3A%22%24%5Bcreative_poster%5D%22%2C%22link%22%3A%22http%3A%2F%2Fwww.google.com%22%2C%22type%22%3A%22mp4%22%7D%5D&userid=0ee5d1d1d1be290e269ceec71f45b5e7&networkUserId=0ee5d1d1d1be290e269ceec71f45b5e7&thrdp=%7B%22AdImpression%22%3A%22%22%2C%22AdVideoFirstQuartile%22%3A%22%22%2C%22AdVideoMidpoint%22%3A%22%22%2C%22AdVideoThirdQuartile%22%3A%22%22%2C%22AdVideoComplete%22%3A%22%22%2C%22AnyClipDMP%22%3A%22http%3A%2F%2Fbcp.crwdcntrl.net%2Fmap%2Fc%3D9642%2Ftp%3DANYC%2Ftpid%3D0ee5d1d1d1be290e269ceec71f45b5e7%22%2C%22AnyClipEventParameters%22%3A%5B%22publisherId%22%2C%22apptst%22%2C%22userid%22%2C%22networkUserId%22%2C%22bidRequest%22%2C%22sessionId%22%5D%2C%22AnyClipImpressionEvent%22%3A%22http%3A%2F%2Fstats.anyclip.com%2Ffictivious.png%3Fcampaign%3D1550%26ad%3D10048%26creative%3D%24%5Bcreative%5D%26action%3Dimp%26publisherId%3Dsx%26bidValue%3D32000%26price%3D%24MBR%26bidRequest%3D4725d328edf011e49ebd11250708394f%26bidResponse%3Dspotx_4725d328edf011e49ebd11250708394f_10048%26apptst%3Dnewacwidget%26userid%3D0ee5d1d1d1be290e269ceec71f45b5e7%26sessionId%3D4725d328edf011e49ebd11250708394f%26PageURL%3Dhttp%253A%252F%252Fcbs.com%26networkUserId%3D0ee5d1d1d1be290e269ceec71f45b5e7%22%7D]]></MediaFile></MediaFiles><AdParameters/></Linear></Creative></Creatives></InLine></Ad></VAST>",
| "adomain": [
| "anyclip.com"
| ],
| "cid": "1550",
| "crid": "3",
| "attr": [],
| "ext": {
| "external-id": 0,
| "priority": 2
| }
| }
| ],
| "seat": "AC-1",
| "group": 0
| }
| ],
| "cur": "USD"
|}
""".stripMargin
}
package com.anyclip.spray_benchmark
import akka.actor.{Actor, ActorSystem, Props}
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import java.util.concurrent.TimeUnit
import com.anyclip.openrtb.BidRequest
import spray.can.Http
import spray.http.ContentTypes._
import spray.http.HttpHeaders._
import spray.routing.HttpService
class MyServiceActor extends Actor with MyService {
def actorRefFactory = context
def receive = runRoute(myRoute)
}
object MyServiceActor {
val staticresponse =
"""
|{
| "id": "4725d328edf011e49ebd11250708394f",
| "bidid": "spotx_4725d328edf011e49ebd11250708394f_10048",
| "seatbid": [
| {
| "bid": [
| {
| "id": "spotx_4725d328edf011e49ebd11250708394f_10048",
| "impid": "1",
| "price": 32,
| "adid": "10040",
| "adm": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><VAST version=\"2.0\"><Ad id=\"10048\"><InLine><AdSystem version=\"1.0\">AnyClip AdManager</AdSystem><AdTitle>VPAID WRAPPER</AdTitle><Creatives><Creative sequence=\"1\"><Linear><Duration>00:00:30</Duration><MediaFiles><MediaFile delivery=\"progressive\" bitrate=\"0\" width=\"400\" height=\"300\" type=\"application/javascript\" apiFramework=\"VPAID\"><![CDATA[//player.anyclip.com/anyclip-widget/qa/v1/js/ac_manager.js?sadpc=0&sessionId=4725d328edf011e49ebd11250708394f&apptst=newacwidget&uid=spotx_4725d328edf011e49ebd11250708394f_10048&adsn=0&adap=1&adm=ac&purl=http%3A%2F%2Fcbs.com&w=300&h=250&ad_tag=$[ad_tag]&campaign=1550&ad=10048&creative=$[creative]&publisherId=sx&bidValue=32000&price=32000&bidRequest=4725d328edf011e49ebd11250708394f&adpl=%5B%7B%22src%22%3A%22http%3A%2F%2Fcdn1.anyclip.com%2F0kUP7uJmhJun.mp4%22%2C%22poster%22%3A%22%24%5Bcreative_poster%5D%22%2C%22link%22%3A%22http%3A%2F%2Fwww.google.com%22%2C%22type%22%3A%22mp4%22%7D%5D&userid=0ee5d1d1d1be290e269ceec71f45b5e7&networkUserId=0ee5d1d1d1be290e269ceec71f45b5e7&thrdp=%7B%22AdImpression%22%3A%22%22%2C%22AdVideoFirstQuartile%22%3A%22%22%2C%22AdVideoMidpoint%22%3A%22%22%2C%22AdVideoThirdQuartile%22%3A%22%22%2C%22AdVideoComplete%22%3A%22%22%2C%22AnyClipDMP%22%3A%22http%3A%2F%2Fbcp.crwdcntrl.net%2Fmap%2Fc%3D9642%2Ftp%3DANYC%2Ftpid%3D0ee5d1d1d1be290e269ceec71f45b5e7%22%2C%22AnyClipEventParameters%22%3A%5B%22publisherId%22%2C%22apptst%22%2C%22userid%22%2C%22networkUserId%22%2C%22bidRequest%22%2C%22sessionId%22%5D%2C%22AnyClipImpressionEvent%22%3A%22http%3A%2F%2Fstats.anyclip.com%2Ffictivious.png%3Fcampaign%3D1550%26ad%3D10048%26creative%3D%24%5Bcreative%5D%26action%3Dimp%26publisherId%3Dsx%26bidValue%3D32000%26price%3D%24MBR%26bidRequest%3D4725d328edf011e49ebd11250708394f%26bidResponse%3Dspotx_4725d328edf011e49ebd11250708394f_10048%26apptst%3Dnewacwidget%26userid%3D0ee5d1d1d1be290e269ceec71f45b5e7%26sessionId%3D4725d328edf011e49ebd11250708394f%26PageURL%3Dhttp%253A%252F%252Fcbs.com%26networkUserId%3D0ee5d1d1d1be290e269ceec71f45b5e7%22%7D]]></MediaFile></MediaFiles><AdParameters/></Linear></Creative></Creatives></InLine></Ad></VAST>",
| "adomain": [
| "anyclip.com"
| ],
| "cid": "1550",
| "crid": "3",
| "attr": [],
| "ext": {
| "external-id": 0,
| "priority": 2
| }
| }
| ],
| "seat": "AC-1",
| "group": 0
| }
| ],
| "cur": "USD"
|}
""".stripMargin
}
// this trait defines our service behavior independently from the service actor
trait MyService extends HttpService {
implicit val timeout = Timeout(300, TimeUnit.MILLISECONDS)
val myRoute =
path("api" / "static") {
get {
complete(MyServiceActor.staticresponse)
}
} ~
path("api" / "staticsmall"){
get{
complete("response")
}
}
}
object Boot extends App {
implicit val system = ActorSystem("on-spray-can")
val service = system.actorOf(Props[MyServiceActor], "demo-service")
implicit val timeout = Timeout(5, TimeUnit.SECONDS)
IO(Http) ? Http.Bind(service, interface = "0.0.0.0", port = 8080)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment