env CUE_TEST_MEMSTATS=memstats.json

env CUE_STATS_FILE=stats.json
exec cue eval x.cue
cmp stats.json out/stats.json

# overwrite existing files.
env CUE_STATS_FILE=overwrite_stats.json
exec cue eval x.cue
cmp overwrite_stats.json out/stats.json

env CUE_STATS_FILE=stats.cue
exec cue eval x.cue
cmp stats.cue out/stats.cue

env CUE_STATS_FILE=stats.yaml
exec cue eval x.cue
cmp stats.yaml out/stats.yaml

# default output is JSON.
env CUE_STATS_FILE=-
exec cue eval x.cue
cmp stderr out/stderr

# Check that EvalVersion reports the correct value.
env CUE_STATS_FILE=-
env CUE_EXPERIMENT=evalv3=0
exec cue eval x.cue
stderr -count=1 '"EvalVersion": 2,'
env CUE_EXPERIMENT=evalv3=1
exec cue eval x.cue
stderr -count=1 '"EvalVersion": 3,'

-- x.cue --
a: 1
b: 2
c: a | b
-- memstats.json --
{
    "TotalAlloc": 300456,
    "Mallocs": 100123
}
-- overwrite_stats.json --
contents overwritten
-- out/stats.json --
{
    "CUE": {
        "EvalVersion": 3,
        "Unifications": 4,
        "Disjuncts": 2,
        "Conjuncts": 8,
        "CloseIDElems": 0,
        "NumCloseIDs": 2,
        "Freed": 0,
        "Reused": 0,
        "Allocs": 6,
        "Retained": 0
    },
    "Go": {
        "AllocBytes": 300456,
        "AllocObjects": 100123
    }
}
-- out/stats.cue --
CUE: {
	EvalVersion:  3
	Unifications: 4
	Disjuncts:    2
	Conjuncts:    8
	CloseIDElems: 0
	NumCloseIDs:  2
	Freed:        0
	Reused:       0
	Allocs:       6
	Retained:     0
}
Go: {
	AllocBytes:   300456
	AllocObjects: 100123
}
-- out/stats.yaml --
CUE:
  EvalVersion: 3
  Unifications: 4
  Disjuncts: 2
  Conjuncts: 8
  CloseIDElems: 0
  NumCloseIDs: 2
  Freed: 0
  Reused: 0
  Allocs: 6
  Retained: 0
Go:
  AllocBytes: 300456
  AllocObjects: 100123
-- out/stderr --
{
    "CUE": {
        "EvalVersion": 3,
        "Unifications": 4,
        "Disjuncts": 2,
        "Conjuncts": 8,
        "CloseIDElems": 0,
        "NumCloseIDs": 2,
        "Freed": 0,
        "Reused": 0,
        "Allocs": 6,
        "Retained": 0
    },
    "Go": {
        "AllocBytes": 300456,
        "AllocObjects": 100123
    }
}
