Skip to content

Commit

Permalink
Add existence check
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszwawrzyk committed Aug 13, 2018
1 parent f44a699 commit a85248e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 20 deletions.
13 changes: 13 additions & 0 deletions src/main/scala/org/virtuslab/zipops/bench/BenchUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@ import java.io.File
import java.nio.file.{ Files, StandardCopyOption, Path }

import net.lingala.zip4j.core.ZipFile
import sbt.io.{ IO, DirectoryFilter }

trait BenchUtil {

def dirContent(dir: File): Seq[(File, String)] = {
import sbt.io.syntax._
(dir ** -DirectoryFilter).get.flatMap { extractedFile =>
IO.relativize(dir, extractedFile) match {
case Some(relPath) =>
List((extractedFile, relPath))
case _ => Nil
}
}
}

def copyResource(name: String): File = {
val resource = getClass.getResourceAsStream(s"/$name")
val tempFile = Files.createTempFile("bench", ".jar")
Expand All @@ -24,4 +36,5 @@ trait BenchUtil {
new ZipFile(zip).extractAll(targetDir.toString)
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.virtuslab.zipops.bench

import org.virtuslab.zipops.ZipOps
import java.io.File
import java.util.concurrent.TimeUnit.{ SECONDS, MILLISECONDS }

import org.openjdk.jmh.annotations._
import org.openjdk.jmh.infra.Blackhole
import org.virtuslab.zipops.ZipOps.InZipPath
import org.virtuslab.zipops.bench.ZipOpsBench._
import org.virtuslab.zipops.impl.MyZipFsOps

class BigJarExistenceTestBench extends ExistenceCheckBenchmark(BigJar)
class SmallJarExistenceTestBench extends ExistenceCheckBenchmark(SmallJar)

@State(Scope.Thread)
abstract class ExistenceCheckBenchmark(jar: String) extends ZipOpsBench with BenchUtil {

var jarFile: File = _
var extractDir: File = _
var paths: Seq[InZipPath] = _
var extractedFiles: Seq[File] = _

@Setup(Level.Trial)
def setup(): Unit = {
jarFile = copyResource(jar)
paths = MyZipFsOps.readPaths(jarFile)

extractDir = extractSomewhere(jarFile).toFile
extractedFiles = dirContent(extractDir).map(_._1)
}

@TearDown(Level.Trial)
def teardown(): Unit = {
jarFile.delete()
}

override def run(ops: ZipOps): Unit = {
val allPaths =ops.readPaths(jarFile)
paths.foreach(allPaths.contains)
}

@Benchmark
@Fork(value = 1)
@Warmup(iterations = WarmupIterations, time = WarmupIterationTime, timeUnit = SECONDS)
@Measurement(iterations = Iterations, time = IterationTime, timeUnit = SECONDS)
@BenchmarkMode(Array(Mode.AverageTime))
@OutputTimeUnit(MILLISECONDS)
def files(hole: Blackhole): Unit = {
extractedFiles.foreach(_.exists())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,6 @@ abstract class IncludeFilesBenchmark(
extractedFiles = dirContent(extractDir)
}

private def dirContent(dir: File): Seq[(File, String)] = {
import sbt.io.syntax._
(dir ** -DirectoryFilter).get.flatMap { extractedFile =>
IO.relativize(dir, extractedFile) match {
case Some(relPath) =>
List((extractedFile, relPath))
case _ => Nil
}
}
}

@TearDown(Level.Invocation)
def teardown(): Unit = {
zipFile.delete()
Expand Down
16 changes: 7 additions & 9 deletions src/main/scala/org/virtuslab/zipops/impl/ZipFsOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ object ZipFsOps extends ZipOps with WithZipFs {
}

override def readPaths(jar: File): Seq[String] = {
if (jar.exists()) {
withZipFs(jar) { fs =>
val list = new ListBuffer[Path]
Files
.walk(fs.getPath("/"))
.forEachOrdered((t: Path) => list += t)
list.map(_.toString)
}
} else Nil
withZipFs(jar) { fs =>
val list = new ListBuffer[Path]
Files
.walk(fs.getPath("/"))
.forEachOrdered((t: Path) => list += t)
list.map(_.toString)
}
}

override def createStamper(j: File): Stamper = (jar: File, cls: String) => {
Expand Down

0 comments on commit a85248e

Please sign in to comment.