test083.tcl
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:4k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. # See the file LICENSE for redistribution information.
  2. #
  3. # Copyright (c) 2000
  4. # Sleepycat Software.  All rights reserved.
  5. #
  6. # $Id: test083.tcl,v 11.6 2000/12/11 17:24:55 sue Exp $
  7. #
  8. # Test 83.
  9. # Test of DB->key_range
  10. proc test083 { method {pgsz 512} {maxitems 5000} {step 2} args} {
  11. source ./include.tcl
  12. set omethod [convert_method $method]
  13. set args [convert_args $method $args]
  14. puts "Test083 $method ($args): Test of DB->key_range"
  15. if { [is_btree $method] != 1 } {
  16. puts "tTest083: Skipping for method $method."
  17. return
  18. }
  19. set pgindex [lsearch -exact $args "-pagesize"]
  20. if { $pgindex != -1 } {
  21. puts "Test083: skipping for specific pagesizes"
  22. return
  23. }
  24. # If we are using an env, then testfile should just be the db name.
  25. # Otherwise it is the test directory and the name.
  26. set eindex [lsearch -exact $args "-env"]
  27. if { $eindex == -1 } {
  28. set testfile $testdir/test083.db
  29. set env NULL
  30. } else {
  31. set testfile test083.db
  32. incr eindex
  33. set env [lindex $args $eindex]
  34. }
  35. # We assume that numbers will be at most six digits wide
  36. error_check_bad maxitems_range [expr $maxitems > 999999] 1
  37. # We want to test key_range on a variety of sizes of btree.
  38. # Start at ten keys and work up to $maxitems keys, at each step
  39. # multiplying the number of keys by $step.
  40. for { set nitems 10 } { $nitems <= $maxitems }
  41.     { set nitems [expr $nitems * $step] } {
  42. puts "tTest083.a: Opening new database"
  43. cleanup $testdir $env
  44. set db [eval {berkdb_open -create -truncate -mode 0644} 
  45.     -pagesize $pgsz $omethod $args $testfile]
  46. error_check_good dbopen [is_valid_db $db] TRUE
  47. t83_build $db $nitems
  48. t83_test $db $nitems
  49. error_check_good db_close [$db close] 0
  50. }
  51. }
  52. proc t83_build { db nitems } {
  53. source ./include.tcl
  54. puts "tTest083.b: Populating database with $nitems keys"
  55. set keylist {}
  56. puts "ttTest083.b.1: Generating key list"
  57. for { set i 0 } { $i < $nitems } { incr i } {
  58. lappend keylist $i
  59. }
  60. # With randomly ordered insertions, the range of errors we
  61. # get from key_range can be unpredictably high [#2134].  For now,
  62. # just skip the randomization step.
  63. #puts "ttTest083.b.2: Randomizing key list"
  64. #set keylist [randomize_list $keylist]
  65. #puts "ttTest083.b.3: Populating database with randomized keys"
  66. puts "ttTest083.b.2: Populating database"
  67. set data [repeat . 50]
  68. foreach keynum $keylist {
  69. error_check_good db_put [$db put key[format %6d $keynum] 
  70.     $data] 0
  71. }
  72. }
  73. proc t83_test { db nitems } {
  74. # Look at the first key, then at keys about 1/4, 1/2, 3/4, and
  75. # all the way through the database.  Make sure the key_ranges
  76. # aren't off by more than 10%.
  77. set dbc [$db cursor]
  78. error_check_good dbc [is_valid_cursor $dbc $db] TRUE
  79. puts "tTest083.c: Verifying ranges..."
  80. for { set i 0 } { $i < $nitems } 
  81.     { incr i [expr $nitems / [berkdb random_int 3 16]] } {
  82. puts "tt...key $i"
  83. error_check_bad key0 [llength [set dbt [$dbc get -first]]] 0
  84. for { set j 0 } { $j < $i } { incr j } {
  85. error_check_bad key$j 
  86.     [llength [set dbt [$dbc get -next]]] 0
  87. }
  88. set ranges [$db keyrange [lindex [lindex $dbt 0] 0]]
  89. #puts $ranges
  90. error_check_good howmanyranges [llength $ranges] 3
  91. set lessthan [lindex $ranges 0]
  92. set morethan [lindex $ranges 2]
  93. set rangesum [expr $lessthan + [lindex $ranges 1] + $morethan]
  94. roughly_equal $rangesum 1 0.05
  95. # Wild guess.
  96. if { $nitems < 500 } {
  97. set tol 0.3
  98. } elseif { $nitems > 500 } {
  99. set tol 0.15
  100. }
  101. roughly_equal $lessthan [expr $i * 1.0 / $nitems] $tol
  102. }
  103. error_check_good dbc_close [$dbc close] 0
  104. }
  105. proc roughly_equal { a b tol } {
  106. error_check_good "$a =~ $b" [expr $a - $b < $tol] 1
  107. }