-- Call benchVolume() once, from the "waiting" state, after
-- the terrain has finished building. Print the two numbers.
--
-- DESTRUCTIVE: the set pass overwrites the terrain. That is
-- harmless because the world is regenerated from
-- StartingState on every launch, so relaunching restores it.

function benchVolume()
  local vox = scene.voxels
  local SX, SY, SZ = 80, 16, 80
  local cells = SX * SY * SZ
  
  -- read pass: what a full-volume save would cost
  local t0 = os.clock()
  local nonAir = 0
  for y = 0, SY - 1 do
    for z = 0, SZ - 1 do
      for x = 0, SX - 1 do
        local id = vox:get(x, y, z, BLOCK_ID)
        if id and id ~= 0 then nonAir = nonAir + 1 end
      end
    end
  end
  local getTime = os.clock() - t0
  
  -- build the string a brute force save would produce, one
  -- printable char per voxel, to get its real size
  t0 = os.clock()
  local parts = {}
  for i = 1, cells do
    parts[i] = string.char(48 + (i % 60))
  end
  local blob = table.concat(parts)
  local encodeTime = os.clock() - t0
  
  -- write pass: what a full-volume load would cost
  t0 = os.clock()
  for y = 0, SY - 1 do
    for z = 0, SZ - 1 do
      for x = 0, SX - 1 do
        vox:set(x, y, z, "Dirt")
      end
    end
  end
  local setTime = os.clock() - t0
  
  print(string.format("cells: %d, nonAir: %d", cells, nonAir))
  print(string.format("get pass:    %.3f s", getTime))
  print(string.format("string pass: %.3f s (%d bytes)", encodeTime, #blob))
  print(string.format("set pass:    %.3f s", setTime))
end
