Posted to tcl by damkerngt at Fri Oct 18 13:37:00 GMT 2013view raw

  1. # Benchmark tests for [file utime].
  2. # See the ticket: http://core.tcl.tk/tcl/info/605074ede5
  3. # and the patch: http://core.tcl.tk/tcl/info/9c8b16fd68
  4. # Damkerng T., 18-Oct-2013.
  5.  
  6. # NOTE: You can run this on standard builds of Tcl 8.5/8.6 too.
  7. # ([file utime] calls will just be skipped/aborted.)
  8.  
  9.  
  10. # Benchmark results:
  11. #
  12. # The penalty of stat() calls seems to vary from machine to machine.
  13. # But it seems to be determined by the two major factors:
  14. # immediately previous and/or current disk I/O and free memory left.
  15. #
  16. # Here are the results from running this script with
  17. # Tcl 8.6.1 (damkerngt-file-utime branch):
  18. #
  19. # On Intel Core2 Duo 1.6 GHz memory 2Gb (Windows XP):
  20. # NOTE: "massive writes" and "no writes" refer to whether the directory
  21. # tmp-for-benchmarking exists before running, or not.
  22. # "Massive writes" = tmp-for-benchmarking does not exist
  23. #
  24. # Case 1: about 2/3 of memory available (1.1 Gb from 1.8 Gb)
  25. # Massive writes No writes
  26. # file mtime 29.313 seconds 27.063 seconds
  27. # file utime 19.297 seconds 18.843 seconds
  28. #
  29. # Case 2: about 1/3 of memory available (0.6 Gb from 1.8 Gb)
  30. # Massive writes No writes
  31. # file mtime 133.219 seconds 125.047 seconds
  32. # file utime 24.359 seconds 25.406 seconds
  33. #
  34. # On AMD 1.3 MHz memory 3 Gb (Ubuntu 9.10):
  35. # The remaining memory seems to have no impact even when over 90% used.
  36. # So I will report only the difference between disk I/O cases:
  37. #
  38. # Massive writes No writes
  39. # file mtime 47.188 seconds 2.966 seconds
  40. # file utime 2.680 seconds 2.797 seconds
  41.  
  42.  
  43. # Running environment.
  44. set machine "$tcl_platform(os) $tcl_platform(osVersion) $tcl_platform(machine)"
  45. puts "Tcl $tcl_patchLevel on $machine"
  46. # Basic [file mtime], [file utime] performance.
  47. proc BasicBenchmarks {} {
  48. set file \$\$touch-[pid].dat
  49. set fd [open $file w]; close $fd
  50. puts "mtime: [time {file mtime $file [clock seconds]} 20000]"
  51. catch {
  52. puts "utime: [time {file utime $file [clock seconds]} 20000]"
  53. }
  54. file delete $file
  55. puts ""
  56. }
  57. BasicBenchmarks
  58.  
  59.  
  60. # Parameter settings:
  61. # WARNING: This test will create a new directory "tmp-for-bechmarking",
  62. # and a whole lot of files and sub-directories under it.
  63. # Be sure you don't already have such a directory (you can change its name below),
  64. # and have enough disk space (default settings require at least 500 Mb).
  65. set dir tmp-for-benchmarking
  66. #~ set extras ""
  67. set extras a/b/c ;# extra depth makes it even more realistic (read "slower")
  68. set size 50000 ;# number of files to bechmark
  69. set step 1000
  70. set filesize 10240
  71. set format %0[string length $size]d
  72.  
  73. foreach v {dir extras size step filesize} {
  74. puts "$v = [set $v]"
  75. }
  76.  
  77.  
  78. # Helper routine
  79. proc Elapse {startMilliseconds} {
  80. set nmsecs [expr {[clock milliseconds] - $startMilliseconds}]
  81. return [format %.03f [expr {$nmsecs/1000.0}]]
  82. }
  83.  
  84. # Preparing files.
  85. # NOTE: You need to create all these files only once.
  86. # If you want to re-create all these files, just purge your bechmark $dir.
  87. if {![file exists $dir]} {
  88. puts "preparing files..."
  89. set n 1
  90. set start [clock milliseconds]
  91. file mkdir $dir
  92. for {set i 0} {$i < $size} {incr i} {
  93. set file [file join $dir {*}$extras {*}[split [format $format $i] ""]]
  94.  
  95. file mkdir [file dirname $file]
  96. set fd [open $file w]
  97. puts $fd [string repeat * $filesize]
  98. close $fd
  99.  
  100. if {[incr n] % $step == 0} {
  101. puts "prepare: $n files, [Elapse $start] seconds passed"
  102. }
  103. }
  104. puts "done in [Elapse $start] seconds"
  105.  
  106. puts "See what happens after massive disk writes..."
  107. BasicBenchmarks
  108. }
  109.  
  110.  
  111. # Here is the test.
  112. # It tests both [file mtime] and [file utime] (see damkerngt-file-utime branch).
  113. # Standard tclsh.exe has only [file mtime], but [file utime] will safely abort.
  114. foreach cmd {mtime utime} {
  115. if {[catch {
  116. puts "touching files (with file $cmd)..."
  117. set n 0
  118. set start [clock milliseconds]
  119. set timestamp [clock seconds]
  120. for {set i 0} {$i < $size} {incr i} {
  121. set file [file join $dir {*}$extras {*}[split [format $format $i] ""]]
  122.  
  123. file $cmd $file $timestamp
  124.  
  125. if {[incr n] % $step == 0} {
  126. puts "$cmd: $n files, [Elapse $start] seconds passed"
  127. }
  128. }
  129. puts "done in [Elapse $start] seconds"
  130. }]} {
  131. puts "aborted"
  132. }
  133. }
  134.