<commit_before>package scala.reactive.core
package concurrent



import java.util.concurrent.ConcurrentLinkedQueue
import org.scalameter.api._



class SnapQueueBenches extends PerformanceTest.OfflineReport {

  def sizes(from: Int, until: Int) =
    Gen.range("size")(from, until, (until - from) / 4)

  val stringSnapQueue = new SnapQueue[String]

  def emptySegs(from: Int, until: Int) = for (sz <- sizes(from, until)) yield {
    new stringSnapQueue.Segment(sz)
  }

  def fullSegs(from: Int, until: Int) = for (sz <- sizes(from, until)) yield {
    val seg = new stringSnapQueue.Segment(sz)
    Util.fillStringSegment(stringSnapQueue)(seg)
    seg
  }

  def linkedQueues(from: Int, unt: Int) = for (sz <- sizes(from, unt)) yield {
    val q = new ConcurrentLinkedQueue[String]()
    for (i <- 0 until sz) q.add("")
    (q, sz)
  }

  val opts = Context(
    exec.minWarmupRuns -> 100,
    exec.maxWarmupRuns -> 200,
    exec.benchRuns -> 60,
    exec.independentSamples -> 8
  )

  performance of "enqueue-1-thread" config(opts) in {
    val from = 100000
    val until = 500000

    using(emptySegs(from, until)) curve("Segment.enq") setUp {
      seg => seg.reinitialize()
    } in { seg =>
      var i = 0
      while (i < seg.capacity) {
        seg.enq(seg.READ_LAST(), "")
        i += 1
      }
    }

    using(emptySegs(from, until)) curve("Segment.enqueue") setUp {
      seg => seg.reinitialize()
    } in { seg =>
      var i = 0
      while (i < seg.capacity) {
        seg.enqueue("")
        i += 1
      }
    }

    using(emptySegs(from, until)) curve("SnapQueue.Segment.enqueue") setUp {
      seg => seg.reinitialize()
    } in { seg =>
      stringSnapQueue.WRITE_ROOT(seg)
      var i = 0
      while (i < seg.capacity) {
        stringSnapQueue.enqueue("")
        i += 1
      }
    }

    using(sizes(from, until)) curve("SnapQueue.Segment.alloc+enqueue") in { sz =>
      val seg = new stringSnapQueue.Segment(sz)
      stringSnapQueue.WRITE_ROOT(seg)
      var i = 0
      while (i < seg.capacity) {
        stringSnapQueue.enqueue("")
        i += 1
      }
    }

    using(sizes(from, until)) curve("SnapQueue(64).enqueue") in { sz =>
      val snapq = new SnapQueue[String](64)
      var i = 0
      while (i < sz) {
        snapq.enqueue("")
        i += 1
      }
    }

    using(sizes(from, until)) curve("ConcurrentLinkedQueue") in { sz =>
      val queue = new ConcurrentLinkedQueue[String]
      var i = 0
      while (i < sz) {
        queue.add("")
        i += 1
      }
    }
  }

  performance of "dequeue-1-thread" config(opts) in {
    val from = 100000
    val until = 500000

    using(fullSegs(from, until)) curve("Segment.deq") setUp {
      seg => seg.WRITE_HEAD(0)
    } in { seg =>
      var i = 0
      while (i < seg.capacity) {
        seg.deq()
        i += 1
      }
    }

    using(linkedQueues(from, until)) curve("ConcurrentLinkedQueue") setUp {
      case (queue, sz) =><commit_msg>Add `SnapQueue#dequeue` benchmark.<commit_after>package scala.reactive.core
package concurrent



import java.util.concurrent.ConcurrentLinkedQueue
import org.scalameter.api._



class SnapQueueBenches extends PerformanceTest.OfflineReport {

  def sizes(from: Int, until: Int) =
    Gen.range("size")(from, until, (until - from) / 4)

  val stringSnapQueue = new SnapQueue[String]

  def emptySegs(from: Int, until: Int) = for (sz <- sizes(from, until)) yield {
    new stringSnapQueue.Segment(sz)
  }

  def fullSegs(from: Int, until: Int) = for (sz <- sizes(from, until)) yield {
    val seg = new stringSnapQueue.Segment(sz)
    Util.fillStringSegment(stringSnapQueue)(seg)
    seg
  }

  def emptySnapQueue(len: Int, from: Int, until: Int) =
    for (sz <- sizes(from, until)) yield (new SnapQueue[String](), sz)

  def linkedQueues(from: Int, unt: Int) = for (sz <- sizes(from, unt)) yield {
    val q = new ConcurrentLinkedQueue[String]()
    for (i <- 0 until sz) q.add("")
    (q, sz)
  }

  val opts = Context(
    exec.minWarmupRuns -> 100,
    exec.maxWarmupRuns -> 200,
    exec.benchRuns -> 60,
    exec.independentSamples -> 8
  )

  performance of "enqueue-1-thread" config(opts) in {
    val from = 100000
    val until = 500000

    using(emptySegs(from, until)) curve("Segment.enq") setUp {
      seg => seg.reinitialize()
    } in { seg =>
      var i = 0
      while (i < seg.capacity) {
        seg.enq(seg.READ_LAST(), "")
        i += 1
      }
    }

    using(emptySegs(from, until)) curve("Segment.enqueue") setUp {
      seg => seg.reinitialize()
    } in { seg =>
      var i = 0
      while (i < seg.capacity) {
        seg.enqueue("")
        i += 1
      }
    }

    using(emptySegs(from, until)) curve("SnapQueue.Segment.enqueue") setUp {
      seg => seg.reinitialize()
    } in { seg =>
      stringSnapQueue.WRITE_ROOT(seg)
      var i = 0
      while (i < seg.capacity) {
        stringSnapQueue.enqueue("")
        i += 1
      }
    }

    using(sizes(from, until)) curve("SnapQueue.Segment.alloc+enqueue") in { sz =>
      val seg = new stringSnapQueue.Segment(sz)
      stringSnapQueue.WRITE_ROOT(seg)
      var i = 0
      while (i < seg.capacity) {
        stringSnapQueue.enqueue("")
        i += 1
      }
    }

    using(sizes(from, until)) curve("SnapQueue(64).enqueue") in { sz =>
      val snapq = new SnapQueue[String](64)
      var i = 0
      while (i < sz) {
        snapq.enqueue("")
        i += 1
      }
    }

    using(sizes(from, until)) curve("ConcurrentLinkedQueue") in { sz =>
      val queue = new ConcurrentLinkedQueue[String]
      var i = 0
      while (i < sz) {
        queue.add("")
        i += 1
      }
    }
  }

  performance of "dequeue-1-thread" config(opts) in {
    val from = 100000
    val until = 500000

    using(fullSegs(from, until)) curve("Segment.deq") setUp {
      seg => seg.WRITE_HEAD(0)
    } in { seg =>
      var i = 0
      while (i < seg.capacity) {
        seg.deq()
        i += 1
      }
    }

    using(emptySnapQueue(64, from, until)) curve("SnapQueue(64).deq") setUp {
      case (q, sz) => for (i <- 0 until sz) q.enqueue("")
    } in {
      case (q, sz) =>
      var i = 0
      while (i < sz) {
        q.dequeue()
        i += 1
      }
    }

    using(linkedQueues(from, until)) curve("ConcurrentLinkedQueue") setUp {
      case (queue, sz) =>
{'fraction_non_alphanumeric': 0.14068199841395718, 'fraction_numerical': 0.01887390959555908, 'mean_word_length': 2.2929503916449088, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 37, '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': '3408437', 'n_tokens_mistral': 2306, 'n_tokens_neox': 2132, 'n_words': 524}