|
| 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.ml.util |
| 19 | + |
| 20 | +import scala.collection.mutable |
| 21 | + |
| 22 | +import org.apache.spark.{Accumulator, SparkContext} |
| 23 | + |
| 24 | +/** |
| 25 | + * Abstract class for stopwatches. |
| 26 | + */ |
| 27 | +private[spark] abstract class Stopwatch extends Serializable { |
| 28 | + |
| 29 | + @transient private var running: Boolean = false |
| 30 | + private var startTime: Long = _ |
| 31 | + |
| 32 | + /** |
| 33 | + * Name of the stopwatch. |
| 34 | + */ |
| 35 | + val name: String |
| 36 | + |
| 37 | + /** |
| 38 | + * Starts the stopwatch. |
| 39 | + * Throws an exception if the stopwatch is already running. |
| 40 | + */ |
| 41 | + def start(): Unit = { |
| 42 | + assume(!running, "start() called but the stopwatch is already running.") |
| 43 | + running = true |
| 44 | + startTime = now |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Stops the stopwatch and returns the duration of the last session in milliseconds. |
| 49 | + * Throws an exception if the stopwatch is not running. |
| 50 | + */ |
| 51 | + def stop(): Long = { |
| 52 | + assume(running, "stop() called but the stopwatch is not running.") |
| 53 | + val duration = now - startTime |
| 54 | + add(duration) |
| 55 | + running = false |
| 56 | + duration |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Checks whether the stopwatch is running. |
| 61 | + */ |
| 62 | + def isRunning: Boolean = running |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns total elapsed time in milliseconds, not counting the current session if the stopwatch |
| 66 | + * is running. |
| 67 | + */ |
| 68 | + def elapsed(): Long |
| 69 | + |
| 70 | + /** |
| 71 | + * Gets the current time in milliseconds. |
| 72 | + */ |
| 73 | + protected def now: Long = System.currentTimeMillis() |
| 74 | + |
| 75 | + /** |
| 76 | + * Adds input duration to total elapsed time. |
| 77 | + */ |
| 78 | + protected def add(duration: Long): Unit |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * A local [[Stopwatch]]. |
| 83 | + */ |
| 84 | +private[spark] class LocalStopwatch(override val name: String) extends Stopwatch { |
| 85 | + |
| 86 | + private var elapsedTime: Long = 0L |
| 87 | + |
| 88 | + override def elapsed(): Long = elapsedTime |
| 89 | + |
| 90 | + override protected def add(duration: Long): Unit = { |
| 91 | + elapsedTime += duration |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * A distributed [[Stopwatch]] using Spark accumulator. |
| 97 | + * @param sc SparkContext |
| 98 | + */ |
| 99 | +private[spark] class DistributedStopwatch( |
| 100 | + sc: SparkContext, |
| 101 | + override val name: String) extends Stopwatch { |
| 102 | + |
| 103 | + private val elapsedTime: Accumulator[Long] = sc.accumulator(0L, s"DistributedStopwatch($name)") |
| 104 | + |
| 105 | + override def elapsed(): Long = elapsedTime.value |
| 106 | + |
| 107 | + override protected def add(duration: Long): Unit = { |
| 108 | + elapsedTime += duration |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * A multiple stopwatch that contains local and distributed stopwatches. |
| 114 | + * @param sc SparkContext |
| 115 | + */ |
| 116 | +private[spark] class MultiStopwatch(@transient private val sc: SparkContext) extends Serializable { |
| 117 | + |
| 118 | + private val stopwatches: mutable.Map[String, Stopwatch] = mutable.Map.empty |
| 119 | + |
| 120 | + /** |
| 121 | + * Adds a local stopwatch. |
| 122 | + * @param name stopwatch name |
| 123 | + */ |
| 124 | + def addLocal(name: String): this.type = { |
| 125 | + require(!stopwatches.contains(name), s"Stopwatch with name $name already exists.") |
| 126 | + stopwatches(name) = new LocalStopwatch(name) |
| 127 | + this |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Adds a distributed stopwatch. |
| 132 | + * @param name stopwatch name |
| 133 | + */ |
| 134 | + def addDistributed(name: String): this.type = { |
| 135 | + require(!stopwatches.contains(name), s"Stopwatch with name $name already exists.") |
| 136 | + stopwatches(name) = new DistributedStopwatch(sc, name) |
| 137 | + this |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Gets a stopwatch. |
| 142 | + * @param name stopwatch name |
| 143 | + */ |
| 144 | + def apply(name: String): Stopwatch = stopwatches(name) |
| 145 | + |
| 146 | + override def toString: String = { |
| 147 | + stopwatches.values.toArray.sortBy(_.name) |
| 148 | + .map(c => s" ${c.name}: ${c.elapsed()}ms") |
| 149 | + .mkString("{\n", ",\n", "\n}") |
| 150 | + } |
| 151 | +} |
0 commit comments