-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
=core Make use of statefulMap instead of statefulMapConcat.
- Loading branch information
Showing
6 changed files
with
193 additions
and
29 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
http-bench-jmh/src/main/scala/org/apache/pekko/BenchTestSource.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* license agreements; and to You under the Apache License, version 2.0: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* This file is part of the Apache Pekko project, which was derived from Akka. | ||
*/ | ||
|
||
/* | ||
* Copyright (C) 2016-2022 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package org.apache.pekko | ||
|
||
import org.apache.pekko | ||
import pekko.stream.Attributes | ||
import pekko.stream.Outlet | ||
import pekko.stream.SourceShape | ||
import pekko.stream.stage.GraphStage | ||
import pekko.stream.stage.GraphStageLogic | ||
import pekko.stream.stage.OutHandler | ||
|
||
/** | ||
* Emits integers from 1 to the given `elementCount`. The `java.lang.Integer` | ||
* objects are allocated in the constructor of the operator, so it should be created | ||
* before the benchmark is started. | ||
*/ | ||
class BenchTestSource(elementCount: Int) extends GraphStage[SourceShape[java.lang.Integer]] { | ||
|
||
private val elements = new Array[java.lang.Integer](elementCount) | ||
(1 to elementCount).foreach(n => elements(n - 1) = n) | ||
|
||
val out: Outlet[java.lang.Integer] = Outlet("BenchTestSource") | ||
override val shape: SourceShape[java.lang.Integer] = SourceShape(out) | ||
|
||
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = | ||
new GraphStageLogic(shape) with OutHandler { | ||
|
||
var n = 0 | ||
|
||
override def onPull(): Unit = { | ||
n += 1 | ||
if (n > elementCount) | ||
complete(out) | ||
else | ||
push(out, elements(n - 1)) | ||
} | ||
|
||
setHandler(out, this) | ||
} | ||
} | ||
|
||
class BenchTestSourceSameElement[T](elements: Int, elem: T) extends GraphStage[SourceShape[T]] { | ||
|
||
val out: Outlet[T] = Outlet("BenchTestSourceSameElement") | ||
override val shape: SourceShape[T] = SourceShape(out) | ||
|
||
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = | ||
new GraphStageLogic(shape) with OutHandler { | ||
|
||
var n = 0 | ||
|
||
override def onPull(): Unit = { | ||
n += 1 | ||
if (n > elements) | ||
complete(out) | ||
else | ||
push(out, elem) | ||
} | ||
|
||
setHandler(out, this) | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
...mh/src/main/scala/org/apache/pekko/http/impl/engine/MessageToFrameRendererBenchmark.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.pekko.http.impl.engine | ||
|
||
import com.typesafe.config.ConfigFactory | ||
|
||
import org.apache.pekko | ||
import pekko.{ BenchTestSourceSameElement, NotUsed } | ||
import pekko.actor.ActorSystem | ||
import pekko.http.impl.engine.ws.FrameEvent | ||
import pekko.http.impl.engine.ws.Protocol.Opcode | ||
import pekko.stream.scaladsl.{ Flow, Keep, Sink, Source } | ||
import pekko.util.ByteString | ||
import org.openjdk.jmh.annotations.{ | ||
Benchmark, | ||
BenchmarkMode, | ||
Mode, | ||
OperationsPerInvocation, | ||
OutputTimeUnit, | ||
Scope, | ||
State, | ||
TearDown | ||
} | ||
|
||
import java.util.concurrent.TimeUnit | ||
import scala.concurrent.Await | ||
import scala.concurrent.duration.{ Duration, DurationInt } | ||
|
||
object MessageToFrameRendererBenchmark { | ||
final val OperationsPerInvocation = 100000 | ||
} | ||
|
||
@State(Scope.Benchmark) | ||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
@BenchmarkMode(Array(Mode.Throughput)) | ||
class MessageToFrameRendererBenchmark { | ||
import MessageToFrameRendererBenchmark.OperationsPerInvocation | ||
|
||
private val config = ConfigFactory.parseString(""" | ||
akka.actor.default-dispatcher { | ||
executor = "fork-join-executor" | ||
fork-join-executor { | ||
parallelism-factor = 1 | ||
} | ||
} | ||
""") | ||
|
||
private implicit val system: ActorSystem = ActorSystem("MessageToFrameRendererBenchmark", config) | ||
|
||
@TearDown | ||
def shutdown(): Unit = { | ||
Await.result(system.terminate(), 5.seconds) | ||
} | ||
|
||
def createSource(count: Int): Source[ByteString, NotUsed] = | ||
Source.fromGraph(new BenchTestSourceSameElement(count, ByteString.empty)) | ||
|
||
private val newstreamedFrames = createSource(OperationsPerInvocation) | ||
.statefulMap(() => true)((isFirst, data) => { | ||
val frameOpcode = if (isFirst) Opcode.Pong else Opcode.Continuation | ||
(false, FrameEvent.fullFrame(frameOpcode, None, data, fin = false)) | ||
}, _ => None) | ||
.toMat(Sink.ignore)(Keep.right) | ||
|
||
private val oldstreamedFrames = createSource(OperationsPerInvocation) | ||
.via(statefulMap(() => { | ||
var isFirst = true | ||
|
||
{ data => | ||
val frameOpcode = | ||
if (isFirst) { | ||
isFirst = false | ||
Opcode.Pong | ||
} else Opcode.Continuation | ||
|
||
FrameEvent.fullFrame(frameOpcode, None, data, fin = false) | ||
} | ||
})) | ||
.toMat(Sink.ignore)(Keep.right) | ||
|
||
def statefulMap[T, U](functionConstructor: () => T => U): Flow[T, U, NotUsed] = | ||
Flow[T].statefulMapConcat { () => | ||
val f = functionConstructor() | ||
i => f(i) :: Nil | ||
} | ||
|
||
@Benchmark | ||
@OperationsPerInvocation(OperationsPerInvocation) | ||
def benchOldStreamedFrames(): Unit = | ||
Await.result(oldstreamedFrames.run(), Duration.Inf) | ||
|
||
@Benchmark | ||
@OperationsPerInvocation(OperationsPerInvocation) | ||
def benchNewStreamedFrames(): Unit = | ||
Await.result(newstreamedFrames.run(), Duration.Inf) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters