org.openjdk.jmh.annotations.BenchmarkMode Scala Examples

The following examples show how to use org.openjdk.jmh.annotations.BenchmarkMode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: StreamInputOutputBenchmark.scala    From scala-commons   with MIT License 5 votes vote down vote up
package com.avsystem.commons
package ser

import java.io.{ByteArrayInputStream, ByteArrayOutputStream, DataInputStream, DataOutputStream}

import com.avsystem.commons.serialization.{GenCodec, StreamInput, StreamOutput}
import org.openjdk.jmh.annotations.{Benchmark, BenchmarkMode, Fork, Measurement, Mode, Scope, State, Warmup}
import org.openjdk.jmh.infra.Blackhole


case class Toplevel(int: Int, nested: Nested, str: String)
case class Nested(list: List[Int], int: Int)

object Toplevel {
  implicit val nestedCodec: GenCodec[Nested] = GenCodec.materialize[Nested]
  implicit val codec: GenCodec[Toplevel] = GenCodec.materialize[Toplevel]
}

@Warmup(iterations = 10)
@Measurement(iterations = 20)
@Fork(1)
@BenchmarkMode(Array(Mode.Throughput))
@State(Scope.Thread)
class StreamInputOutputBenchmark {

  val something = Toplevel(35, Nested(List(121, 122, 123, 124, 125, 126), 53), "lol")

  val inputArray: Array[Byte] = {
    val os = new ByteArrayOutputStream()

    GenCodec.write(new StreamOutput(new DataOutputStream(os)), something)
    os.toByteArray
  }

  @Benchmark
  def testEncode(bh: Blackhole): Unit = {
    val os = new ByteArrayOutputStream(inputArray.length)
    val output = new StreamOutput(new DataOutputStream(os))
    GenCodec.write(output, something)
    bh.consume(os.toByteArray)
  }

  @Benchmark
  def testDecode(bh: Blackhole): Unit = {
    val is = new DataInputStream(new ByteArrayInputStream(inputArray))
    val input = new StreamInput(is)
    bh.consume(GenCodec.read[Toplevel](input))
  }

  @Benchmark
  def testEncodeRaw(bh: Blackhole): Unit = {
    val os = new ByteArrayOutputStream(inputArray.length)
    val output = new StreamOutput(new DataOutputStream(os))
    val toplevelOutput = output.writeObject()
    toplevelOutput.writeField("int").writeSimple().writeInt(35)
    val nestedOutput = toplevelOutput.writeField("nested").writeObject()
    val listOutput = nestedOutput.writeField("list").writeList()
    listOutput.writeElement().writeSimple().writeInt(121)
    listOutput.writeElement().writeSimple().writeInt(122)
    listOutput.writeElement().writeSimple().writeInt(123)
    listOutput.writeElement().writeSimple().writeInt(124)
    listOutput.writeElement().writeSimple().writeInt(125)
    listOutput.writeElement().writeSimple().writeInt(126)
    listOutput.finish()
    nestedOutput.writeField("int").writeSimple().writeInt(53)
    nestedOutput.finish()
    toplevelOutput.writeField("str").writeSimple().writeString("lol")
    toplevelOutput.finish()
    bh.consume(os.toByteArray)
  }

  @Benchmark
  def testDecodeRaw(bh: Blackhole): Unit = {
    val is = new DataInputStream(new ByteArrayInputStream(inputArray))
    val input = new StreamInput(is)
    val objInput = input.readObject()
    val intField = objInput.nextField().readSimple().readInt()
    val nestedInput = objInput.nextField().readObject()
    val listInput = nestedInput.nextField().readList()
    val listNested = List(
      listInput.nextElement().readSimple().readInt(),
      listInput.nextElement().readSimple().readInt(),
      listInput.nextElement().readSimple().readInt(),
      listInput.nextElement().readSimple().readInt(),
      listInput.nextElement().readSimple().readInt(),
      listInput.nextElement().readSimple().readInt()
    )
    listInput.hasNext
    val intNested = nestedInput.nextField().readSimple().readInt()
    nestedInput.hasNext
    val strField = objInput.nextField().readSimple().readString()
    objInput.hasNext
    bh.consume(Toplevel(intField, Nested(listNested, intNested), strField))
  }
} 
Example 2
Source File: OptionBenchmarks.scala    From Scala-High-Performance-Programming   with MIT License 5 votes vote down vote up
package highperfscala.option

import java.util.concurrent.TimeUnit

import org.openjdk.jmh.annotations.Mode._
import org.openjdk.jmh.annotations.{BenchmarkMode, _}

import scalaz.@@
import OptionBenchmarks._

@BenchmarkMode(Array(Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
@Warmup(iterations = 3, time = 5, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 30, time = 10, timeUnit = TimeUnit.SECONDS)
@Fork(value = 1, warmups = 1, jvmArgs = Array("-Xms1G", "-Xmx1G"))
class OptionBenchmarks {

  @Benchmark
  def scalaOption(s: OptionState): Option[ShareCount] = {
    val c = s.counter
    s.counter = s.counter + 1
    c % s.someFrequency match {
      case 0 => Some(ShareCount(s.counter))
      case _ => None
    }
  }

  @Benchmark
  def nullOption(s: OptionState): ShareCount @@ Opt = {
    OptOps.some(ShareCount(s.counter))
    val c = s.counter
    s.counter = s.counter + 1
    c % s.someFrequency match {
      case 0 => OptOps.some(ShareCount(s.counter))
      case _ => OptOps.none
    }
  }

  @Benchmark
  def nullOptionNoneReused(s: OptionState): ShareCount @@ Opt = {
    val c = s.counter
    s.counter = s.counter + 1
    c % s.someFrequency match {
      case 0 => OptOps.some(ShareCount(s.counter))
      case _ => OptionBenchmarks.noShareCount
    }
  }
}

object OptionBenchmarks {

  val noShareCount = OptOps.none[ShareCount]

  case class ShareCount(value: Long) extends AnyVal

  @State(Scope.Benchmark)
  class OptionState {

    @Param(Array("1", "2", "3", "5"))
    var someFrequency: Int = 0

    var counter: Long = 0
  }
} 
Example 3
package highperfscala.option

import java.util.concurrent.TimeUnit

import org.openjdk.jmh.annotations.Mode._
import org.openjdk.jmh.annotations.{BenchmarkMode, _}
import OptionCreationBenchmarks._

import scalaz.@@

@BenchmarkMode(Array(Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
@Warmup(iterations = 3, time = 5, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 30, time = 10, timeUnit = TimeUnit.SECONDS)
@Fork(value = 1, warmups = 1, jvmArgs = Array("-Xms1G", "-Xmx1G"))
class OptionCreationBenchmarks {

  @Benchmark
  def scalaSome(): Option[ShareCount] = Some(ShareCount(1))

  @Benchmark
  def scalaNone(): Option[ShareCount] = None

  @Benchmark
  def optSome(): ShareCount @@ Opt = OptOps.some(ShareCount(1))

  @Benchmark
  def optSomeWithNullChecking(): ShareCount @@ Opt =
    OptOps.nullCheckingSome(ShareCount(1))

  @Benchmark
  def optNone(): ShareCount @@ Opt = OptOps.none

  @Benchmark
  def optNoneReuse(): ShareCount @@ Opt = noShares
}

object OptionCreationBenchmarks {
  case class ShareCount(value: Long) extends AnyVal
  val noShares: ShareCount @@ Opt = OptOps.none
} 
Example 4
Source File: ListVectorBenchmarks.scala    From Scala-High-Performance-Programming   with MIT License 5 votes vote down vote up
package highperfscala.dataanalysis.benchmarks

import java.util.concurrent.TimeUnit

import highperfscala.dataanalysis.benchmarks.ListVectorBenchmarks.ExecutionList
import highperfscala.dataanalysis.util.DataCodec
import highperfscala.dataanalysis.{ListVectorExperiment, Midpoint, MinuteRollUp, Return}
import org.openjdk.jmh.annotations.Mode._
import org.openjdk.jmh.annotations.{BenchmarkMode, _}

@BenchmarkMode(Array(Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
@Warmup(iterations = 3, time = 5, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 30, time = 10, timeUnit = TimeUnit.SECONDS)
@Fork(value = 1, warmups = 1, jvmArgs = Array("-Xms1G", "-Xmx1G"))
class ListVectorBenchmarks {

  @Benchmark
  def computeReturnsWithList(s: ExecutionList): List[Return] = {
    ListVectorExperiment.computeReturnsWithList(
      MinuteRollUp(s.rollUp),
      s.list
    )
  }

  @Benchmark
  def computeReturnsWithVector(s: ExecutionList): Vector[Return] = {
    ListVectorExperiment.computeReturnsWithVector(
      MinuteRollUp(s.rollUp),
      s.vector
    )
  }

}

object ListVectorBenchmarks {

  @State(Scope.Benchmark)
  class ExecutionList {

    @Param(Array("10", "60", "120"))
    var rollUp: Int = 0

    var list: List[Midpoint] = Nil
    var vector: Vector[Midpoint] = Vector.empty

    @Setup(Level.Trial)
    def setup(): Unit = {
      list = DataCodec.read(
        getClass.getResourceAsStream("/dataanalysis/executions"))
        .map(Midpoint.fromExecution)
      vector = list.toVector
    }
  }

} 
Example 5
Source File: Interpreters.scala    From scato   with Apache License 2.0 5 votes vote down vote up
package scato
package benchmarks

import org.openjdk.jmh.annotations.{Benchmark, BenchmarkMode, Fork, Mode}

object Scato {
  import Prelude._
  import transformers.State

  def mkState[F[_]](xs: F[Int])(f: Long => (Unit, Long))(implicit F: Traversable[F]): State[Long, Unit] =
    xs.foldLeft(State.pure[Long, Unit](()))((s, _) => s.flatMap(_ => State.state(f)))

  def run[F[_]](xs: F[Int])(f: Long => (Unit, Long))(implicit F: Traversable[F]): (Unit, Long) =
    State.run(mkState(xs)(f))(0)
}

object Scalaz {
  import scalaz._
  import scalaz.State
  import scalaz.Free._
  import scalaz.syntax.traverse._

  def mkState[F[_]](xs: F[Int])(f: Long => (Long, Unit))(implicit F: Traverse[F]) =
    xs.foldLeft(State.state[Long, Unit](()).liftF)((s, _) => s.flatMap(_ => State[Long, Unit](s => f(s)).liftF))

  def run[F[_]](xs: F[Int])(f: Long => (Long, Unit))(implicit F: Traverse[F]): (Long, Unit) =
    mkState(xs)(f).foldRun(0L)((a,b) => b(a))
}

@Fork(1)
@BenchmarkMode(Array(Mode.Throughput))
class Interpreters {
  import Data._

  def f0(i: Long): (Unit, Long) = ((), i + 1)
  def f1(i: Long): (Long, Unit) = (i + 1, ())

  @Benchmark def scalaz = {
    import _root_.scalaz.std.AllInstances._
    Scalaz.run(xs)(f1 _)
  }

  @Benchmark def scato  = Scato.run(xs)(f0 _)
} 
Example 6
Source File: FreeMonads.scala    From scato   with Apache License 2.0 5 votes vote down vote up
package scato
package benchmarks

import scala.annotation.tailrec
import org.openjdk.jmh.annotations.{Benchmark, BenchmarkMode, Fork, Mode}

@Fork(1)
@BenchmarkMode(Array(Mode.Throughput))
class FreeMonads {
  def runScato(size: Int) = {
    import transformers.State
    import FreeMonadsScato._
    run(mkFreeT[State[Int, ?]](size))
  }

  def runScalaz(size: Int): Int =  {
    import FreeMonadsScalaz._
    run(mkFree(size))
  }

  @Benchmark def free128Scato = runScato(128)
  @Benchmark def free128Scalaz = runScalaz(128)

  @Benchmark def free1024Scato = runScato(1024)
  @Benchmark def free1024Scalaz = runScalaz(1024)

  @Benchmark def free10000Scato = runScato(10000)
  @Benchmark def free10000Scalaz = runScalaz(10000)
}

object FreeMonadsScato {
  import clazz.{Functor, FunctorClass, Monad}
  import free.monad._
  import transformers.State

  case class Op[A](a: A)

  object Op extends FunctorClass[Op] {
    def map[A, B](op: Op[A])(f: A => B): Op[B] = op match {
      case Op(a) => Op(f(a))
    }
  }

  def mkFreeT[M[_]: Monad](size: Int): FreeT[Op, M, Unit] = {
    @tailrec def build(i: Int, prg: FreeT[Op, M, Unit]): FreeT[Op, M, Unit] =
      if (i < 1) prg else build(i - 1, prg.flatMap(_ => FreeT.liftF(Op(()))))
    build(size, FreeT.pure[Op, M, Unit](()))
  }

  def run(prg: FreeT[Op, State[Int, ?], Unit]): Int = State.exec(FreeT.run[Op, State[Int, ?], Unit](prg) {
    case Op(a) => State.modify[Int](_ + 1)
  })(0)
}

object FreeMonadsScalaz {
  import scalaz.{~>, Free, Functor, IndexedStateT, State, StateT, Monad}
  import Free.Trampoline

  case class Op[A](a: A)

  object Op {
    implicit val functor: Functor[Op] = new Functor[Op] {
      def map[A, B](op: Op[A])(f: A => B): Op[B] = Op(f(op.a))
    }
  }

  val opToStateT: Op ~> StateT[Trampoline, Int, ?] = new (Op ~> StateT[Trampoline, Int, ?]) {
    override def apply[A](oa: Op[A]): StateT[Trampoline, Int, A] = oa match {
      case Op(a) => State.modify[Int](_ + 1).lift[Trampoline].flatMap(_ => State.state(a).lift[Trampoline])
    }
  }

  def mkFree(size: Int): Free[Op, Unit] = {
    @tailrec def build(i: Int, prg: Free[Op, Unit]): Free[Op, Unit] =
      if (i < 1) prg else build(i - 1, prg.flatMap(_ => Free.liftF(Op(()))))
    build(size, Free.point[Op, Unit](()))
  }

  def run(prg: Free[Op, Unit]): Int =
    prg.foldMap[StateT[Trampoline, Int, ?]](opToStateT)(StateT.stateTMonadState[Int, Trampoline]).exec(0).run
} 
Example 7
Source File: StringItemCuckooBenchmark.scala    From bloom-filter-scala   with MIT License 5 votes vote down vote up
package bloomfilter.mutable

import java.util.concurrent.TimeUnit

import org.openjdk.jmh.annotations.{BenchmarkMode, OperationsPerInvocation, OutputTimeUnit, _}

import scala.util.Random

@State(Scope.Benchmark)
class StringItemCuckooBenchmark {

  private val itemsExpected = 100000000L
  private val random = new Random()

  private var bf: CuckooFilter[String] = _

  @Param(Array("1024"))
  var length: Int = _

  private val items = new Array[String](10000)
  var i = 0
  while (i < items.length) {
    items(i) = random.nextString(length)
    i += 1
  }

  @Setup(Level.Iteration)
  def setup(): Unit = {
    bf = CuckooFilter[String](itemsExpected)
  }

  @Benchmark
  @BenchmarkMode(Array(Mode.SingleShotTime))
  @OutputTimeUnit(TimeUnit.NANOSECONDS)
  @OperationsPerInvocation(10000)
  def myPut(): Unit = {
    var i = 0
    while (i < items.length) {
      bf.add(items(i))
      i += 1
    }
  }

  @Benchmark
  @BenchmarkMode(Array(Mode.Throughput))
  @OperationsPerInvocation(10000)
  def myGet(): Unit = {
    var i = 0
    while (i < items.length) {
      bf.mightContain(items(i))
      i += 1
    }
  }

} 
Example 8
Source File: CornichonJsonBench.scala    From cornichon   with Apache License 2.0 5 votes vote down vote up
package parsers

import cats.instances.string._
import com.github.agourlay.cornichon.json.CornichonJson
import org.openjdk.jmh.annotations.{ Benchmark, BenchmarkMode, Fork, Measurement, Mode, Scope, State, Warmup }

@State(Scope.Benchmark)
@BenchmarkMode(Array(Mode.Throughput))
@Warmup(iterations = 10)
@Measurement(iterations = 10)
@Fork(value = 1, jvmArgsAppend = Array(
  "-XX:+FlightRecorder",
  "-XX:StartFlightRecording=filename=./CornichonJSonBench-profiling-data.jfr,name=profile,settings=profile",
  "-Xmx1G"))
class CornichonJsonBench {

  

  @Benchmark
  def parseDslStringJsonString() = {
    val res = CornichonJson.parseDslJson("cornichon")
    assert(res.isRight)
  }

  @Benchmark
  def parseDslStringJsonArray() = {
    val res = CornichonJson.parseDslJson("""[ "cornichon", "cornichon" ] """)
    assert(res.isRight)
  }

  @Benchmark
  def parseDslStringJsonTable() = {
    val res = CornichonJson.parseDslJson("""
      Name   |   Age  |
      "John" |   30   |
      "Bob"  |   41   |
    """)
    assert(res.isRight)
  }

} 
Example 9
Source File: RandomUtilBenchmarks.scala    From aerosolve   with Apache License 2.0 5 votes vote down vote up
package com.airbnb.common.ml.util

import scala.collection.mutable

import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.BenchmarkMode
import org.openjdk.jmh.annotations.Fork
import org.openjdk.jmh.annotations.Measurement
import org.openjdk.jmh.annotations.Mode
import org.openjdk.jmh.annotations.Param
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.State
import org.openjdk.jmh.annotations.Warmup


object RandomUtilBenchmarks {


  
  @BenchmarkMode(Array(Mode.Throughput))
  @Fork(value = 1, warmups = 0)
  @Warmup(iterations = 4, time = 2)
  @Measurement(iterations = 8, time = 2)
  @State(value = Scope.Benchmark)
  class SampleBenchmarks {

    // Try different collection sizes
    @Param(value = Array("1000", "10000"))
    var numItemsToGenerate: Int = _

    val ratios: Seq[Double] = Seq(0.85, 0.1, 0.05)


    @Benchmark
    def sampleArray(): Seq[Seq[Int]] = {
      RandomUtil.sample(
        Array.range(1, numItemsToGenerate),
        ratios
      )
    }

    @Benchmark
    def sampleArraySeq(): Seq[Seq[Int]] = {
      RandomUtil.sample(
        mutable.ArraySeq.range(1, numItemsToGenerate),
        ratios
      )
    }

    @Benchmark
    def sampleList(): Seq[Seq[Int]] = {
      RandomUtil.sample(
        List.range(1, numItemsToGenerate),
        ratios
      )
    }

    @Benchmark
    def sampleSeq(): Seq[Seq[Int]] = {
      RandomUtil.sample(
        Seq.range(1, numItemsToGenerate),
        ratios
      )
    }

    @Benchmark
    def sampleVector(): Seq[Seq[Int]] = {
      RandomUtil.sample(
        Vector.range(1, numItemsToGenerate),
        ratios
      )
    }

    @Benchmark
    def sampleSeqToVector(): Seq[Seq[Int]] = {
      RandomUtil.sample(
        Seq.range(1, numItemsToGenerate).toVector,
        ratios
      )
    }
  }
} 
Example 10
Source File: CoproductErrorHandlingBench.scala    From hotpotato   with Apache License 2.0 5 votes vote down vote up
package hotpotato

// Must not be in default package
import java.util.concurrent.TimeUnit

import org.openjdk.jmh.annotations.{Benchmark, BenchmarkMode, Mode, OutputTimeUnit}
import hotpotato.Examples._
import hotpotato.ZioExamples._
import BenchmarkSetup.TracedRuntime

@OutputTimeUnit(TimeUnit.MILLISECONDS)
@BenchmarkMode(Array(Mode.Throughput))
class CoproductErrorHandlingBench {

  @Benchmark
  def copBigMapErrorSome1(): Unit = {
    val io = b_E1to8_1.mapErrorSome(
      (_: E1) => MSG,
      (_: E8) => MSG_8,
    )
    val _ = TracedRuntime.unsafeRun(io.either)
  }

  @Benchmark
  def copBigMapErrorSome8(): Unit = {
    val io = b_E1to8_8.mapErrorSome(
      (_: E1) => MSG,
      (_: E8) => MSG_8,
    )
    val _ = TracedRuntime.unsafeRun(io.either)
  }

} 
Example 11
Source File: RefineVBenchmark.scala    From refined   with MIT License 5 votes vote down vote up
package eu.timepit.refined.benchmark

import eu.timepit.refined.api.Refined
import eu.timepit.refined.numeric.Positive
import eu.timepit.refined.refineV
import eu.timepit.refined.string.Regex
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations.{Benchmark, BenchmarkMode, Mode, OutputTimeUnit}

@BenchmarkMode(Array(Mode.AverageTime))
class RefineVBenchmark {

  @Benchmark
  @OutputTimeUnit(TimeUnit.NANOSECONDS)
  def refineV_Positive: Either[String, Int Refined Positive] =
    refineV[Positive](1)

  @Benchmark
  @OutputTimeUnit(TimeUnit.NANOSECONDS)
  def refineV_Regex: Either[String, String Refined Regex] =
    refineV[Regex](".*")
} 
Example 12
Source File: JavaSerializationBenchmark.scala    From scala-commons   with MIT License 5 votes vote down vote up
package com.avsystem.commons
package rpc.akka.serialization

import java.io.{ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream}

import org.openjdk.jmh.annotations.{Benchmark, BenchmarkMode, Fork, Measurement, Mode, Scope, State, Warmup}
import org.openjdk.jmh.infra.Blackhole


@Warmup(iterations = 5)
@Measurement(iterations = 20)
@Fork(1)
@BenchmarkMode(Array(Mode.Throughput))
@State(Scope.Thread)
class JavaSerializationBenchmark {

  val something = Something(42, Nested(4 :: 8 :: 15 :: 16 :: 23 :: 42 :: Nil, 0), "lol")
  val array = {
    val baos = new ByteArrayOutputStream()
    val o = new ObjectOutputStream(baos)

    o.writeObject(something)
    o.close()

    baos.toByteArray
  }

  @Benchmark
  def byteStringOutput(): Something = {
    val baos = new ByteArrayOutputStream()
    val o = new ObjectOutputStream(baos)

    o.writeObject(something)
    o.close()

    val array = baos.toByteArray

    new ObjectInputStream(new ByteArrayInputStream(array)).readObject().asInstanceOf[Something]
  }

  @Benchmark
  def writeTest(): Array[Byte] = {
    val baos = new ByteArrayOutputStream()
    val o = new ObjectOutputStream(baos)

    o.writeObject(something)
    o.close()

    baos.toByteArray
  }

  @Benchmark
  def readTest(): Something = {
    new ObjectInputStream(new ByteArrayInputStream(array)).readObject().asInstanceOf[Something]
  }
} 
Example 13
Source File: BsonInputOutputBenchmark.scala    From scala-commons   with MIT License 5 votes vote down vote up
package com.avsystem.commons
package mongo

import java.io.StringWriter
import java.nio.ByteBuffer

import com.avsystem.commons.rpc.akka.serialization.{Nested, Something}
import org.bson.io.BasicOutputBuffer
import org.bson.json.{JsonReader, JsonWriter}
import org.bson.{BsonBinaryReader, BsonBinaryWriter, BsonDocument, BsonDocumentReader, BsonDocumentWriter, BsonReader, BsonValue, BsonWriter}
import org.openjdk.jmh.annotations.{Benchmark, BenchmarkMode, Fork, Measurement, Mode, Scope, State, Warmup}

@Warmup(iterations = 10)
@Measurement(iterations = 20)
@Fork(1)
@BenchmarkMode(Array(Mode.Throughput))
@State(Scope.Thread)
class BsonInputOutputBenchmark {
  private val something = Something(42, Nested(List(4, 8, 15, 16, 23, 42, 0), 131), "lol")
  private val bytes = binaryEncode(something)
  private val doc = documentEncode(something)
  private val json = jsonEncode(something)

  def write(something: Something, bsonWriter: BsonWriter): Unit = {
    val output = new BsonWriterOutput(bsonWriter)
    Something.codec.write(output, something)
  }

  def binaryEncode(something: Something): Array[Byte] = {
    val bsonOutput = new BasicOutputBuffer()
    write(something, new BsonBinaryWriter(bsonOutput))
    bsonOutput.toByteArray
  }

  def documentEncode(something: Something): BsonDocument = {
    val doc = new BsonDocument()
    write(something, new BsonDocumentWriter(doc))
    doc
  }

  def jsonEncode(something: Something): String = {
    val stringWriter = new StringWriter()
    write(something, new JsonWriter(stringWriter))
    stringWriter.toString
  }

  @Benchmark
  def binaryEncoding(): Array[Byte] = {
    binaryEncode(something)
  }

  @Benchmark
  def documentEncoding(): BsonDocument = {
    documentEncode(something)
  }

  @Benchmark
  def jsonEncoding(): String = {
    jsonEncode(something)
  }

  @Benchmark
  def valueEncoding(): BsonValue = {
    BsonValueOutput.write(something)
  }

  def read(bsonReader: BsonReader): Something = {
    val input = new BsonReaderInput(bsonReader)
    Something.codec.read(input)
  }

  @Benchmark
  def binaryDecoding(): Something = {
    read(new BsonBinaryReader(ByteBuffer.wrap(bytes)))
  }

  @Benchmark
  def documentDecoding(): Something = {
    read(new BsonDocumentReader(doc))
  }

  @Benchmark
  def jsonDecoding(): Something = {
    read(new JsonReader(json))
  }
} 
Example 14
Source File: BsonCodecBenchmark.scala    From scala-commons   with MIT License 5 votes vote down vote up
package com.avsystem.commons
package mongo

import java.nio.ByteBuffer

import com.avsystem.commons.rpc.akka.serialization.{Nested, Something}
import org.bson.codecs.{BsonDocumentCodec, DecoderContext, EncoderContext}
import org.bson.io.BasicOutputBuffer
import org.bson.{BsonArray, BsonBinaryReader, BsonBinaryWriter, BsonDocument, BsonInt32, BsonString}
import org.openjdk.jmh.annotations.{Benchmark, BenchmarkMode, Fork, Measurement, Mode, Scope, State, Warmup}

@Warmup(iterations = 10)
@Measurement(iterations = 20)
@Fork(1)
@BenchmarkMode(Array(Mode.Throughput))
@State(Scope.Thread)
class BsonCodecBenchmark {

  import BsonCodecBenchmark._

  private val something = Something(42, Nested(List(4, 8, 15, 16, 23, 42, 0), 131), "lol")
  private val doc = somethingCodec.toDocument(something)
  private val bytes = binaryEncode(something)

  def binaryEncode(something: Something): Array[Byte] = {
    val output = new BasicOutputBuffer()
    val writer = new BsonBinaryWriter(output)
    val doc = somethingCodec.toDocument(something)
    bsonDocumentCodec.encode(writer, doc.toBson, EncoderContext.builder().build())
    output.toByteArray
  }

  @Benchmark
  def binaryEncoding(): Array[Byte] = {
    binaryEncode(something)
  }

  @Benchmark
  def binaryDecoding(): Something = {
    val reader = new BsonBinaryReader(ByteBuffer.wrap(bytes))
    val doc = bsonDocumentCodec.decode(reader, DecoderContext.builder().build())
    somethingCodec.fromDocument(new Doc(doc))
  }

  @Benchmark
  def encoding(): Doc = {
    somethingCodec.toDocument(something)
  }

  @Benchmark
  def decoding(): Something = {
    somethingCodec.fromDocument(doc)
  }
}

object BsonCodecBenchmark {

  import BsonCodec._

  val bsonDocumentCodec = new BsonDocumentCodec()

  val intKey: DocKey[Int, BsonInt32] = int32.key("int")
  val strKey: DocKey[String, BsonString] = string.key("str")
  val listKey: DocKey[List[Int], BsonArray] = int32.collection[List].key("list")

  val nestedCodec = new DocumentCodec[Nested] {
    override def toDocument(t: Nested): Doc = Doc()
      .put(listKey, t.list)
      .put(intKey, t.int)

    override def fromDocument(doc: Doc) = Nested(
      list = doc.require(listKey),
      int = doc.require(intKey)
    )
  }

  val nestedKey: DocKey[Nested, BsonDocument] = nestedCodec.bsonCodec.key("nested")

  val somethingCodec = new DocumentCodec[Something] {
    override def toDocument(t: Something): Doc = Doc()
      .put(intKey, t.int)
      .put(nestedKey, t.nested)
      .put(strKey, t.str)

    override def fromDocument(doc: Doc): Something = Something(
      int = doc.require(intKey),
      nested = doc.require(nestedKey),
      str = doc.require(strKey)
    )
  }
} 
Example 15
Source File: FastFiloRowReaderBenchmark.scala    From filo   with Apache License 2.0 5 votes vote down vote up
package org.velvia.filo

import java.sql.Timestamp
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.BenchmarkMode
import org.openjdk.jmh.annotations.{Mode, State, Scope}
import org.openjdk.jmh.annotations.OutputTimeUnit
import scalaxy.loops._
import scala.language.postfixOps

import java.util.concurrent.TimeUnit


@State(Scope.Thread)
class FastFiloRowReaderBenchmark {
  import VectorReader._

  // Ok, create an IntColumn and benchmark it.
  val numValues = 10000

  val randomInts = (0 until numValues).map(i => util.Random.nextInt)
  val randomLongs = randomInts.map(_.toLong)
  val randomTs = randomLongs.map(l => new Timestamp(l))

  val chunks = Array(VectorBuilder(randomInts).toFiloBuffer,
                     VectorBuilder(randomLongs).toFiloBuffer,
                     VectorBuilder(randomTs).toFiloBuffer)
  val clazzes = Array[Class[_]](classOf[Int], classOf[Long], classOf[Timestamp])

  // According to @ktosopl, be sure to return some value if possible so that JVM won't
  // optimize out the method body.  However JMH is apparently very good at avoiding this.
  // fastest loop possible using FiloVectorApply method
  @Benchmark
  @BenchmarkMode(Array(Mode.AverageTime))
  @OutputTimeUnit(TimeUnit.MICROSECONDS)
  def createFastFiloRowReader(): RowReader = {
    new FastFiloRowReader(chunks, clazzes)
  }

  val fastReader = new FastFiloRowReader(chunks, clazzes)

  @Benchmark
  @BenchmarkMode(Array(Mode.Throughput))
  @OutputTimeUnit(TimeUnit.SECONDS)
  def fastFiloRowReaderReadOne(): Int = {
    fastReader.setRowNo(0)
    if (fastReader.notNull(0)) fastReader.getInt(0) + 1 else 0
  }
} 
Example 16
Source File: BasicFiloBenchmark.scala    From filo   with Apache License 2.0 5 votes vote down vote up
package org.velvia.filo

import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.BenchmarkMode
import org.openjdk.jmh.annotations.{Mode, State, Scope}
import org.openjdk.jmh.annotations.OutputTimeUnit
import scalaxy.loops._
import scala.language.postfixOps

import java.util.concurrent.TimeUnit


@State(Scope.Thread)
class BasicFiloBenchmark {
  import VectorReader._
  import vectors.IntBinaryVector

  // Ok, create an IntColumn and benchmark it.
  val numValues = 10000

  val randomInts = (0 until numValues).map(i => util.Random.nextInt)
  val randomIntsAray = randomInts.toArray
  val filoBuffer = VectorBuilder(randomInts).toFiloBuffer
  val sc = FiloVector[Int](filoBuffer)

  val ivbuilder = IntBinaryVector.appendingVectorNoNA(numValues)
  randomInts.foreach(ivbuilder.addData)
  val iv = IntBinaryVector(ivbuilder.base, ivbuilder.offset, ivbuilder.numBytes)

  val byteFiloBuf = VectorBuilder(randomInts.map(_ % 128)).toFiloBuffer
  val byteVect = FiloVector[Int](byteFiloBuf)

  val diffFiloBuf = VectorBuilder(randomInts.map(10000 + _ % 128)).toFiloBuffer
  val diffVect = FiloVector[Int](diffFiloBuf)

  // According to @ktosopl, be sure to return some value if possible so that JVM won't
  // optimize out the method body.  However JMH is apparently very good at avoiding this.
  // fastest loop possible using FiloVectorApply method
  @Benchmark
  @BenchmarkMode(Array(Mode.AverageTime))
  @OutputTimeUnit(TimeUnit.MICROSECONDS)
  def sumAllIntsFiloApply(): Int = {
    var total = 0
    for { i <- 0 until numValues optimized } {
      total += sc(i)
    }
    total
  }

  @Benchmark
  @BenchmarkMode(Array(Mode.AverageTime))
  @OutputTimeUnit(TimeUnit.MICROSECONDS)
  def sumAllIntsBinaryVectApply(): Int = {
    var total = 0
    for { i <- 0 until numValues optimized } {
      total += iv(i)
    }
    total
  }

  @Benchmark
  @BenchmarkMode(Array(Mode.AverageTime))
  @OutputTimeUnit(TimeUnit.MICROSECONDS)
  def sumAllIntsFiloByteApply(): Int = {
    var total = 0
    for { i <- 0 until numValues optimized } {
      total += byteVect(i)
    }
    total
  }

  @Benchmark
  @BenchmarkMode(Array(Mode.AverageTime))
  @OutputTimeUnit(TimeUnit.MICROSECONDS)
  def sumAllIntsFiloDiffApply(): Int = {
    var total = 0
    for { i <- 0 until numValues optimized } {
      total += diffVect(i)
    }
    total
  }

  @Benchmark
  @BenchmarkMode(Array(Mode.AverageTime))
  @OutputTimeUnit(TimeUnit.MICROSECONDS)
  def sumAllNotNullIntsFiloApply(): Int = {
    var total = 0
    for { i <- 0 until numValues optimized } {
      if (sc.isAvailable(i)) total += sc(i)
    }
    total
  }

  // sum which uses foreach from FiloVector
  @Benchmark
  @BenchmarkMode(Array(Mode.AverageTime))
  @OutputTimeUnit(TimeUnit.MICROSECONDS)
  def sumAllIntsFiloForeachFoldLeft(): Int = {
    sc.foldLeft(0)(_ + _)
  }
} 
Example 17
Source File: UTF8StringBenchmark.scala    From filo   with Apache License 2.0 5 votes vote down vote up
package org.velvia.filo

import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.BenchmarkMode
import org.openjdk.jmh.annotations.{Mode, State, Scope}
import org.openjdk.jmh.annotations.OutputTimeUnit

import java.util.concurrent.TimeUnit


@State(Scope.Thread)
class UTF8StringBenchmark {

  val str = "xylophonemania"
  val str2 = "xylophonemaniac"
  val zcStr = ZeroCopyUTF8String(str)
  val zcStr2 = ZeroCopyUTF8String(str2)

  // According to @ktosopl, be sure to return some value if possible so that JVM won't
  // optimize out the method body.  However JMH is apparently very good at avoiding this.
  // fastest loop possible using FiloVectorApply method
  @Benchmark
  @BenchmarkMode(Array(Mode.Throughput))
  @OutputTimeUnit(TimeUnit.SECONDS)
  def utf8StrCompare(): Int = {
    zcStr.compare(zcStr2)
  }

  @Benchmark
  @BenchmarkMode(Array(Mode.Throughput))
  @OutputTimeUnit(TimeUnit.SECONDS)
  def nativeStrCompare(): Int = {
    str.compare(str2)
  }

  @Benchmark
  @BenchmarkMode(Array(Mode.Throughput))
  @OutputTimeUnit(TimeUnit.SECONDS)
  def utf8Substring(): ZeroCopyUTF8String = {
    zcStr.substring(2, 6)
  }

  @Benchmark
  @BenchmarkMode(Array(Mode.Throughput))
  @OutputTimeUnit(TimeUnit.SECONDS)
  def nativeSubstring(): String = {
    str.substring(2, 6)
  }

  @Benchmark
  @BenchmarkMode(Array(Mode.Throughput))
  @OutputTimeUnit(TimeUnit.SECONDS)
  def utf8hash(): Int = {
    zcStr.hashCode
  }
} 
Example 18
Source File: DictStringBenchmark.scala    From filo   with Apache License 2.0 5 votes vote down vote up
package org.velvia.filo

import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.BenchmarkMode
import org.openjdk.jmh.annotations.{Mode, State, Scope}
import org.openjdk.jmh.annotations.OutputTimeUnit
import scalaxy.loops._
import scala.language.postfixOps

import java.util.concurrent.TimeUnit


@State(Scope.Thread)
class DictStringBenchmark {
  import scala.util.Random.{alphanumeric, nextInt, nextFloat}
  import VectorReader._

  val numValues = 10000
  // NOTE: results show that time spent is heavily influenced by ratio of unique strings...
  val numUniqueStrings = 500
  val maxStringLength = 15
  val minStringLength = 5
  val naChance = 0.05    //5% of values will be NA

  def randString(len: Int): String = alphanumeric.take(len).mkString

  val uniqueStrings = (0 until numUniqueStrings).map { i =>
    randString(minStringLength + nextInt(maxStringLength - minStringLength))
  }
  val randomStrings = (0 until numValues).map(i => uniqueStrings(nextInt(numUniqueStrings)))
  val filoBufferNoNA = VectorBuilder(randomStrings).toFiloBuffer
  val scNoNA = FiloVector[String](filoBufferNoNA)

  def shouldNA: Boolean = nextFloat < naChance

  val filoBufferNA = VectorBuilder.fromOptions(
                       randomStrings.map(str => if (shouldNA) None else Some(str))
                     ).toFiloBuffer
  val scNA = FiloVector[String](filoBufferNA)

  @Benchmark
  @BenchmarkMode(Array(Mode.AverageTime))
  @OutputTimeUnit(TimeUnit.MICROSECONDS)
  def rawStringLengthTotal(): Int = {
    var totalLen = 0
    for { i <- 0 until numValues optimized } {
      totalLen += scNoNA(i).length
    }
    totalLen
  }

  // TODO: also a benchmark for the foreach/fold of a column with no NA's?

  @Benchmark
  @BenchmarkMode(Array(Mode.AverageTime))
  @OutputTimeUnit(TimeUnit.MICROSECONDS)
  // Measures foreach and NA read speed
  def withNAlengthTotal(): Unit = {
    var totalLen = 0
    scNA.foreach { str => totalLen += str.length }
    totalLen
  }
} 
Example 19
Source File: ScalaReadBenchmark.scala    From filo   with Apache License 2.0 5 votes vote down vote up
package org.velvia.filo

import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.BenchmarkMode
import org.openjdk.jmh.annotations.{Mode, State, Scope}
import org.openjdk.jmh.annotations.OutputTimeUnit
import scalaxy.loops._
import scala.language.postfixOps

import java.util.concurrent.TimeUnit


@State(Scope.Thread)
class ScalaReadBenchmark {
  // Ok, create an IntColumn and benchmark it.
  val numValues = 10000

  val randomInts = (0 until numValues).map(i => util.Random.nextInt)
  val randomIntsAray = randomInts.toArray

  // Scala Seq sum
  @Benchmark
  @BenchmarkMode(Array(Mode.AverageTime))
  @OutputTimeUnit(TimeUnit.MICROSECONDS)
  def sumAllIntsScalaSeqFoldLeft(): Int = {
    randomInts.foldLeft(0)(_ + _)
  }

  @Benchmark
  @BenchmarkMode(Array(Mode.AverageTime))
  @OutputTimeUnit(TimeUnit.MICROSECONDS)
  def sumAllIntsScalaArrayFoldLeft(): Int = {
    randomIntsAray.foldLeft(0)(_ + _)
  }

  @Benchmark
  @BenchmarkMode(Array(Mode.AverageTime))
  @OutputTimeUnit(TimeUnit.MICROSECONDS)
  def sumAllIntsScalaArrayWhileLoop(): Int = {
    var total = 0
    for { i <- 0 until numValues optimized } {
      total += randomIntsAray(i)
    }
    total
  }
} 
Example 20
Source File: FiberRefBenchmarks.scala    From zio   with Apache License 2.0 5 votes vote down vote up
package zio

import java.util.concurrent.TimeUnit

import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.BenchmarkMode
import org.openjdk.jmh.annotations.Fork
import org.openjdk.jmh.annotations.Measurement
import org.openjdk.jmh.annotations.Mode
import org.openjdk.jmh.annotations.OutputTimeUnit
import org.openjdk.jmh.annotations.Param
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.State
import org.openjdk.jmh.annotations.Threads
import org.openjdk.jmh.annotations.Warmup

import zio.IOBenchmarks.verify

@State(Scope.Thread)
@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(1)
@Threads(1)
class FiberRefBenchmarks {
  @Param(Array("32"))
  var n: Int = _

  @Benchmark
  def tracedCreateUpdateAndRead(): Unit =
    createUpdateAndRead(IOBenchmarks.TracedRuntime)

  @Benchmark
  def unTracedCreateUpdateAndRead(): Unit =
    createUpdateAndRead(IOBenchmarks)

  @Benchmark
  def unTracedJustYield(): Unit =
    justYield(IOBenchmarks)

  @Benchmark
  def unTracedCreateFiberRefsAndYield(): Unit =
    createFiberRefsAndYield(IOBenchmarks)

  private def justYield(runtime: Runtime[Any]) = runtime.unsafeRun {
    for {
      _ <- ZIO.foreach_(1.to(n))(_ => ZIO.yieldNow)
    } yield ()
  }

  private def createFiberRefsAndYield(runtime: Runtime[Any]) = runtime.unsafeRun {
    for {
      fiberRefs <- ZIO.foreach(1.to(n))(i => FiberRef.make(i))
      _         <- ZIO.foreach_(1.to(n))(_ => ZIO.yieldNow)
      values    <- ZIO.foreachPar(fiberRefs)(_.get)
      _         <- verify(values == 1.to(n))(s"Got $values")
    } yield ()
  }

  private def createUpdateAndRead(runtime: Runtime[Any]) = runtime.unsafeRun {
    for {
      fiberRefs <- ZIO.foreach(1.to(n))(i => FiberRef.make(i))
      values1   <- ZIO.foreachPar(fiberRefs)(ref => ref.update(-_) *> ref.get)
      values2   <- ZIO.foreachPar(fiberRefs)(_.get)
      _ <- verify(values1.forall(_ < 0) && values1.size == values2.size)(
            s"Got \nvalues1: $values1, \nvalues2: $values2"
          )
    } yield ()
  }
}