puts "==========="
puts "0031918: Application Framework - New binary format for fast reading part of OCAF document"
puts "==========="

set docname ${imagedir}/doc_${casename}.cbfl

NewDocument D0 BinLOcaf
UndoLimit D0 10

NewCommand D0

# set an array 1000 values from 100 to 1099
set values "100 "
for {set i 101} {$i < 1100} {incr i} {set values "$values $i"}

# set 100 arrays to sub-labels of 0:1 0:2 0:3 and 0:4
for {set lab 1} {$lab <= 4} {incr lab} {
  for {set sublab 1} {$sublab <= 100} {incr sublab} {
   set command "SetIntArray D0 0:$lab:$sublab 0 1 1000 $values"
   eval $command
   SetReal D0 0:$lab:$sublab 0.1
  }
}

CommitCommand D0

SaveAs D0 ${docname}
Close D0

# Computes the elapsed time of open and close document with a given arguments (as text).
# It launches 10 iteration, 10 actions in each. Returns time of average time of the minimum-time iteration.
proc action_time args {
  global docname
  set min_time 1000000
  for {set iter 0} {$iter < 10} {incr iter} {
    set iter_time [lindex [time {
      Open ${docname} D {*}$args
      Close D
    } 10] 0]
    puts "Iteration time $iter_time mcs"
    if {$iter_time < $min_time} {
      set min_time $iter_time
    }
  }

  return $min_time
}

set whole_time [action_time]
puts "Whole document open time $whole_time mcs"

set quater_time [action_time -read0:2]
puts "Quater of document open time $quater_time mcs"

# Check that open of quater of the document is significantly faster than open of whole.
if { [expr $quater_time * 1.5] > $whole_time } {
  puts "Error : loading of quater of the document content too slow relatively to the whole document load"
}

set four_quaters_time [action_time -read0:1 -read0:2 -read0:3 -read0:4]
puts "Four quaters of document open time $four_quaters_time mcs"

# Check that open of four quaters of the document is not too much slower than opening of the whole document.
if { [expr $four_quaters_time * 0.9] > $whole_time } {
  puts "Error : loading of four quaters of the document content too slow relatively to the whole document load"
}

set no_arrays_time [lindex [time {
  Open ${docname} D4 -skipTDataStd_IntegerArray -read0:2
}] 0]
puts "Quater of document without arrays open time $no_arrays_time mcs"

set attrs [Attributes D4 0:2:13]
if {"${attrs}" != "TDataStd_Real "} {
  puts "Error : loading of document skipping arrays contains invalid attributes list '${attrs}'"
}

if {![catch {Attributes D4 0:1:1:13}] || ![catch {Attributes D4 0:1:3:14}] || ![catch {Attributes D4 0:1:4:1}]} {
  puts "Error : loading of document skipping arrays and sub-trees contains invalid attributes list"
}

# check for appending arrays to the document from the file
set append_arrays_time [lindex [time {
  Open ${docname} D4 -append -readTDataStd_IntegerArray -read0:2 -read0:3
}] 0]
puts "Half of document arrays open time $append_arrays_time mcs"

set attrs [Attributes D4 0:2:13]
if {"${attrs}" != "TDataStd_Real TDataStd_IntegerArray "} {
  puts "Error : loading of document reading arrays separately contains invalid attributes list '${attrs}'"
}
set attrs [Attributes D4 0:3:1]
if {"${attrs}" != "TDataStd_IntegerArray "} {
  puts "Error : loading of document reading arrays separately contains invalid attributes list on subtree 3 '${attrs}'"
}
if {![catch {Attributes D4 0:1:1:13}] || ![catch {Attributes D4 0:1:4:1}]} {
  puts "Error : loading of document reading arrays separately contains invalid attributes list on 1 and 4 subtrees"
}

Close D4
