|
| 1 | +package binary_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/philandstuff/dhall-golang/v3/binary" |
| 11 | + "github.com/philandstuff/dhall-golang/v3/core" |
| 12 | + "github.com/philandstuff/dhall-golang/v3/imports" |
| 13 | + "github.com/philandstuff/dhall-golang/v3/internal" |
| 14 | + "github.com/philandstuff/dhall-golang/v3/term" |
| 15 | +) |
| 16 | + |
| 17 | +func BenchmarkDecodeLargeExpression(b *testing.B) { |
| 18 | + file, err := os.Open("../dhall-lang/tests/parser/success/largeExpressionB.dhallb") |
| 19 | + if err != nil { |
| 20 | + b.Fatal(err) |
| 21 | + } |
| 22 | + defer file.Close() |
| 23 | + fileinfo, err := file.Stat() |
| 24 | + if err != nil { |
| 25 | + b.Fatal(err) |
| 26 | + } |
| 27 | + b.SetBytes(fileinfo.Size()) |
| 28 | + for n := 0; n < b.N; n++ { |
| 29 | + file.Seek(0, io.SeekStart) |
| 30 | + binary.DecodeAsCbor(file) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func BenchmarkDecodePrelude(b *testing.B) { |
| 35 | + file := internal.NewLocalImport("../dhall-lang/Prelude/package.dhall", term.Code) |
| 36 | + t, err := imports.Load(file) |
| 37 | + if err != nil { |
| 38 | + b.Fatal(err) |
| 39 | + } |
| 40 | + v := core.Eval(t) |
| 41 | + cache, err := imports.StandardCache() |
| 42 | + if err != nil { |
| 43 | + b.Fatal(err) |
| 44 | + } |
| 45 | + hash, err := binary.SemanticHash(v) |
| 46 | + if err != nil { |
| 47 | + b.Fatal(err) |
| 48 | + } |
| 49 | + // ensure Prelude is saved into the cache |
| 50 | + cache.Save(hash, core.Quote(v)) |
| 51 | + |
| 52 | + cacheDir, err := imports.DhallCacheDir() |
| 53 | + if err != nil { |
| 54 | + b.Fatal(err) |
| 55 | + } |
| 56 | + hash16 := fmt.Sprintf("%x", hash) |
| 57 | + cborfile, err := os.Open(path.Join(cacheDir, hash16)) |
| 58 | + if err != nil { |
| 59 | + b.Fatal(err) |
| 60 | + } |
| 61 | + fileinfo, err := cborfile.Stat() |
| 62 | + if err != nil { |
| 63 | + b.Fatal(err) |
| 64 | + } |
| 65 | + b.SetBytes(fileinfo.Size()) |
| 66 | + b.ResetTimer() |
| 67 | + for n := 0; n < b.N; n++ { |
| 68 | + cborfile.Seek(0, io.SeekStart) |
| 69 | + binary.DecodeAsCbor(cborfile) |
| 70 | + } |
| 71 | +} |
0 commit comments