excilys/gatling — pull request #1308 by nremond (2013-07-24 17:06:30+00:00)
Title: Caching mechanism should take into account the Cache-Control header

```scala
import scala.util.{ Try, Failure }

val maxAgePrefix = "max-age="

def extractMaxAgeValue(s: String): Option[Int] = {
var index = s.indexOf(maxAgePrefix)
if (index > 0)
Try(s.substring(maxAgePrefix.length + index).takeWhile(Character.isDigit(_)).toInt).toOption
else
None
}

def hasPositiveMaxAge(s: String) = {
val index = s.indexOf(maxAgePrefix)
val start = maxAgePrefix.length + index
if (index > 0 && start < s.length) {
val c = s.charAt(start)
if (Character.isDigit(c))
c.getNumericValue > 0
else
false
} else
false
}

@Test
@BenchmarkOptions(benchmarkRounds = 300000, warmupRounds = 80000, concurrency = 8)
def hasPositiveMaxAgeBench() {
val r = hasPositiveMaxAge("private, max-age=3600, must-revalidate")
}

@Test
@BenchmarkOptions(benchmarkRounds = 300000, warmupRounds = 80000, concurrency = 8)
def manualExtract() {
val r = extractMaxAgeValue("private, max-age=3600, must-revalidate").exists(_>0)
}

val maxAgeRegex = """max-age=(\d+)""".r
@Test
@BenchmarkOptions(benchmarkRounds = 300000, warmupRounds = 80000, concurrency = 8)
def regexExtract() {
val maxAge = for (maxAgeRegex(maxAge) <- maxAgeRegex.findFirstIn("private, max-age=3600, must-revalidate")) yield maxAge.toInt
var r = maxAge.exists(_>0)
}
```

This benchmark gives me the following scores:
```
NicoBench.regexExtract: [measured 300000 out of 380000 rounds, threads: 8 (physical processors: 4)]
round: 0.00 [+- 0.00], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 4, GC.time: 0.12, time.total: 2.48, time.warmup: 1.97, time.bench: 0.51
NicoBench.manualExtract: [measured 300000 out of 380000 rounds, threads: 8 (physical processors: 4)]
round: 0.00 [+- 0.00], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 1, GC.time: 0.10, time.total: 0.57, time.warmup: 0.24, time.bench: 0.34
NicoBench.hasPositiveMaxAgeBench: [measured 300000 out of 380000 rounds, threads: 8 (physical processors: 4)]
round: 0.00 [+- 0.00], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 0, GC.time: 0.00, time.total: 0.34, time.warmup: 0.12, time.bench: 0.22
```

WDYT ?