4
4
5
5
module SolidCache
6
6
class EntrySizeEstimateTest < ActiveSupport ::TestCase
7
+ setup do
8
+ @encrypted = SolidCache . configuration . encrypt?
9
+ end
10
+
7
11
test "write and read cache entries" do
8
12
assert_equal 0 , estimate ( samples : 10 )
9
13
end
10
14
11
15
test "gets exact estimate when samples sizes are big enough" do
12
16
write_entries ( value_lengths : [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] )
13
17
14
- assert_equal 1535 , estimate ( samples : 12 )
15
- assert_equal 1535 , estimate ( samples : 10 )
16
- assert_equal 1688 , estimate ( samples : 6 )
17
- assert_equal 1689 , estimate ( samples : 5 )
18
+ assert_equal @encrypted ? 3235 : 1535 , estimate ( samples : 12 )
19
+ assert_equal @encrypted ? 3235 : 1535 , estimate ( samples : 10 )
20
+ assert_equal @encrypted ? 3558 : 1688 , estimate ( samples : 6 )
21
+ assert_equal @encrypted ? 3559 : 1689 , estimate ( samples : 5 )
18
22
end
19
23
20
24
test "test larger sample estimates" do
21
25
values_lengths = with_fixed_srand ( 1 ) { 1000 . times . map { ( rand **2 * 1000 ) . to_i } }
22
26
write_entries ( value_lengths : values_lengths )
23
27
24
- assert_equal 481257 , estimate ( samples : 1000 )
25
- assert_equal 481662 , estimate ( samples : 500 )
26
- with_fixed_srand ( 1 ) { assert_equal 501624 , estimate ( samples : 100 ) }
27
- with_fixed_srand ( 1 ) { assert_equal 477621 , estimate ( samples : 50 ) }
28
- with_fixed_srand ( 1 ) { assert_equal 471878 , estimate ( samples : 10 ) }
28
+ assert_equal @encrypted ? 651257 : 481257 , estimate ( samples : 1000 )
29
+ assert_equal @encrypted ? 651832 : 481662 , estimate ( samples : 500 )
30
+ with_fixed_srand ( 1 ) { assert_equal @encrypted ? 680804 : 501624 , estimate ( samples : 100 ) }
31
+ with_fixed_srand ( 1 ) { assert_equal @encrypted ? 660541 : 477621 , estimate ( samples : 50 ) }
32
+ with_fixed_srand ( 1 ) { assert_equal @encrypted ? 692368 : 471878 , estimate ( samples : 10 ) }
29
33
end
30
34
31
35
test "test with gaps in records estimates" do
@@ -34,12 +38,12 @@ class EntrySizeEstimateTest < ActiveSupport::TestCase
34
38
first_mod = Entry . first . id % 3
35
39
Entry . where ( "id % 3 = #{ first_mod } " ) . delete_all
36
40
37
- assert_equal 324532 , estimate ( samples : 1000 )
38
- assert_equal 324741 , estimate ( samples : 500 )
39
- with_fixed_srand ( 1 ) { assert_equal 323946 , estimate ( samples : 334 ) }
40
- with_fixed_srand ( 1 ) { assert_equal 345103 , estimate ( samples : 100 ) }
41
- with_fixed_srand ( 1 ) { assert_equal 335770 , estimate ( samples : 50 ) }
42
- with_fixed_srand ( 1 ) { assert_equal 281944 , estimate ( samples : 10 ) }
41
+ assert_equal @encrypted ? 437752 : 324532 , estimate ( samples : 1000 )
42
+ assert_equal @encrypted ? 438131 : 324741 , estimate ( samples : 500 )
43
+ with_fixed_srand ( 1 ) { assert_equal @encrypted ? 437166 : 323946 , estimate ( samples : 334 ) }
44
+ with_fixed_srand ( 1 ) { assert_equal @encrypted ? 462859 : 345103 , estimate ( samples : 100 ) }
45
+ with_fixed_srand ( 1 ) { assert_equal @encrypted ? 453859 : 335770 , estimate ( samples : 50 ) }
46
+ with_fixed_srand ( 1 ) { assert_equal @encrypted ? 401216 : 281944 , estimate ( samples : 10 ) }
43
47
end
44
48
45
49
test "test with more gaps in records estimates" do
@@ -48,12 +52,12 @@ class EntrySizeEstimateTest < ActiveSupport::TestCase
48
52
first_mod = Entry . first . id % 4
49
53
Entry . where ( "id % 4 != #{ first_mod } " ) . delete_all
50
54
51
- assert_equal 120304 , estimate ( samples : 1000 )
52
- assert_equal 121488 , estimate ( samples : 500 )
53
- with_fixed_srand ( 1 ) { assert_equal 121188 , estimate ( samples : 250 ) }
54
- with_fixed_srand ( 1 ) { assert_equal 126768 , estimate ( samples : 100 ) }
55
- with_fixed_srand ( 1 ) { assert_equal 132657 , estimate ( samples : 50 ) }
56
- with_fixed_srand ( 1 ) { assert_equal 25537 , estimate ( samples : 10 ) }
55
+ assert_equal @encrypted ? 162804 : 120304 , estimate ( samples : 1000 )
56
+ assert_equal @encrypted ? 165348 : 121488 , estimate ( samples : 500 )
57
+ with_fixed_srand ( 1 ) { assert_equal @encrypted ? 164704 : 121188 , estimate ( samples : 250 ) }
58
+ with_fixed_srand ( 1 ) { assert_equal @encrypted ? 174266 : 126768 , estimate ( samples : 100 ) }
59
+ with_fixed_srand ( 1 ) { assert_equal @encrypted ? 179794 : 132657 , estimate ( samples : 50 ) }
60
+ with_fixed_srand ( 1 ) { assert_equal @encrypted ? 44016 : 25537 , estimate ( samples : 10 ) }
57
61
end
58
62
59
63
test "overestimate when all samples sizes are the same" do
@@ -62,11 +66,11 @@ class EntrySizeEstimateTest < ActiveSupport::TestCase
62
66
# estimate in this case.
63
67
write_entries ( value_lengths : [ 1 ] * 1000 )
64
68
65
- assert_equal 149000 , estimate ( samples : 1000 )
66
- assert_equal 297851 , estimate ( samples : 999 )
67
- assert_equal 223500 , estimate ( samples : 500 )
68
- with_fixed_srand ( 1 ) { assert_equal 272422 , estimate ( samples : 6 ) }
69
- with_fixed_srand ( 1 ) { assert_equal 326906 , estimate ( samples : 5 ) }
69
+ assert_equal @encrypted ? 319000 : 149000 , estimate ( samples : 1000 )
70
+ assert_equal @encrypted ? 637681 : 297851 , estimate ( samples : 999 )
71
+ assert_equal @encrypted ? 478500 : 223500 , estimate ( samples : 500 )
72
+ with_fixed_srand ( 1 ) { assert_equal @encrypted ? 583238 : 272422 , estimate ( samples : 6 ) }
73
+ with_fixed_srand ( 1 ) { assert_equal @encrypted ? 699886 : 326906 , estimate ( samples : 5 ) }
70
74
end
71
75
72
76
private
0 commit comments