Created
December 9, 2023 19:08
-
-
Save djspiewak/bc5b8e173498e9eddb4515fae83c2c06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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