# Setup and teardown in BenchmarkTools

**URL:** <https://discourse.julialang.org/t/setup-and-teardown-in-benchmarktools/20972>\
**Category:** General Usage\
**Tags:** benchmark\
**Created:** [February 19, 2019, 10:14am UTC](https://discourse.julialang.org/t/setup-and-teardown-in-benchmarktools/20972 "2019-02-19T10:14:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [February 19, 2019, 10:14am UTC](https://discourse.julialang.org/t/setup-and-teardown-in-benchmarktools/20972/1 "2019-02-19T10:14:02Z")

</div>

I really have the feeling that this is something trivial, since I am doing the same setup/teardown as I found here: [https://github.com/Keno/StructIO.jl/issues/13](https://github.com/Keno/StructIO.jl/issues/13) but that approach does not work to rewind the file before each step.

So I’d like to do some performance studies on parsing binary data and created an MWE:

```julia
using BenchmarkTools

struct Foo
    a::Int32
    b::Int32
end

const fname = "test.dat"

write(fname, [Foo(1, 2)])

function read_io(io::IOStream, t::T) where T
    a = read(io, Int32)
    b = read(io, Int32)
    t(a, b)
end

const fobj = open(fname, "r")

@show read_io(fobj, Foo)

seekstart(fobj)

@time read_io(fobj, Foo)

@btime read_io($fobj, Foo) setup=seekstart($fobj)

close(fobj)

```

This throws an

```julia
> julia mwe.jl
read_io(fobj, Foo) = Foo(1, 2)
  0.000005 seconds (5 allocations: 176 bytes)
ERROR: LoadError: EOFError: read end of file
...

```

Now my simple question: shouldn’t `setup=seekstart($fobj)` rewind the file before each test? How do I benchmark this?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 19, 2019, 10:17am UTC](https://discourse.julialang.org/t/setup-and-teardown-in-benchmarktools/20972/2 "2019-02-19T10:17:17Z")

</div>

You likely also need to pass `evals = 1` to `@btime`.
