|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.streaming.scheduler.rate |
| 19 | + |
| 20 | +import scala.util.Random |
| 21 | + |
| 22 | +import org.scalatest.Inspectors.forAll |
| 23 | +import org.scalatest.Matchers |
| 24 | + |
| 25 | +import org.apache.spark.{SparkConf, SparkFunSuite} |
| 26 | +import org.apache.spark.streaming.Seconds |
| 27 | + |
| 28 | +class PIDRateEstimatorSuite extends SparkFunSuite with Matchers { |
| 29 | + |
| 30 | + test("the right estimator is created") { |
| 31 | + val conf = new SparkConf |
| 32 | + conf.set("spark.streaming.backpressure.rateEstimator", "pid") |
| 33 | + val pid = RateEstimator.create(conf, Seconds(1)) |
| 34 | + pid.getClass should equal(classOf[PIDRateEstimator]) |
| 35 | + } |
| 36 | + |
| 37 | + test("estimator checks ranges") { |
| 38 | + intercept[IllegalArgumentException] { |
| 39 | + new PIDRateEstimator(0, 1, 2, 3) |
| 40 | + } |
| 41 | + intercept[IllegalArgumentException] { |
| 42 | + new PIDRateEstimator(100, -1, 2, 3) |
| 43 | + } |
| 44 | + intercept[IllegalArgumentException] { |
| 45 | + new PIDRateEstimator(100, 0, -1, 3) |
| 46 | + } |
| 47 | + intercept[IllegalArgumentException] { |
| 48 | + new PIDRateEstimator(100, 0, 0, -1) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private def createDefaultEstimator: PIDRateEstimator = { |
| 53 | + new PIDRateEstimator(20, 1D, 0D, 0D) |
| 54 | + } |
| 55 | + |
| 56 | + test("first bound is None") { |
| 57 | + val p = createDefaultEstimator |
| 58 | + p.compute(0, 10, 10, 0) should equal(None) |
| 59 | + } |
| 60 | + |
| 61 | + test("second bound is rate") { |
| 62 | + val p = createDefaultEstimator |
| 63 | + p.compute(0, 10, 10, 0) |
| 64 | + // 1000 elements / s |
| 65 | + p.compute(10, 10, 10, 0) should equal(Some(1000)) |
| 66 | + } |
| 67 | + |
| 68 | + test("works even with no time between updates") { |
| 69 | + val p = createDefaultEstimator |
| 70 | + p.compute(0, 10, 10, 0) |
| 71 | + p.compute(10, 10, 10, 0) |
| 72 | + p.compute(10, 10, 10, 0) should equal(None) |
| 73 | + } |
| 74 | + |
| 75 | + test("bound is never negative") { |
| 76 | + val p = new PIDRateEstimator(20, 1D, 1D, 0D) |
| 77 | + // prepare a series of batch updates, one every 20ms, 0 processed elements, 2ms of processing |
| 78 | + // this might point the estimator to try and decrease the bound, but we test it never |
| 79 | + // goes below zero, which would be nonsensical. |
| 80 | + val times = List.tabulate(50)(x => x * 20) // every 20ms |
| 81 | + val elements = List.fill(50)(0) // no processing |
| 82 | + val proc = List.fill(50)(20) // 20ms of processing |
| 83 | + val sched = List.fill(50)(100) // strictly positive accumulation |
| 84 | + val res = for (i <- List.range(0, 50)) yield p.compute(times(i), elements(i), proc(i), sched(i)) |
| 85 | + res.head should equal(None) |
| 86 | + res.tail should equal(List.fill(49)(Some(0D))) |
| 87 | + } |
| 88 | + |
| 89 | + test("with no accumulated or positive error, |I| > 0, follow the processing speed") { |
| 90 | + val p = new PIDRateEstimator(20, 1D, 1D, 0D) |
| 91 | + // prepare a series of batch updates, one every 20ms with an increasing number of processed |
| 92 | + // elements in each batch, but constant processing time, and no accumulated error. Even though |
| 93 | + // the integral part is non-zero, the estimated rate should follow only the proportional term |
| 94 | + val times = List.tabulate(50)(x => x * 20) // every 20ms |
| 95 | + val elements = List.tabulate(50)(x => x * 20) // increasing |
| 96 | + val proc = List.fill(50)(20) // 20ms of processing |
| 97 | + val sched = List.fill(50)(0) |
| 98 | + val res = for (i <- List.range(0, 50)) yield p.compute(times(i), elements(i), proc(i), sched(i)) |
| 99 | + res.head should equal(None) |
| 100 | + res.tail should equal(List.tabulate(50)(x => Some(x * 1000D)).tail) |
| 101 | + } |
| 102 | + |
| 103 | + test("with no accumulated but some positive error, |I| > 0, follow the processing speed") { |
| 104 | + val p = new PIDRateEstimator(20, 1D, 1D, 0D) |
| 105 | + // prepare a series of batch updates, one every 20ms with an decreasing number of processed |
| 106 | + // elements in each batch, but constant processing time, and no accumulated error. Even though |
| 107 | + // the integral part is non-zero, the estimated rate should follow only the proportional term, |
| 108 | + // asking for less and less elements |
| 109 | + val times = List.tabulate(50)(x => x * 20) // every 20ms |
| 110 | + val elements = List.tabulate(50)(x => (50 - x) * 20) // decreasing |
| 111 | + val proc = List.fill(50)(20) // 20ms of processing |
| 112 | + val sched = List.fill(50)(0) |
| 113 | + val res = for (i <- List.range(0, 50)) yield p.compute(times(i), elements(i), proc(i), sched(i)) |
| 114 | + res.head should equal(None) |
| 115 | + res.tail should equal(List.tabulate(50)(x => Some((50 - x) * 1000D)).tail) |
| 116 | + } |
| 117 | + |
| 118 | + test("with some accumulated and some positive error, |I| > 0, stay below the processing speed") { |
| 119 | + val p = new PIDRateEstimator(20, 1D, .01D, 0D) |
| 120 | + val times = List.tabulate(50)(x => x * 20) // every 20ms |
| 121 | + val rng = new Random() |
| 122 | + val elements = List.tabulate(50)(x => rng.nextInt(1000)) |
| 123 | + val procDelayMs = 20 |
| 124 | + val proc = List.fill(50)(procDelayMs) // 20ms of processing |
| 125 | + val sched = List.tabulate(50)(x => rng.nextInt(19)) // random wait |
| 126 | + val speeds = elements map ((x) => x.toDouble / procDelayMs * 1000) |
| 127 | + |
| 128 | + val res = for (i <- List.range(0, 50)) yield p.compute(times(i), elements(i), proc(i), sched(i)) |
| 129 | + res.head should equal(None) |
| 130 | + forAll(List.range(1, 50)) { (n) => |
| 131 | + res(n) should not be None |
| 132 | + if (res(n).get > 0 && sched(n) > 0) { |
| 133 | + res(n).get should be < speeds(n) |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments