|
| 1 | +/* |
| 2 | + * Copyright 2020 Fabio Labella |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package dynosaur |
| 18 | + |
| 19 | +import cats.syntax.all._ |
| 20 | + |
| 21 | +import org.openjdk.jmh.annotations.{ |
| 22 | + Benchmark, |
| 23 | + BenchmarkMode, |
| 24 | + Mode |
| 25 | +} |
| 26 | +import cats.data.NonEmptyList |
| 27 | + |
| 28 | +sealed trait Dynosaur |
| 29 | +object Dynosaur { |
| 30 | + case class Parasaurolophus(name: String, age: Int, songs: Int) |
| 31 | + extends Dynosaur |
| 32 | + case class TyrannosaurusRex(name: String, age: Int, victims: Int) |
| 33 | + extends Dynosaur |
| 34 | + case class Allosaurus(name: String, age: Int, attacks: Int) extends Dynosaur |
| 35 | +} |
| 36 | + |
| 37 | +import Dynosaur._ |
| 38 | + |
| 39 | +object DecodingBench { |
| 40 | + |
| 41 | + val tyrannosaurusRexDv = DynamoValue.m( |
| 42 | + "name" -> DynamoValue.s("Foolio"), |
| 43 | + "age" -> DynamoValue.n(20000000), |
| 44 | + "victims" -> DynamoValue.n(9) |
| 45 | + ) |
| 46 | + |
| 47 | + val parasaurolophusDv = DynamoValue.m( |
| 48 | + "name" -> DynamoValue.s("Cantolio"), |
| 49 | + "age" -> DynamoValue.n(25000000), |
| 50 | + "songs" -> DynamoValue.n(9) |
| 51 | + ) |
| 52 | + |
| 53 | + val allosaurusDv = DynamoValue.m( |
| 54 | + "name" -> DynamoValue.s("Cantolio"), |
| 55 | + "age" -> DynamoValue.n(25000000), |
| 56 | + "attacks" -> DynamoValue.n(99) |
| 57 | + ) |
| 58 | + |
| 59 | + val tyrannosaurusRexWithTagDv = DynamoValue.m( |
| 60 | + "tyrannosaurus-rex" -> tyrannosaurusRexDv |
| 61 | + ) |
| 62 | + |
| 63 | + val allosaurusWithTagDv = DynamoValue.m( |
| 64 | + "allosaurus" -> allosaurusDv |
| 65 | + ) |
| 66 | + |
| 67 | + val parasaurolophusWithTagDv = DynamoValue.m( |
| 68 | + "parasaurolophus" -> parasaurolophusDv |
| 69 | + ) |
| 70 | + |
| 71 | + val allosauruses = DynamoValue.l( |
| 72 | + (0 until 10) |
| 73 | + .map(_ => allosaurusDv) |
| 74 | + .toList |
| 75 | + ) |
| 76 | + |
| 77 | + val dynosaursWithTag = DynamoValue.l( |
| 78 | + tyrannosaurusRexWithTagDv, |
| 79 | + allosaurusWithTagDv, |
| 80 | + parasaurolophusWithTagDv |
| 81 | + ) |
| 82 | + |
| 83 | + val dynosaurDvWithDiscriminator = DynamoValue.m( |
| 84 | + "name" -> DynamoValue.s("Foolio"), |
| 85 | + "age" -> DynamoValue.n(20000000), |
| 86 | + "victims" -> DynamoValue.n(9), |
| 87 | + "kind" -> DynamoValue.s("tyrannosaurus-rex") |
| 88 | + ) |
| 89 | + |
| 90 | + val schermaForParasaurolophus: Schema[Parasaurolophus] = |
| 91 | + Schema.record[Parasaurolophus] { fields => |
| 92 | + ( |
| 93 | + fields("name", _.name), |
| 94 | + fields("age", _.age), |
| 95 | + fields("songs", _.songs) |
| 96 | + ).mapN(Parasaurolophus.apply) |
| 97 | + } |
| 98 | + |
| 99 | + def defSchermaForTyrannosaurusRex: Schema[TyrannosaurusRex] = |
| 100 | + Schema.record[TyrannosaurusRex] { fields => |
| 101 | + ( |
| 102 | + fields("name", _.name), |
| 103 | + fields("age", _.age), |
| 104 | + fields("victims", _.victims) |
| 105 | + ).mapN(TyrannosaurusRex.apply) |
| 106 | + } |
| 107 | + |
| 108 | + val schermaForTyrannosaurusRex: Schema[TyrannosaurusRex] = |
| 109 | + defSchermaForTyrannosaurusRex |
| 110 | + |
| 111 | + implicit def implicitSchermaForTyrannosaurusRex: Schema[TyrannosaurusRex] = |
| 112 | + defSchermaForTyrannosaurusRex |
| 113 | + |
| 114 | + val schermaForAllosaurus: Schema[Allosaurus] = Schema.record[Allosaurus] { |
| 115 | + fields => |
| 116 | + ( |
| 117 | + fields("name", _.name), |
| 118 | + fields("age", _.age), |
| 119 | + fields("attacks", _.attacks) |
| 120 | + ).mapN(Allosaurus.apply) |
| 121 | + } |
| 122 | + |
| 123 | + def defSchermaForAllosaurus: Schema[Allosaurus] = Schema.record[Allosaurus] { |
| 124 | + fields => |
| 125 | + ( |
| 126 | + fields("name", _.name), |
| 127 | + fields("age", _.age), |
| 128 | + fields("attacks", _.attacks) |
| 129 | + ).mapN(Allosaurus.apply) |
| 130 | + } |
| 131 | + |
| 132 | + implicit lazy val implicitSchermaForAllosaurus: Schema[Allosaurus] = |
| 133 | + defSchermaForAllosaurus |
| 134 | + |
| 135 | + val schermaForParasaurolophusWithDiscriminator: Schema[Parasaurolophus] = |
| 136 | + Schema.record[Parasaurolophus] { fields => |
| 137 | + fields.const("kind", "parasaurolophus") *> |
| 138 | + ( |
| 139 | + fields("name", _.name), |
| 140 | + fields("age", _.age), |
| 141 | + fields("songs", _.songs) |
| 142 | + ).mapN(Parasaurolophus.apply) |
| 143 | + } |
| 144 | + |
| 145 | + val schermaForTyrannosaurusRexWithDiscriminator: Schema[TyrannosaurusRex] = |
| 146 | + Schema.record[TyrannosaurusRex] { fields => |
| 147 | + fields.const("kind", "tyrannosaurus-rex") *> |
| 148 | + ( |
| 149 | + fields("name", _.name), |
| 150 | + fields("age", _.age), |
| 151 | + fields("victims", _.victims) |
| 152 | + ).mapN(TyrannosaurusRex.apply) |
| 153 | + } |
| 154 | + |
| 155 | + val schermaForAllosaurusWithDiscriminator: Schema[Allosaurus] = |
| 156 | + Schema.record[Allosaurus] { fields => |
| 157 | + fields.const("kind", "allosaurus") *> |
| 158 | + ( |
| 159 | + fields("name", _.name), |
| 160 | + fields("age", _.age), |
| 161 | + fields("attacks", _.attacks) |
| 162 | + ).mapN(Allosaurus.apply) |
| 163 | + } |
| 164 | + |
| 165 | + val schemaForDynosaurWithTag: Schema[Dynosaur] = Schema.oneOf { alt => |
| 166 | + NonEmptyList |
| 167 | + .of( |
| 168 | + alt(schermaForParasaurolophus.tag("parasaurolophus")), |
| 169 | + alt(schermaForTyrannosaurusRex.tag("tyrannosaurus-rex")), |
| 170 | + alt(schermaForAllosaurus.tag("allosaurus")) |
| 171 | + ) |
| 172 | + .reduceLeft(_ |+| _) |
| 173 | + } |
| 174 | + |
| 175 | + val schemaForDynosaurWithDiscriminator: Schema[Dynosaur] = Schema.oneOf { |
| 176 | + alt => |
| 177 | + NonEmptyList |
| 178 | + .of( |
| 179 | + alt(schermaForParasaurolophusWithDiscriminator), |
| 180 | + alt(schermaForTyrannosaurusRexWithDiscriminator), |
| 181 | + alt(schermaForAllosaurusWithDiscriminator) |
| 182 | + ) |
| 183 | + .reduceLeft(_ |+| _) |
| 184 | + } |
| 185 | + |
| 186 | + val schermaForAllosauruses = |
| 187 | + Schema.seq(schermaForAllosaurus) |
| 188 | + |
| 189 | + val string = DynamoValue.s("dynosaur") |
| 190 | + val strings = DynamoValue.l((0 until 10).map { idx => |
| 191 | + DynamoValue.s(s"test-$idx") |
| 192 | + }.toList) |
| 193 | + |
| 194 | + val schemaForStrings = Schema.seq(Schema.string) |
| 195 | +} |
| 196 | + |
| 197 | +class DecodingBench { |
| 198 | + |
| 199 | + import DecodingBench._ |
| 200 | + |
| 201 | + @Benchmark |
| 202 | + @BenchmarkMode(Array(Mode.Throughput)) |
| 203 | + def decodeAnS = |
| 204 | + Schema.string.read(string) |
| 205 | + |
| 206 | + @Benchmark |
| 207 | + @BenchmarkMode(Array(Mode.Throughput)) |
| 208 | + def decodeAnM = |
| 209 | + schermaForAllosaurus.read(allosaurusDv) |
| 210 | + |
| 211 | + @Benchmark |
| 212 | + @BenchmarkMode(Array(Mode.Throughput)) |
| 213 | + def decodeOneOfWithTag = |
| 214 | + schemaForDynosaurWithTag.read(tyrannosaurusRexWithTagDv) |
| 215 | + |
| 216 | + @Benchmark |
| 217 | + @BenchmarkMode(Array(Mode.Throughput)) |
| 218 | + def decodeOneOfWithDiscriminator = |
| 219 | + schemaForDynosaurWithDiscriminator.read(dynosaurDvWithDiscriminator) |
| 220 | + |
| 221 | + @Benchmark |
| 222 | + @BenchmarkMode(Array(Mode.Throughput)) |
| 223 | + def decodeList = schemaForStrings.read(strings) |
| 224 | + |
| 225 | +} |
0 commit comments