<commit_before>// dirt simple sequential benchmarks

package chemistry

import java.util.Date
import java.util.concurrent._

private object NotApplicable extends Exception

private abstract class Benchmark {
  val iters = 1000000
  def name: String
  def hand: Unit
  def reagent: Unit
  def juc { throw NotApplicable }
}

private object Push extends Benchmark {
  private val d = new Date()
  def name = "Push"
  def hand {
    val s = new HandStack[java.util.Date]()
    for (i <- 1 to iters) s.push(d)
  }
  def reagent {		
    val s = new TreiberStack[java.util.Date]()
    for (i <- 1 to iters) s.push ! d 
  }
}

private object PushPop extends Benchmark {
  private val d = new Date()
  def name = "Push/pop"
  def hand {
    val s = new HandStack[java.util.Date]()
    for (i <- 1 to iters) {
      s.push(d)
      s.push(d)
      s.tryPop
      s.tryPop
    }
  } 
  def reagent {
    val s = new TreiberStack[java.util.Date]()
    for (i <- 1 to iters) {
      s.push ! d;
      s.push ! d;
      s.tryPop ! ()
      s.tryPop ! ()
    }
  }
}

private object Enq extends Benchmark {
  private val d = new Date()
  def name = "Enq"
  def hand {
    val s = new HandQueue[java.util.Date]()
    for (i <- 1 to iters) {
      s.enq(d)
    }
  } 
  def reagent {
    val s = new MSQueue[java.util.Date]()
    for (i <- 1 to iters) {
      s.enq ! d 
    }
  }
  override def juc {
    val s = new ConcurrentLinkedQueue[java.util.Date]()
    for (i <- 1 to iters) {
      s.add(d)
    }
  }
}

private object EnqDeq extends Benchmark {
  private val d = new Date()
  def name = "EnqDeq"
  def hand {
    val s = new HandQueue[java.util.Date]()
    for (i <- 1 to iters) {
      s.enq(d)
      s.tryDeq
    }
  } 
  def reagent {
    val s = new MSQueue[java.util.Date]()
    for (i <- 1 to iters) {
      s.enq ! d;
      s.tryDeq ! ()
    }
  }
  override def juc {
    val s = new ConcurrentLinkedQueue[java.util.Date]()
    for (i <- 1 to iters) {
      s.add(d)
      s.poll()
    }
  }
}

object Bench extends App {
  val trials = 5

  def bench(b: Benchmark) {
    def getTime = (new Date()).getTime
    def run(thunk: => Unit): Option[(Double,Double)] = try {
      for (i <- 1 to 3+(trials/10)) thunk  // warm up
      val times = (1 to trials).map { (_) =>
	System.gc()
	val t1 = getTime
	thunk
	val t2 = getTime	
	(t2 - t1).toDouble
      }
      val mean = times.sum/trials
      val std = scala.math.sqrt(
	times.map(x => (x - mean) * (x - mean)).sum/trials)
      val coeffOfVar = std / scala.math.abs(mean)
      Some(mean, coeffOfVar)
    } catch { 
      case _ => None
    }

    // run reagent benchmark separately, to pull out mean for normalization
    val r = run(b.reagent)
    val rm = r match { 
      case Some((m, _)) => m
      case _ => throw new Exception("Impossible")
    }

    // run the remaining benchmarks
    val metrics = List(run(b.hand), r, run(b.juc))

    // output
    print("%10.10s".format(b.name))
    metrics.map({
      case Some((m,c)) => " %7.2f %5.2f (%3.0f)".format(m, rm/m, 100 * c)
      case None => " %19s".format("N/A", "")
    }).foreach(print(_))
    println("")
  }

  println("%10.10s %19s %19s %19s".format(
    "Benchmark", "--By hand--", "--Reagent--", "--JUC--"<commit_msg>changed benchmark to report avg throughput rather than avg time<commit_after>// dirt simple sequential benchmarks

package chemistry

import java.util.Date
import java.util.concurrent._

private object NotApplicable extends Exception

private abstract class Benchmark {
  val iters = 1000000
  def name: String
  def hand: Unit
  def reagent: Unit
  def juc { throw NotApplicable }
}

private object Push extends Benchmark {
  override val iters = 2000000
  private val d = new Date()
  def name = "Push"
  def hand {
    val s = new HandStack[java.util.Date]()
    for (i <- 1 to iters) s.push(d)
  }
  def reagent {		
    val s = new TreiberStack[java.util.Date]()
    for (i <- 1 to iters) s.push ! d 
  }
}

private object PushPop extends Benchmark {
  private val d = new Date()
  def name = "Push/pop"
  def hand {
    val s = new HandStack[java.util.Date]()
    for (i <- 1 to iters) {
      s.push(d)
      s.push(d)
      s.tryPop
      s.tryPop
    }
  } 
  def reagent {
    val s = new TreiberStack[java.util.Date]()
    for (i <- 1 to iters) {
      s.push ! d;
      s.push ! d;
      s.tryPop ! ()
      s.tryPop ! ()
    }
  }
}

private object Enq extends Benchmark {
  private val d = new Date()
  def name = "Enq"
  def hand {
    val s = new HandQueue[java.util.Date]()
    for (i <- 1 to iters) {
      s.enq(d)
    }
  } 
  def reagent {
    val s = new MSQueue[java.util.Date]()
    for (i <- 1 to iters) {
      s.enq ! d 
    }
  }
  override def juc {
    val s = new ConcurrentLinkedQueue[java.util.Date]()
    for (i <- 1 to iters) {
      s.add(d)
    }
  }
}

private object EnqDeq extends Benchmark {
  private val d = new Date()
  def name = "EnqDeq"
  def hand {
    val s = new HandQueue[java.util.Date]()
    for (i <- 1 to iters) {
      s.enq(d)
      s.tryDeq
    }
  } 
  def reagent {
    val s = new MSQueue[java.util.Date]()
    for (i <- 1 to iters) {
      s.enq ! d;
      s.tryDeq ! ()
    }
  }
  override def juc {
    val s = new ConcurrentLinkedQueue[java.util.Date]()
    for (i <- 1 to iters) {
      s.add(d)
      s.poll()
    }
  }
}

object Bench extends App {
  val trials = 5

  def bench(b: Benchmark) {
    def getTime = (new Date()).getTime
    def run(thunk: => Unit): Option[(Double,Double,Double)] = try {
      for (i <- 1 to 3+(trials/10)) thunk  // warm up
      val times = (1 to trials).map { (_) =>
	System.gc()
	val t1 = getTime
	thunk
	val t2 = getTime	
	(t2 - t1).toDouble
      }
      val mean = times.sum/trials
      val std = scala.math.sqrt(
	times.map(x => (x - mean) * (x - mean)).sum/trials)
      val coeffOfVar = std / scala.math.abs(mean)
      val throughput = b.iters / (mean * 1000) // iters/microsecond
      Some(mean, coeffOfVar,throughput)
    } catch { 
      case _ => None
    }

    // run reagent benchmark separately, to pull out mean for normalization
    val r = run(b.reagent)
    val rm = r match { 
      case Some((m,_,_)) => m
      case _ => throw new Exception("Impossible")
    }

    // run the remaining benchmarks
    val metrics = List(run(b.hand), r, run(b.juc))

    // output
    print("%10.10s".format(b.name))
    metrics.map({
      case Some((m,c,t)) => 
	" %7.2f %5.2f (%3.0f)".format(t, rm/m, 100 * c)
      case None => 
	" %19s".format("N/A", "")
    }).foreach(print(_))
    println("")
  }

  println("%10.10s %19s %19s %19s".format(
    "Benchmark", "--By hand--", "--Reagent--", "--JUC--"
{'fraction_non_alphanumeric': 0.14423076923076922, 'fraction_numerical': 0.017277644230769232, 'mean_word_length': 2.1151146467009827, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6179846', 'n_tokens_mistral': 2627, 'n_tokens_neox': 2456, 'n_words': 706}