Skip to content
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

added mutable fake ps and improved benchmarking #11

Merged
merged 1 commit into from
Mar 2, 2016
Merged
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
13 changes: 7 additions & 6 deletions src/test/scala/common/Benchmark.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package edu.luc.etl.osdi.processtree.scala.common
package edu.luc.etl.osdi.processtree.scala
package common

import org.scalameter.api._

Expand All @@ -8,14 +9,14 @@ import org.scalameter.api._
*/
abstract class Benchmark(label: String) extends Bench.LocalTime with TreeBuilder {

val sizes: Gen[Int] = Gen.exponential("processes")(1000, 100000, 10)
val sizes: Gen[Int] = Gen.exponential("processes")(10, 100000, 10)

val inputs: Gen[Iterator[Process]] = sizes map FakePs.fakePs
val inputs: Gen[Iterator[Process]] = sizes map fakeps.fakePs cached

performance of label in {
measure method "fakePs" in {
using(inputs) in { ps =>
buildTree(ps)
measure method "buildTree" in {
using (inputs) in { ps =>
(1 to 10000) foreach { _ => buildTree(ps) }
}
}
}
Expand Down
41 changes: 0 additions & 41 deletions src/test/scala/common/FakePs.scala

This file was deleted.

23 changes: 23 additions & 0 deletions src/test/scala/fakeps/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package edu.luc.etl.osdi.processtree.scala
package fakeps

import scala.util.Try

/**
* Main method to generate a fake list of processes of the specified length.
* Also exposes its utility methods for testing/benchmarking.
*/
object Main extends App {

val arg = Try { args(0).toInt }
if (arg.isFailure) {
Console.err.println("usage: fakeps n (where n = number of process table entries)")
System.exit(1)
}

val n = arg.get
val ps = fakePs(arg.get)
println("%-10s %-10s %-10s".format("PID", "PPID", "CMD"))
for ((pid, ppid, cmd) <- ps)
println("%-10s %-10s %s".format(pid, ppid, cmd))
}
19 changes: 19 additions & 0 deletions src/test/scala/fakeps/benchmarks.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package edu.luc.etl.osdi.processtree.scala
package fakeps

import org.scalameter.api._

abstract class Benchmark(val f: Int => Map[Int, Iterable[Int]], val label: String) extends Bench.LocalTime {

val sizes: Gen[Int] = Gen.exponential("processes")(10, 10000, 10)

measure method label in {
using (sizes) in { n =>
f(n)
}
}
}

object BenchmarkMutable extends Benchmark(fakePsMutable, "fakePsMutable")

object BenchmarkFold extends Benchmark(fakePsFold, "fakePsFold")
53 changes: 53 additions & 0 deletions src/test/scala/fakeps/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package edu.luc.etl.osdi.processtree.scala
package fakeps

import scala.collection.mutable.{ArrayBuffer, Map => MMap}
import scala.util.Random
import common.Process

/**
* Utility methods for generating a fake list of processes of the specified length.
*/
object `package` {

/**
* Generates a barebones process tree (ppid -> pid*) of size n
* using an immutable implementation. It runs very slowly.
*/
def fakePsFold(n: Int): Map[Int, Iterable[Int]] = (2 to n).foldLeft {
Map(0 -> Seq(1), 1 -> Seq.empty)
} { (ps, nextPid) =>
val randomPid = 1 + Random.nextInt(ps.size - 1)
val randomPidChildren = nextPid +: ps(randomPid)
val nextPidChildren = ps.getOrElse(nextPid, Seq.empty)
ps + (nextPid -> nextPidChildren, randomPid -> randomPidChildren)
}

/**
* Generates a barebones process tree (ppid -> pid*) of size n
* using a mutable implementation. It runs blazingly fast.
*/
def fakePsMutable(n: Int): Map[Int, Iterable[Int]] = {
val ps = MMap(0 -> ArrayBuffer(1), 1 -> ArrayBuffer.empty[Int])
(2 to n) foreach { nextPid =>
val randomPid = 1 + Random.nextInt(ps.size - 1)
ps.getOrElseUpdate(nextPid, ArrayBuffer.empty)
ps(randomPid) += nextPid
}
ps.toMap
}

/** Converts a tree (ppid -> pid*) into an iterator of pid -> ppid edges. */
def inverseEdges(m: Map[Int, Iterable[Int]]): Iterator[(Int, Int)] =
for (ppid <- m.keys.iterator; pid <- m(ppid).iterator) yield (pid, ppid)

/** Adds a command string to each pid -> ppid edge. */
def addCmd(i: Iterator[(Int, Int)], s: String): Iterator[Process] =
i map { case (pid, ppid) => (pid, ppid, s + " " + pid) }

/** Adds a command string to each pid -> ppid edge. */
def addCmd(i: Iterator[(Int, Int)]): Iterator[Process] = addCmd(i, "Fake Process")

/** Generates the fake ps command output. */
def fakePs(n: Int) = addCmd(inverseEdges(fakePsMutable(n)))
}
2 changes: 1 addition & 1 deletion src/test/scala/mutable/Benchmark.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package edu.luc.etl.osdi.processtree.scala
package mutable

object Benchmark extends common.Benchmark("ProcessTree.mutable") with MutableTreeBuilder
object Benchmark extends common.Benchmark("Benchmark") with MutableTreeBuilder