uuidtools 2.1.1 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG +15 -0
- data/LICENSE.txt +202 -0
- data/README.md +37 -0
- data/Rakefile +10 -23
- data/lib/uuidtools.rb +363 -172
- data/lib/uuidtools/version.rb +16 -21
- data/spec/spec_helper.rb +2 -0
- data/spec/uuidtools/mac_address_spec.rb +442 -3
- data/spec/uuidtools/utility_spec.rb +5 -5
- data/spec/uuidtools/uuid_creation_spec.rb +50 -41
- data/spec/uuidtools/uuid_parsing_spec.rb +63 -43
- data/tasks/benchmark.rake +46 -27
- data/tasks/gem.rake +31 -18
- data/tasks/rspec.rake +55 -0
- data/tasks/yard.rake +22 -0
- data/uuidtools.gemspec +42 -0
- metadata +84 -68
- data/LICENSE +0 -20
- data/README +0 -13
- data/tasks/clobber.rake +0 -2
- data/tasks/rdoc.rake +0 -29
- data/tasks/rubyforge.rake +0 -89
- data/tasks/spec.rake +0 -64
@@ -1,4 +1,4 @@
|
|
1
|
-
require File.
|
1
|
+
require File.expand_path("../../spec_helper.rb", __FILE__)
|
2
2
|
|
3
3
|
describe SecureRandom do
|
4
4
|
it "should correctly obtain random bits" do
|
@@ -7,15 +7,15 @@ describe SecureRandom do
|
|
7
7
|
bits << SecureRandom.random_bytes(16)
|
8
8
|
end
|
9
9
|
# Check to make sure that none of the 10,000 strings were duplicates
|
10
|
-
(bits.map {|x| x.to_s}).uniq.size.
|
10
|
+
expect((bits.map {|x| x.to_s}).uniq.size).to eql(bits.size)
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should return the correct number of random bits" do
|
14
|
-
SecureRandom.random_bytes(16).size.
|
15
|
-
SecureRandom.random_bytes(6).size.
|
14
|
+
expect(SecureRandom.random_bytes(16).size).to eql(16)
|
15
|
+
expect(SecureRandom.random_bytes(6).size).to eql(6)
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should return a sane random number" do
|
19
|
-
SecureRandom.random_number(5000).
|
19
|
+
expect(SecureRandom.random_number(5000)).to be < 5000
|
20
20
|
end
|
21
21
|
end
|
@@ -1,37 +1,39 @@
|
|
1
|
-
require File.
|
1
|
+
require File.expand_path("../../spec_helper.rb", __FILE__)
|
2
2
|
|
3
3
|
describe UUIDTools::UUID, "when generating" do
|
4
4
|
it "should correctly generate SHA1 variant UUIDs" do
|
5
|
-
UUIDTools::UUID.sha1_create(
|
5
|
+
expect(UUIDTools::UUID.sha1_create(
|
6
6
|
UUIDTools::UUID_URL_NAMESPACE, 'http://sporkmonger.com'
|
7
|
-
).to_s.
|
7
|
+
).to_s).to eql('f2d04685-b787-55da-8644-9bd28a6f5a53')
|
8
8
|
end
|
9
9
|
|
10
10
|
it "should correctly generate MD5 variant UUIDs" do
|
11
|
-
UUIDTools::UUID.md5_create(
|
11
|
+
expect(UUIDTools::UUID.md5_create(
|
12
12
|
UUIDTools::UUID_URL_NAMESPACE, 'http://sporkmonger.com'
|
13
|
-
).to_s.
|
13
|
+
).to_s).to eql('15074785-9071-3fe3-89bd-876e4b9e919b')
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should correctly generate timestamp variant UUIDs" do
|
17
|
-
UUIDTools::UUID.timestamp_create.
|
18
|
-
UUIDTools::UUID.timestamp_create.to_s.
|
17
|
+
expect(UUIDTools::UUID.timestamp_create).not_to be_random_node_id
|
18
|
+
expect(UUIDTools::UUID.timestamp_create.to_s).not_to eql(
|
19
19
|
UUIDTools::UUID.timestamp_create.to_s
|
20
|
+
)
|
20
21
|
current_time = Time.now
|
21
|
-
UUIDTools::UUID.timestamp_create(current_time).to_s.
|
22
|
+
expect(UUIDTools::UUID.timestamp_create(current_time).to_s).not_to eql(
|
22
23
|
UUIDTools::UUID.timestamp_create(current_time).to_s
|
24
|
+
)
|
23
25
|
uuids = []
|
24
26
|
1000.times do
|
25
27
|
uuids << UUIDTools::UUID.timestamp_create
|
26
28
|
end
|
27
29
|
# Check to make sure that none of the 1,000 UUIDs were duplicates
|
28
|
-
(uuids.map {|x| x.to_s}).uniq.size.
|
30
|
+
expect((uuids.map {|x| x.to_s}).uniq.size).to eql(uuids.size)
|
29
31
|
end
|
30
32
|
|
31
33
|
it "should correctly generate UUIDs without a MAC address" do
|
32
34
|
mac_address = UUIDTools::UUID.mac_address
|
33
35
|
UUIDTools::UUID.mac_address = nil
|
34
|
-
UUIDTools::UUID.timestamp_create.
|
36
|
+
expect(UUIDTools::UUID.timestamp_create).to be_random_node_id
|
35
37
|
UUIDTools::UUID.mac_address = mac_address
|
36
38
|
end
|
37
39
|
|
@@ -41,81 +43,88 @@ describe UUIDTools::UUID, "when generating" do
|
|
41
43
|
uuids << UUIDTools::UUID.random_create
|
42
44
|
end
|
43
45
|
# Check to make sure that none of the 1,000 UUIDs were duplicates
|
44
|
-
(uuids.map {|x| x.to_s}).uniq.size.
|
46
|
+
expect((uuids.map {|x| x.to_s}).uniq.size).to eql(uuids.size)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should not have internal state used in string representations" do
|
50
|
+
uuid = UUIDTools::UUID.random_create
|
51
|
+
uuid_string = uuid.to_s.dup
|
52
|
+
uuid.to_s.gsub!("-", "/")
|
53
|
+
expect(uuid.to_s).to eql(uuid_string)
|
45
54
|
end
|
46
55
|
|
47
56
|
it "should throw an exception if a segment has an invalid value" do
|
48
|
-
(lambda do
|
57
|
+
expect(lambda do
|
49
58
|
UUIDTools::UUID.new(-1, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])
|
50
|
-
end).
|
51
|
-
(lambda do
|
59
|
+
end).to raise_error(ArgumentError)
|
60
|
+
expect(lambda do
|
52
61
|
UUIDTools::UUID.new(4294967296, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])
|
53
|
-
end).
|
62
|
+
end).to raise_error(ArgumentError)
|
54
63
|
end
|
55
64
|
|
56
65
|
it "should throw an exception if a segment has an invalid value" do
|
57
|
-
(lambda do
|
66
|
+
expect(lambda do
|
58
67
|
UUIDTools::UUID.new(0, -1, 0, 0, 0, [0, 0, 0, 0, 0, 0])
|
59
|
-
end).
|
60
|
-
(lambda do
|
68
|
+
end).to raise_error(ArgumentError)
|
69
|
+
expect(lambda do
|
61
70
|
UUIDTools::UUID.new(0, 65536, 0, 0, 0, [0, 0, 0, 0, 0, 0])
|
62
|
-
end).
|
71
|
+
end).to raise_error(ArgumentError)
|
63
72
|
end
|
64
73
|
|
65
74
|
it "should throw an exception if a segment has an invalid value" do
|
66
|
-
(lambda do
|
75
|
+
expect(lambda do
|
67
76
|
UUIDTools::UUID.new(0, 0, -1, 0, 0, [0, 0, 0, 0, 0, 0])
|
68
|
-
end).
|
69
|
-
(lambda do
|
77
|
+
end).to raise_error(ArgumentError)
|
78
|
+
expect(lambda do
|
70
79
|
UUIDTools::UUID.new(0, 0, 65536, 0, 0, [0, 0, 0, 0, 0, 0])
|
71
|
-
end).
|
80
|
+
end).to raise_error(ArgumentError)
|
72
81
|
end
|
73
82
|
|
74
83
|
it "should throw an exception if a segment has an invalid value" do
|
75
|
-
(lambda do
|
84
|
+
expect(lambda do
|
76
85
|
UUIDTools::UUID.new(0, 0, 0, -1, 0, [0, 0, 0, 0, 0, 0])
|
77
|
-
end).
|
78
|
-
(lambda do
|
86
|
+
end).to raise_error(ArgumentError)
|
87
|
+
expect(lambda do
|
79
88
|
UUIDTools::UUID.new(0, 0, 0, 256, 0, [0, 0, 0, 0, 0, 0])
|
80
|
-
end).
|
89
|
+
end).to raise_error(ArgumentError)
|
81
90
|
end
|
82
91
|
|
83
92
|
it "should throw an exception if a segment has an invalid value" do
|
84
|
-
(lambda do
|
93
|
+
expect(lambda do
|
85
94
|
UUIDTools::UUID.new(0, 0, 0, 0, -1, [0, 0, 0, 0, 0, 0])
|
86
|
-
end).
|
87
|
-
(lambda do
|
95
|
+
end).to raise_error(ArgumentError)
|
96
|
+
expect(lambda do
|
88
97
|
UUIDTools::UUID.new(0, 0, 0, 0, 256, [0, 0, 0, 0, 0, 0])
|
89
|
-
end).
|
98
|
+
end).to raise_error(ArgumentError)
|
90
99
|
end
|
91
100
|
|
92
101
|
it "should throw an exception if nodes are not a collection" do
|
93
|
-
(lambda do
|
102
|
+
expect(lambda do
|
94
103
|
UUIDTools::UUID.new(0, 0, 0, 0, 0, :bogus)
|
95
|
-
end).
|
104
|
+
end).to raise_error(TypeError)
|
96
105
|
end
|
97
106
|
|
98
107
|
it "should throw an exception if nodes are the wrong size" do
|
99
|
-
(lambda do
|
108
|
+
expect(lambda do
|
100
109
|
UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0])
|
101
|
-
end).
|
110
|
+
end).to raise_error(ArgumentError)
|
102
111
|
end
|
103
112
|
|
104
113
|
it "should throw an exception if any nodes have invalid values" do
|
105
|
-
(lambda do
|
114
|
+
expect(lambda do
|
106
115
|
UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 256])
|
107
|
-
end).
|
116
|
+
end).to raise_error(ArgumentError)
|
108
117
|
end
|
109
118
|
|
110
119
|
it "should throw an exception if parsing anything but a String" do
|
111
|
-
(lambda do
|
120
|
+
expect(lambda do
|
112
121
|
UUIDTools::UUID.parse(:bogus)
|
113
|
-
end).
|
122
|
+
end).to raise_error(TypeError)
|
114
123
|
end
|
115
124
|
|
116
125
|
it "should throw an exception if raw parsing anything but a String" do
|
117
|
-
(lambda do
|
126
|
+
expect(lambda do
|
118
127
|
UUIDTools::UUID.parse_raw(:bogus)
|
119
|
-
end).
|
128
|
+
end).to raise_error(TypeError)
|
120
129
|
end
|
121
130
|
end
|
@@ -1,66 +1,67 @@
|
|
1
|
-
require File.
|
1
|
+
require File.expand_path("../../spec_helper.rb", __FILE__)
|
2
2
|
|
3
3
|
describe UUIDTools::UUID, "when parsing" do
|
4
4
|
it "should correctly parse the MAC address from a timestamp version UUID" do
|
5
|
-
UUIDTools::UUID.timestamp_create.mac_address.
|
5
|
+
expect(UUIDTools::UUID.timestamp_create.mac_address).to eql(
|
6
6
|
UUIDTools::UUID.mac_address
|
7
|
+
)
|
7
8
|
end
|
8
9
|
|
9
10
|
it "should correctly parse the variant from a timestamp version UUID" do
|
10
|
-
UUIDTools::UUID.timestamp_create.variant.
|
11
|
+
expect(UUIDTools::UUID.timestamp_create.variant).to eql(0b100)
|
11
12
|
end
|
12
13
|
|
13
14
|
it "should correctly parse the version from a timestamp version UUID" do
|
14
|
-
UUIDTools::UUID.timestamp_create.version.
|
15
|
+
expect(UUIDTools::UUID.timestamp_create.version).to eql(1)
|
15
16
|
end
|
16
17
|
|
17
18
|
it "should correctly parse the timestamp from a timestamp version UUID" do
|
18
|
-
UUIDTools::UUID.timestamp_create.timestamp.
|
19
|
-
UUIDTools::UUID.timestamp_create.timestamp.
|
19
|
+
expect(UUIDTools::UUID.timestamp_create.timestamp).to be < (Time.now + 1)
|
20
|
+
expect(UUIDTools::UUID.timestamp_create.timestamp).to be > (Time.now - 1)
|
20
21
|
end
|
21
22
|
|
22
23
|
it "should not treat a timestamp version UUID as a nil UUID" do
|
23
|
-
UUIDTools::UUID.timestamp_create.
|
24
|
+
expect(UUIDTools::UUID.timestamp_create).not_to be_nil_uuid
|
24
25
|
end
|
25
26
|
|
26
27
|
it "should not treat a timestamp version UUID as a random node UUID" do
|
27
|
-
UUIDTools::UUID.timestamp_create.
|
28
|
+
expect(UUIDTools::UUID.timestamp_create).not_to be_random_node_id
|
28
29
|
end
|
29
30
|
|
30
31
|
it "should treat a timestamp version UUID as a random node UUID " +
|
31
32
|
"if there is no MAC address" do
|
32
33
|
old_mac_address = UUIDTools::UUID.mac_address
|
33
34
|
UUIDTools::UUID.mac_address = nil
|
34
|
-
UUIDTools::UUID.timestamp_create.
|
35
|
+
expect(UUIDTools::UUID.timestamp_create).to be_random_node_id
|
35
36
|
UUIDTools::UUID.mac_address = old_mac_address
|
36
37
|
end
|
37
38
|
|
38
39
|
it "should correctly identify the nil UUID" do
|
39
|
-
UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0]).
|
40
|
+
expect(UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])).to be_nil_uuid
|
40
41
|
end
|
41
42
|
|
42
43
|
it "should correctly identify timestamp version UUIDs as valid" do
|
43
|
-
UUIDTools::UUID.timestamp_create.
|
44
|
+
expect(UUIDTools::UUID.timestamp_create).to be_valid
|
44
45
|
end
|
45
46
|
|
46
47
|
it "should correctly identify random number version UUIDs as valid" do
|
47
|
-
UUIDTools::UUID.random_create.
|
48
|
+
expect(UUIDTools::UUID.random_create).to be_valid
|
48
49
|
end
|
49
50
|
|
50
51
|
it "should correctly identify SHA1 hash version UUIDs as valid" do
|
51
|
-
UUIDTools::UUID.sha1_create(
|
52
|
+
expect(UUIDTools::UUID.sha1_create(
|
52
53
|
UUIDTools::UUID_URL_NAMESPACE, 'http://sporkmonger.com'
|
53
|
-
).
|
54
|
+
)).to be_valid
|
54
55
|
end
|
55
56
|
|
56
57
|
it "should correctly identify MD5 hash version UUIDs as valid" do
|
57
|
-
UUIDTools::UUID.md5_create(
|
58
|
+
expect(UUIDTools::UUID.md5_create(
|
58
59
|
UUIDTools::UUID_URL_NAMESPACE, 'http://sporkmonger.com'
|
59
|
-
).
|
60
|
+
)).to be_valid
|
60
61
|
end
|
61
62
|
|
62
63
|
it "should not identify the nil UUID as valid" do
|
63
|
-
UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0]).
|
64
|
+
expect(UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])).not_to be_valid
|
64
65
|
end
|
65
66
|
|
66
67
|
it "should allow for sorting of UUID arrays" do
|
@@ -69,59 +70,78 @@ describe UUIDTools::UUID, "when parsing" do
|
|
69
70
|
uuids << UUIDTools::UUID.timestamp_create
|
70
71
|
end
|
71
72
|
uuids.sort!
|
72
|
-
uuids.first.
|
73
|
-
uuids.last.
|
73
|
+
expect(uuids.first).to be < uuids.last
|
74
|
+
expect(uuids.last).to be > uuids.first
|
74
75
|
end
|
75
76
|
|
76
77
|
it "should allow for comparison of UUIDs" do
|
77
|
-
UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0]).
|
78
|
+
expect(UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])).to be <
|
78
79
|
UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 1])
|
79
|
-
UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 1]).
|
80
|
-
UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])
|
81
|
-
UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0]).should ==
|
80
|
+
expect(UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 1])).to be >
|
82
81
|
UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])
|
82
|
+
expect(UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])).to eql(
|
83
|
+
UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0]))
|
83
84
|
end
|
84
85
|
|
85
86
|
it "should produce the correct hexdigest for a UUID" do
|
86
|
-
UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0]).hexdigest.
|
87
|
-
|
88
|
-
UUIDTools::UUID.new(1, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0]).hexdigest.
|
89
|
-
|
90
|
-
UUIDTools::UUID.timestamp_create.hexdigest.size.
|
87
|
+
expect(UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0]).hexdigest).to eql(
|
88
|
+
'00000000000000000000000000000000')
|
89
|
+
expect(UUIDTools::UUID.new(1, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0]).hexdigest).to eql(
|
90
|
+
'00000001000000000000000000000000')
|
91
|
+
expect(UUIDTools::UUID.timestamp_create.hexdigest.size).to eql(32)
|
91
92
|
end
|
92
93
|
|
93
94
|
it "should produce a sane hash value for a UUID" do
|
94
95
|
uuid = UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])
|
95
|
-
uuid.to_i.
|
96
|
-
uuid.hash.
|
96
|
+
expect(uuid.to_i).to eql(0)
|
97
|
+
expect(uuid.hash).to be_kind_of(Integer)
|
97
98
|
end
|
98
99
|
|
99
100
|
it "should produce the correct URI for a UUID" do
|
100
|
-
UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0]).to_uri.
|
101
|
-
|
101
|
+
expect(UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0]).to_uri).to eql(
|
102
|
+
'urn:uuid:00000000-0000-0000-0000-000000000000')
|
102
103
|
end
|
103
104
|
|
104
105
|
it "should correctly test UUID equality" do
|
105
|
-
UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0]).
|
106
|
+
expect(UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])).to eql(
|
106
107
|
UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])
|
107
108
|
)
|
108
109
|
end
|
109
110
|
|
110
111
|
it "should correctly parse integers" do
|
111
|
-
UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0]).
|
112
|
-
UUIDTools::UUID.parse_int(0)
|
113
|
-
UUIDTools::UUID.parse_int(0).
|
112
|
+
expect(UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])).to eql(
|
113
|
+
UUIDTools::UUID.parse_int(0))
|
114
|
+
expect(UUIDTools::UUID.parse_int(0)).to be_nil_uuid
|
114
115
|
uuid = UUIDTools::UUID.timestamp_create
|
115
|
-
UUIDTools::UUID.parse_int(uuid.to_i).
|
116
|
+
expect(UUIDTools::UUID.parse_int(uuid.to_i)).to eql(uuid)
|
116
117
|
end
|
117
118
|
|
118
119
|
it "should correctly parse hexdigests" do
|
119
|
-
UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0]).
|
120
|
-
UUIDTools::UUID.parse_hexdigest(
|
121
|
-
UUIDTools::UUID.parse_hexdigest(
|
122
|
-
|
123
|
-
).
|
120
|
+
expect(UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])).to eql(
|
121
|
+
UUIDTools::UUID.parse_hexdigest('00000000000000000000000000000000'))
|
122
|
+
expect(UUIDTools::UUID.parse_hexdigest(
|
123
|
+
'00000000000000000000000000000000'
|
124
|
+
)).to be_nil_uuid
|
125
|
+
uuid = UUIDTools::UUID.timestamp_create
|
126
|
+
expect(UUIDTools::UUID.parse_hexdigest(uuid.hexdigest)).to eql(uuid)
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should correctly parse raw bytes" do
|
130
|
+
# NOTE: Short Input
|
131
|
+
expect(UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])).to eql(
|
132
|
+
UUIDTools::UUID.parse_raw(""))
|
133
|
+
|
134
|
+
# NOTE: Nil Input
|
135
|
+
expect(UUIDTools::UUID.parse_raw(
|
136
|
+
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
137
|
+
)).to be_nil_uuid
|
138
|
+
|
139
|
+
# NOTE: Realistic Input
|
124
140
|
uuid = UUIDTools::UUID.timestamp_create
|
125
|
-
UUIDTools::UUID.
|
141
|
+
expect(UUIDTools::UUID.parse_raw(uuid.raw)).to eql(uuid)
|
142
|
+
|
143
|
+
# NOTE: Long input
|
144
|
+
raw192bit = "\1\2\3\4\5\6\7\8" + uuid.raw
|
145
|
+
expect(UUIDTools::UUID.parse_raw(raw192bit)).to eql(uuid)
|
126
146
|
end
|
127
147
|
end
|
data/tasks/benchmark.rake
CHANGED
@@ -1,38 +1,57 @@
|
|
1
1
|
task :benchmark do
|
2
|
-
require 'lib/uuidtools'
|
3
2
|
require 'benchmark'
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
10000.times do
|
8
|
-
UUID.timestamp_create.to_s
|
9
|
-
end
|
10
|
-
end
|
11
|
-
puts "#{(10000.0 / result.real)} version 1 per second."
|
4
|
+
$LOAD_PATH.unshift File.expand_path('../..', __FILE__)
|
5
|
+
require 'lib/uuidtools'
|
12
6
|
|
13
|
-
|
14
|
-
|
15
|
-
10000.times do
|
16
|
-
UUID.md5_create(UUID_URL_NAMESPACE,
|
17
|
-
"http://www.ietf.org/rfc/rfc4122.txt").to_s
|
18
|
-
end
|
7
|
+
def format_float(float)
|
8
|
+
("%.2f" % float).rjust(9, ' ')
|
19
9
|
end
|
20
|
-
puts "#{(10000.0 / result.real)} version 3 per second."
|
21
10
|
|
22
|
-
|
23
|
-
|
24
|
-
10000.times do
|
25
|
-
UUID.random_create.to_s
|
26
|
-
end
|
11
|
+
def format_result(result, action, iterations)
|
12
|
+
$stdout.puts "#{format_float(iterations / result.real)} | #{action}"
|
27
13
|
end
|
28
|
-
puts "#{(10000.0 / result.real)} version 4 per second."
|
29
14
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
UUID.sha1_create(UUID_URL_NAMESPACE,
|
34
|
-
"http://www.ietf.org/rfc/rfc4122.txt").to_s
|
15
|
+
def benchmark(name, n = 10000)
|
16
|
+
result = Benchmark.measure do
|
17
|
+
n.times { yield }
|
35
18
|
end
|
19
|
+
|
20
|
+
format_result(result, name, n)
|
36
21
|
end
|
37
|
-
|
22
|
+
|
23
|
+
$stdout.puts ' x/second | Benchmark'
|
24
|
+
$stdout.puts '---------- -----------'
|
25
|
+
|
26
|
+
##
|
27
|
+
# Benchmark UUID creation
|
28
|
+
namespace = UUIDTools::UUID_URL_NAMESPACE
|
29
|
+
url = "http://www.ietf.org/rfc/rfc4122.txt"
|
30
|
+
|
31
|
+
benchmark('Version 1') { UUIDTools::UUID.timestamp_create.to_s }
|
32
|
+
benchmark('Version 3') { UUIDTools::UUID.md5_create(namespace, url).to_s }
|
33
|
+
benchmark('Version 4') { UUIDTools::UUID.random_create.to_s }
|
34
|
+
benchmark('Version 5') { UUIDTools::UUID.sha1_create(namespace, url).to_s }
|
35
|
+
|
36
|
+
##
|
37
|
+
# Benchmark UUID parsing
|
38
|
+
uuid_s = UUIDTools::UUID.random_create.to_s
|
39
|
+
benchmark('UUID::parse', 40000) { UUIDTools::UUID.parse(uuid_s) }
|
40
|
+
|
41
|
+
uuid_raw = UUIDTools::UUID.random_create.raw
|
42
|
+
benchmark('UUID::parse_raw', 40000) { UUIDTools::UUID.parse_raw(uuid_raw) }
|
43
|
+
|
44
|
+
uuid_i = UUIDTools::UUID.random_create.to_i
|
45
|
+
benchmark('UUID::parse_int', 40000) { UUIDTools::UUID.parse_int(uuid_i) }
|
46
|
+
|
47
|
+
uuid_hex = UUIDTools::UUID.random_create.hexdigest
|
48
|
+
benchmark('UUID::parse_hexdigest', 40000) { UUIDTools::UUID.parse_hexdigest(uuid_hex) }
|
49
|
+
|
50
|
+
##
|
51
|
+
# Benchmark UUID private api
|
52
|
+
byte_string = UUIDTools::UUID.timestamp_create.raw
|
53
|
+
benchmark('UUID::convert_byte_string_to_int') { UUIDTools::UUID.convert_byte_string_to_int(byte_string) }
|
54
|
+
|
55
|
+
bigint = UUIDTools::UUID.timestamp_create.to_i
|
56
|
+
benchmark('UUID::convert_int_to_byte_string') { UUIDTools::UUID.convert_int_to_byte_string(bigint, 16) }
|
38
57
|
end
|