Skip to content

Added the ability to disable coverage data recording at run-time #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ To see scoverage in action check out the [samples](https://github.com/scoverage/

### Release History

* Added the ability to disable coverage data recording by setting the JVM property `scoverage.recording.enabled` to `false`
* Slight optimization of runtime

##### 26th April 2015 1.1.0

* Bug fixes
Expand Down
16 changes: 12 additions & 4 deletions scalac-scoverage-runtime/src/main/scala/scoverage/Invoker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ import java.io.{FileFilter, File, FileWriter}
import scala.collection.{mutable, Set}
import scala.collection.concurrent.TrieMap
import scala.io.Source
import scala.util.Try

/** @author Stephen Samuel */
object Invoker {

private val MeasurementsPrefix = "scoverage.measurements."
private val threadFiles = new ThreadLocal[TrieMap[String, FileWriter]]
private val ids = TrieMap.empty[(String, Int), Any]
private val recordingEnabled = {
val key = "scoverage.recording.enabled"
Try(System.getProperty(key, "true").toLowerCase.toBoolean)
.getOrElse(throw new IllegalArgumentException(
s"""When specified, $key property must be "true" or "false" (default: true)."""))
}

/**
* We record that the given id has been invoked by appending its id to the coverage
Expand All @@ -29,18 +36,19 @@ object Invoker {
* @param dataDir the directory where the measurement data is held
*/
def invoked(id: Int, dataDir: String): Unit = {
// [sam] we can do this simple check to save writing out to a file.
// [sam] we can do this simple containment check to save writing out to a file.
// This won't work across JVMs but since there's no harm in writing out the same id multiple
// times since for coverage we only care about 1 or more, (it just slows things down to
// do it more than once), anything we can do to help is good. This helps especially with code
// that is executed many times quickly, eg tight loops.
if (!ids.contains(dataDir, id)) {
if (recordingEnabled && !ids.contains(dataDir, id)) {
// Each thread writes to a separate measurement file, to reduce contention
// and because file appends via FileWriter are not atomic on Windows.
var files = threadFiles.get()
if (files == null)
if (files == null) {
files = TrieMap.empty[String, FileWriter]
threadFiles.set(files)
threadFiles.set(files)
}

val writer = files.getOrElseUpdate(dataDir, new FileWriter(measurementFile(dataDir), true))
writer.append(id.toString + '\n').flush()
Expand Down