Skip to content

Instantly share code, notes, and snippets.

@djspiewak
Created December 9, 2023 19:08
Show Gist options
  • Save djspiewak/bc5b8e173498e9eddb4515fae83c2c06 to your computer and use it in GitHub Desktop.
Save djspiewak/bc5b8e173498e9eddb4515fae83c2c06 to your computer and use it in GitHub Desktop.
@State(Scope.Thread)
class FilesBenchmark {
private[this] var target: Path = _
@Setup
def setup() = {
val file = File.createTempFile("fs2-benchmarks-", "-walk")
file.delete()
file.mkdir()
target = Path(file.toString) // ewwwww
val MaxDepth = 4
val Names = 'A'.to('E').toList.map(_.toString)
def loop(cwd: File, depth: Int): Unit = {
if (depth < MaxDepth) {
Names foreach { name =>
val sub = new File(cwd, name)
sub.mkdir()
loop(sub, depth + 1)
}
} else if (depth == MaxDepth) {
Names foreach { name =>
val sub = new File(cwd, name)
sub.createNewFile()
loop(sub, depth + 1)
}
}
}
loop(file, 0)
}
@Benchmark
def countFiles(): Int = {
Files[IO]
.walk(target)
.chunks
.map(_.size)
.fold(0)(_ + _)
.compile
.lastOrError
.unsafeRunSync()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment