Skip to content

Instantly share code, notes, and snippets.

@nobeans
Created August 1, 2019 05:40
Show Gist options
  • Save nobeans/8edcd78b15fef0219227369e7ae09dd8 to your computer and use it in GitHub Desktop.
Save nobeans/8edcd78b15fef0219227369e7ae09dd8 to your computer and use it in GitHub Desktop.
import org.apache.commons.lang3.RandomUtils
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import org.junit.rules.TestName
import org.springframework.util.StopWatch
import spock.lang.Shared
import spock.lang.Specification
class FileIOSpec extends Specification {
static final byte[] DATA = RandomUtils.nextBytes(10_000)
static int repeatCount = 100
static int parallelCount = 400
@Rule
TestName name = new TestName()
@Rule
TemporaryFolder tempFolder = new TemporaryFolder()
@Shared
StopWatch watch = new StopWatch()
def setup() {
tempFolder.newFolder('work')
}
def cleanupSpec() {
println watch.prettyPrint()
}
def "warm up?"() {
expect:
sleep 500
true
}
def "Left shift directly to shared File from byte[]"() {
given:
def threads = [] as List<Thread>
def files = [] as List<File>
when:
watch.start(name.methodName)
parallelCount.times {
def file = tempFolder.newFile()
files << file
threads << Thread.start {
repeatCount.times {
file << DATA
}
}
}
threads*.join()
watch.stop()
then:
files.size() == parallelCount
files.each { assert it.size() == DATA.size() * repeatCount }
}
def "Left shift to FileOutputStream from byte[]"() {
given:
def threads = [] as List<Thread>
def files = [] as List<File>
when:
watch.start(name.methodName)
parallelCount.times {
def file = tempFolder.newFile()
files << file
threads << Thread.start {
repeatCount.times {
new FileOutputStream(file, true).withCloseable { out ->
out << DATA
}
}
}
}
threads*.join()
watch.stop()
then:
files.size() == parallelCount
files.each { assert it.size() == DATA.size() * repeatCount }
}
def "Left shift to FileOutputStream from ByteArrayInputStream"() {
given:
def threads = [] as List<Thread>
def files = [] as List<File>
when:
watch.start(name.methodName)
parallelCount.times {
def file = tempFolder.newFile()
files << file
threads << Thread.start {
repeatCount.times {
new FileOutputStream(file, true).withCloseable { out ->
out << new ByteArrayInputStream(DATA)
}
}
}
}
threads*.join()
watch.stop()
then:
files.size() == parallelCount
files.each { assert it.size() == DATA.size() * repeatCount }
}
def "Left shift to BufferedOutputStream from byte[]"() {
given:
def threads = [] as List<Thread>
def files = [] as List<File>
when:
watch.start(name.methodName)
parallelCount.times {
def file = tempFolder.newFile()
files << file
threads << Thread.start {
repeatCount.times {
new BufferedOutputStream(new FileOutputStream(file, true)).withCloseable { out ->
out << DATA
}
}
}
}
threads*.join()
watch.stop()
then:
files.size() == parallelCount
files.each { assert it.size() == DATA.size() * repeatCount }
}
def "Left shift to BufferedOutputStream from ByteArrayInputStream"() {
given:
def threads = [] as List<Thread>
def files = [] as List<File>
when:
watch.start(name.methodName)
parallelCount.times {
def file = tempFolder.newFile()
files << file
threads << Thread.start {
repeatCount.times {
new BufferedOutputStream(new FileOutputStream(file, true)).withCloseable { out ->
out << new ByteArrayInputStream(DATA)
}
}
}
}
threads*.join()
watch.stop()
then:
files.size() == parallelCount
files.each { assert it.size() == DATA.size() * repeatCount }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment