Skip to content

Commit

Permalink
SC-792 Rebenchmark, add function families and optimize (wavesplatform…
Browse files Browse the repository at this point in the history
  • Loading branch information
xrtm000 authored Oct 18, 2021
1 parent 9ece7b0 commit 3ca899f
Show file tree
Hide file tree
Showing 38 changed files with 1,205 additions and 366 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.wavesplatform.lang.v1

import java.util.concurrent.TimeUnit

import com.wavesplatform.account.{Address, PublicKey}
import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.crypto.Curve25519
import com.wavesplatform.lang.v1.FunctionHeader.Native
import com.wavesplatform.lang.v1.compiler.Terms.{CONST_BYTESTR, CaseObj, FUNCTION_CALL}
import com.wavesplatform.lang.v1.evaluator.FunctionIds
import com.wavesplatform.lang.v1.evaluator.ctx.impl.waves.Types
import org.openjdk.jmh.annotations._
import com.wavesplatform.common.utils._
import com.wavesplatform.lang.v1.AddressToStringBenchmark.AddressToString
import org.openjdk.jmh.infra.Blackhole

import scala.util.Random
import com.wavesplatform.lang.v1.PureFunctionsRebenchmark.evalV5

@OutputTimeUnit(TimeUnit.MICROSECONDS)
@BenchmarkMode(Array(Mode.AverageTime))
@Threads(1)
@Fork(1)
@Warmup(iterations = 10, time = 1)
@Measurement(iterations = 10, time = 1)
class AddressToStringBenchmark {
@Benchmark
def addressToString(bh: Blackhole, st: AddressToString): Unit =
bh.consume(evalV5(st.expr))
}

object AddressToStringBenchmark {
@State(Scope.Benchmark)
class AddressToString {
val publicKey = new Array[Byte](Curve25519.KeyLength)
Random.nextBytes(publicKey)

val address = Address.fromPublicKey(PublicKey(publicKey)).bytes

val expr =
FUNCTION_CALL(
Native(FunctionIds.ADDRESSTOSTRING),
List(CaseObj(Types.addressType, Map("bytes" -> CONST_BYTESTR(ByteStr(address)).explicitGet())))
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.wavesplatform.lang.v1

import java.util.concurrent.TimeUnit

import com.wavesplatform.lang.Common
import com.wavesplatform.lang.directives.DirectiveSet
import com.wavesplatform.lang.directives.values.{Account, Expression, V5}
import com.wavesplatform.lang.utils.lazyContexts
import com.wavesplatform.lang.v1.FunctionHeader.Native
import com.wavesplatform.lang.v1.compiler.Terms.{CONST_BIGINT, FUNCTION_CALL}
import com.wavesplatform.lang.v1.evaluator.EvaluatorV2
import com.wavesplatform.lang.v1.evaluator.FunctionIds.BIGINT_TO_STRING
import com.wavesplatform.lang.v1.evaluator.ctx.impl.PureContext
import org.openjdk.jmh.annotations.{State, _}
import org.openjdk.jmh.infra.Blackhole

@OutputTimeUnit(TimeUnit.MICROSECONDS)
@BenchmarkMode(Array(Mode.AverageTime))
@Threads(1)
@Fork(1)
@Warmup(iterations = 10, time = 1)
@Measurement(iterations = 10, time = 1)
class BigIntToStringBenchmark {
@Benchmark
def toString(bh: Blackhole, s: BigIntToStringSt): Unit = bh.consume(EvaluatorV2.applyCompleted(s.ctx, s.expr, V5))
}

@State(Scope.Benchmark)
class BigIntToStringSt {
val ds = DirectiveSet(V5, Account, Expression).fold(null, identity)
val ctx = lazyContexts(ds).value().evaluationContext(Common.emptyBlockchainEnvironment())

val expr = FUNCTION_CALL(
Native(BIGINT_TO_STRING),
List(CONST_BIGINT(PureContext.BigIntMin))
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.wavesplatform.lang.v1

import java.util.concurrent.TimeUnit

import com.wavesplatform.lang.Common
import com.wavesplatform.lang.directives.DirectiveSet
import com.wavesplatform.lang.directives.values.{Account, Expression, V5}
import com.wavesplatform.lang.utils.lazyContexts
import com.wavesplatform.lang.v1.FunctionHeader.Native
import com.wavesplatform.lang.v1.compiler.Terms.{CONST_BIGINT, FUNCTION_CALL}
import com.wavesplatform.lang.v1.evaluator.EvaluatorV2
import com.wavesplatform.lang.v1.evaluator.FunctionIds.{FRACTION_BIGINT, FRACTION_BIGINT_ROUNDS}
import com.wavesplatform.lang.v1.evaluator.ctx.impl.{PureContext, Rounding}
import org.openjdk.jmh.annotations.{State, _}
import org.openjdk.jmh.infra.Blackhole

@OutputTimeUnit(TimeUnit.MICROSECONDS)
@BenchmarkMode(Array(Mode.AverageTime))
@Threads(1)
@Fork(1)
@Warmup(iterations = 10, time = 1)
@Measurement(iterations = 10, time = 1)
class FractionBigIntBenchmark {
@Benchmark
def fraction1(bh: Blackhole, s: FractionBigIntSt): Unit = bh.consume(EvaluatorV2.applyCompleted(s.ctx, s.expr1, V5))

@Benchmark
def fraction2(bh: Blackhole, s: FractionBigIntSt): Unit = bh.consume(EvaluatorV2.applyCompleted(s.ctx, s.expr2, V5))

@Benchmark
def fraction3(bh: Blackhole, s: FractionBigIntSt): Unit = bh.consume(EvaluatorV2.applyCompleted(s.ctx, s.expr3, V5))

@Benchmark
def fraction1Round(bh: Blackhole, s: FractionBigIntSt): Unit = bh.consume(EvaluatorV2.applyCompleted(s.ctx, s.expr1Round, V5))

@Benchmark
def fraction2Round(bh: Blackhole, s: FractionBigIntSt): Unit = bh.consume(EvaluatorV2.applyCompleted(s.ctx, s.expr2Round, V5))

@Benchmark
def fraction3Round(bh: Blackhole, s: FractionBigIntSt): Unit = bh.consume(EvaluatorV2.applyCompleted(s.ctx, s.expr3Round, V5))
}

@State(Scope.Benchmark)
class FractionBigIntSt {
val ds = DirectiveSet(V5, Account, Expression).fold(null, identity)
val ctx = lazyContexts(ds).value().evaluationContext(Common.emptyBlockchainEnvironment())

val max = CONST_BIGINT(PureContext.BigIntMax)
val halfMax = CONST_BIGINT(PureContext.BigIntMax / 2)
val min = CONST_BIGINT(PureContext.BigIntMin)
val maxSqrt = CONST_BIGINT(BigInt("57896044618658097711785492504343953926634992332820282019728792003956564819968"))
val three = CONST_BIGINT(3)

val expr1 = FUNCTION_CALL(
Native(FRACTION_BIGINT),
List(halfMax, three, three)
)

val expr2 = FUNCTION_CALL(
Native(FRACTION_BIGINT),
List(max, min, min)
)

val expr3 = FUNCTION_CALL(
Native(FRACTION_BIGINT),
List(maxSqrt, maxSqrt, maxSqrt)
)

val expr1Round = FUNCTION_CALL(
Native(FRACTION_BIGINT_ROUNDS),
List(halfMax, three, three, Rounding.HalfEven.value)
)

val expr2Round = FUNCTION_CALL(
Native(FRACTION_BIGINT_ROUNDS),
List(max, min, min, Rounding.HalfEven.value)
)

val expr3Round = FUNCTION_CALL(
Native(FRACTION_BIGINT_ROUNDS),
List(maxSqrt, maxSqrt, maxSqrt, Rounding.HalfEven.value)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.wavesplatform.lang.v1

import com.wavesplatform.lang.Common
import com.wavesplatform.lang.directives.DirectiveSet
import com.wavesplatform.lang.directives.values.{Account, Expression, V5}
import com.wavesplatform.lang.directives.values.{Account, Expression, V6}
import com.wavesplatform.lang.utils.lazyContexts
import com.wavesplatform.lang.v1.compiler.Terms.EXPR
import com.wavesplatform.lang.v1.compiler.TestCompiler
Expand All @@ -16,39 +16,41 @@ import java.util.concurrent.TimeUnit
@BenchmarkMode(Array(Mode.AverageTime))
@Threads(1)
@Fork(1)
@Warmup(iterations = 30)
@Measurement(iterations = 30)
@Warmup(iterations = 10, time = 1)
@Measurement(iterations = 10, time = 1)
class FractionIntBenchmark {
@Benchmark
def fraction1(bh: Blackhole, s: St): Unit = bh.consume(EvaluatorV2.applyCompleted(s.ctx, s.expr1, V5))
def fraction1(bh: Blackhole, s: St): Unit = bh.consume(EvaluatorV2.applyCompleted(s.ctx, s.expr1, V6))

@Benchmark
def fraction2(bh: Blackhole, s: St): Unit = bh.consume(EvaluatorV2.applyCompleted(s.ctx, s.expr2, V5))
def fraction2(bh: Blackhole, s: St): Unit = bh.consume(EvaluatorV2.applyCompleted(s.ctx, s.expr2, V6))

@Benchmark
def fraction3(bh: Blackhole, s: St): Unit = bh.consume(EvaluatorV2.applyCompleted(s.ctx, s.expr3, V5))
def fraction3(bh: Blackhole, s: St): Unit = bh.consume(EvaluatorV2.applyCompleted(s.ctx, s.expr3, V6))

@Benchmark
def fraction1Round(bh: Blackhole, s: St): Unit = bh.consume(EvaluatorV2.applyCompleted(s.ctx, s.expr1Round, V5))
def fraction1Round(bh: Blackhole, s: St): Unit = bh.consume(EvaluatorV2.applyCompleted(s.ctx, s.expr1Round, V6))

@Benchmark
def fraction2Round(bh: Blackhole, s: St): Unit = bh.consume(EvaluatorV2.applyCompleted(s.ctx, s.expr2Round, V5))
def fraction2Round(bh: Blackhole, s: St): Unit = bh.consume(EvaluatorV2.applyCompleted(s.ctx, s.expr2Round, V6))

@Benchmark
def fraction3Round(bh: Blackhole, s: St): Unit = bh.consume(EvaluatorV2.applyCompleted(s.ctx, s.expr3Round, V5))
def fraction3Round(bh: Blackhole, s: St): Unit = bh.consume(EvaluatorV2.applyCompleted(s.ctx, s.expr3Round, V6))
}

@State(Scope.Benchmark)
class St {
val ds = DirectiveSet(V5, Account, Expression).fold(null, identity)
val ds = DirectiveSet(V6, Account, Expression).fold(null, identity)
val ctx = lazyContexts(ds).value().evaluationContext(Common.emptyBlockchainEnvironment())
val max = Long.MaxValue

val expr1 = TestCompiler(V5).compileExpression(s"fraction($max, -$max, 1)").expr.asInstanceOf[EXPR]
val expr2 = TestCompiler(V5).compileExpression(s"fraction($max, -$max, -$max)").expr.asInstanceOf[EXPR]
val expr3 = TestCompiler(V5).compileExpression(s"fraction($max, $max, ${-max / 2 + 1})").expr.asInstanceOf[EXPR]
val max = Long.MaxValue
val maxSqrt = 3037000499L

val expr1Round = TestCompiler(V5).compileExpression(s"fraction($max, -$max, 1, HALFEVEN)").expr.asInstanceOf[EXPR]
val expr2Round = TestCompiler(V5).compileExpression(s"fraction($max, -$max, -$max, HALFEVEN)").expr.asInstanceOf[EXPR]
val expr3Round = TestCompiler(V5).compileExpression(s"fraction($max, $max, ${-max / 2 + 1}, HALFEVEN)").expr.asInstanceOf[EXPR]
val expr1 = TestCompiler(V6).compileExpression(s"fraction($max / 2, 3, 3)").expr.asInstanceOf[EXPR]
val expr2 = TestCompiler(V6).compileExpression(s"fraction($max, -$max, -$max)").expr.asInstanceOf[EXPR]
val expr3 = TestCompiler(V6).compileExpression(s"fraction($maxSqrt, $maxSqrt, $maxSqrt)").expr.asInstanceOf[EXPR]

val expr1Round = TestCompiler(V6).compileExpression(s"fraction($max / 2, 3, 3, HALFEVEN)").expr.asInstanceOf[EXPR]
val expr2Round = TestCompiler(V6).compileExpression(s"fraction($max, -$max, -$max, HALFEVEN)").expr.asInstanceOf[EXPR]
val expr3Round = TestCompiler(V6).compileExpression(s"fraction($maxSqrt, $maxSqrt, $maxSqrt, HALFEVEN)").expr.asInstanceOf[EXPR]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,92 @@ package com.wavesplatform.lang.v1

import java.util.concurrent.TimeUnit

import com.wavesplatform.lang.v1.MakeStringBenchmark.MakeStringSt
import com.wavesplatform.common.utils._
import com.wavesplatform.lang.v1.FunctionHeader.Native
import com.wavesplatform.lang.v1.MakeStringBenchmark._
import com.wavesplatform.lang.v1.PureFunctionsRebenchmark.evalV5
import com.wavesplatform.lang.v1.compiler.Terms.{ARR, CONST_STRING, FUNCTION_CALL}
import com.wavesplatform.lang.v1.evaluator.FunctionIds
import org.openjdk.jmh.annotations._
import org.openjdk.jmh.infra.Blackhole

import scala.util.Random

@OutputTimeUnit(TimeUnit.MICROSECONDS)
@BenchmarkMode(Array(Mode.AverageTime))
@Threads(1)
@Fork(1)
@Warmup(iterations = 30)
@Measurement(iterations = 30)
@Warmup(iterations = 10, time = 1)
@Measurement(iterations = 10, time = 1)
class MakeStringBenchmark {
@Benchmark
def makeStringSep31x1000(st: MakeStringSep31x1000, bh: Blackhole): Unit =
bh.consume(evalV5(st.expr))

@Benchmark
def makeString31x1000(st: MakeString31x1000, bh: Blackhole): Unit =
bh.consume(evalV5(st.expr))

@Benchmark
def makeString31x100(st: MakeString31x100, bh: Blackhole): Unit =
bh.consume(evalV5(st.expr))

@Benchmark
def makeString31x10(st: MakeString31x10, bh: Blackhole): Unit =
bh.consume(evalV5(st.expr))

@Benchmark
def makeString(st: MakeStringSt, bh: Blackhole): Unit =
bh.consume(st.stringList.mkString(","))
def makeString310x100(st: MakeString310x100, bh: Blackhole): Unit =
bh.consume(evalV5(st.expr))

@Benchmark
def makeString3100x10(st: MakeString3100x10, bh: Blackhole): Unit =
bh.consume(evalV5(st.expr))

@Benchmark
def makeString1x70(st: MakeString1x70, bh: Blackhole): Unit =
bh.consume(evalV5(st.expr))

@Benchmark
def makeString7x70(st: MakeString7x70, bh: Blackhole): Unit =
bh.consume(evalV5(st.expr))
}

object MakeStringBenchmark {
@State(Scope.Benchmark)
class MakeStringSt {
val stringList = List.fill(1000)("a" * 33)
abstract class MakeString(listSize: Int, stringSize: Int = 1, separatorSize: Int = 1) {
val string = Random.nextPrintableChar().toString * stringSize
val separator = Random.nextPrintableChar().toString * separatorSize
val expr =
FUNCTION_CALL(
Native(FunctionIds.MAKESTRING),
List(
ARR(Vector.fill(listSize)(CONST_STRING(string).explicitGet()), limited = true).explicitGet(),
CONST_STRING(separator).explicitGet()
)
)
}
}

@State(Scope.Benchmark)
class MakeStringSep31x1000 extends MakeString(listSize = 1000, separatorSize = 31)

@State(Scope.Benchmark)
class MakeString31x1000 extends MakeString(listSize = 1000, stringSize = 31)

@State(Scope.Benchmark)
class MakeString31x100 extends MakeString(listSize = 100, stringSize = 31)

@State(Scope.Benchmark)
class MakeString31x10 extends MakeString(listSize = 100, stringSize = 31)

@State(Scope.Benchmark)
class MakeString310x100 extends MakeString(listSize = 100, stringSize = 310)

@State(Scope.Benchmark)
class MakeString3100x10 extends MakeString(listSize = 10, stringSize = 3100)

@State(Scope.Benchmark)
class MakeString1x70 extends MakeString(listSize = 70, stringSize = 1)

@State(Scope.Benchmark)
class MakeString7x70 extends MakeString(listSize = 70, stringSize = 7)
}
Loading

0 comments on commit 3ca899f

Please sign in to comment.