Skip to content

Commit 4e50ed9

Browse files
committed
Merge branch 'master' into hmusum/use-assertions-from-rubygem
2 parents 8c58f9a + ac112ec commit 4e50ed9

File tree

25 files changed

+142
-123
lines changed

25 files changed

+142
-123
lines changed

docker/include/feature-flags.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
{ "id" : "max-uncommitted-memory", "rules" : [ { "value" : 130000 } ] },
5252
{ "id" : "resource-limit-disk", "rules" : [ { "value" : 0.9 } ] },
5353
{ "id" : "write-config-server-session-data-as-blob", "rules" : [ { "value" : true } ] },
54+
{ "id" : "send-protobuf-querytree", "rules" : [ { "value" : true } ] },
5455
{ "id" : "unknown-config-definition", "rules" : [ { "value" : "fail" } ] },
5556
{ "id" : "content-layer-metadata-feature-level", "rules" : [ { "value" : 1 } ] },
5657
{ "id" : "search-core-transaction-log-replay-soft-memory-limit", "rules" : [ { "value" : -5 } ] },

lib/assertions.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,17 @@
44
module Assertions
55
include Test::Unit::Assertions
66

7+
# Passes if the JSON parsed value of expected string is
8+
# equal to the JSON parsed value of actual.
9+
def assert_json_string_equal(expected, actual)
10+
assert_equal(JSON.parse(expected), JSON.parse(actual))
11+
end
12+
13+
# Converts parsed canonical tensors to objects and compares
14+
def assert_tensors_equal(expected, actual)
15+
exp = TensorResult.new(expected)
16+
act = TensorResult.new(actual)
17+
assert_equal(exp, act, "Tensors should be equal: Expected #{exp} != Actual #{act}")
18+
end
19+
720
end

lib/bm25_scorer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Copyright Vespa.ai. All rights reserved.
22

3-
require 'test/unit/assertions'
3+
require 'assertions'
44

55
# Utility class to calculate bm25 scores for a document.
66
# Used by Bm25FeatureTest and SameElementOperator
77

88
class Bm25Scorer
9-
include Test::Unit::Assertions
9+
include Assertions
1010

1111
attr_reader :avg_element_length
1212
attr_reader :avg_field_length

lib/document_api_v1.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Copyright Vespa.ai. All rights reserved.
22

3-
require 'test/unit/assertions'
3+
require 'assertions'
44
require 'erb'
55
require 'set'
66
require 'http_connection_pool'
@@ -30,7 +30,7 @@ def on_document_count(doc_count)
3030

3131
# Class to use the document rest api (v1) for feed, get, visit (https://docs.vespa.ai/documentation/document-api.html)
3232
class DocumentApiV1
33-
include Test::Unit::Assertions
33+
include Assertions
3434

3535
attr_reader :host, :port
3636

lib/nodetypes/storage.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Copyright Vespa.ai. All rights reserved.
22
require "document.rb"
3-
require 'test/unit/assertions'
3+
require 'assertions'
44
require 'bucket_copy'
55
require 'nodetypes/storageclusterstate.rb'
66
require 'environment'
@@ -39,7 +39,7 @@ def restart
3939
end
4040

4141
class Storage
42-
include Test::Unit::Assertions
42+
include Assertions
4343

4444
attr_accessor :distributor, :feeder, :fleetcontroller, :storage, :stress, :clustername
4545
attr_reader :bucket_crosscheck_params

lib/performance/system.rb

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,65 @@
44

55
module Perf
66

7+
# Calculate cpu util for a time period, use start() and end()
78
class System
89
attr_reader :hostname
910

1011
def initialize(node, data = {})
1112
@node = node
12-
@hostname = node.hostname
13+
@hostname = node.hostname unless node == nil
1314
@data = data
1415
@start_cpu_used = 0
1516
@start_cpu_total = 0
1617
@end_cpu_used = 0
1718
@end_cpu_total = 0
18-
@ysar_binary = propose_ysar_gather # TODO Remove dependency on internal tooling
1919
end
2020

21-
def propose_ysar_gather
22-
@ysar_binary = "#{Environment.instance.vespa_home}/sbin/ysar_gather"
23-
@ysar_binary = "/usr/sbin/ysar_gather" unless has_ysar
21+
# For unit testing
22+
def self.create_for_testing(hostname)
23+
@hostname = hostname
24+
new(nil)
2425
end
2526

26-
def has_ysar
27-
@node.file?(@ysar_binary)
27+
def cpu_usage
28+
calculate_cpu_usage(@node.execute("cat /proc/stat", :noecho => true))
2829
end
2930

30-
def ysar_gather_cmd
31-
"#{@ysar_binary} --list --delay-mode=none"
32-
end
33-
34-
def load
35-
unless has_ysar
36-
return [0, 0]
37-
end
38-
output = @node.execute(ysar_gather_cmd, :noecho => true)
39-
output.split("\n").each do |l|
40-
if l =~ /^cput=/
41-
key,value = l.split('=')
42-
values = value.split(',').collect(&:to_i)
43-
31+
def calculate_cpu_usage(stat_output)
32+
# See 'man proc_stat' for format. Basically this sums all cpu usage types and subtracts idle time to find cpu used
33+
stat_output.split("\n").each do |line|
34+
if line =~ /^cpu /
35+
values = line.split(' ').collect(&:to_i)
36+
values.delete(0) # Remove first item ('cpu')
4437
total = values.inject(:+)
45-
used = total - values[4]
38+
# Subtract idle time to find cpu usage
39+
used = total - values[3]
4640

4741
return [used, total]
4842
end
4943
end
5044

51-
return [0, 0]
45+
[0, 0]
5246
end
5347

5448
def start
55-
@start_cpu_used, @start_cpu_total = load
49+
@start_cpu_used, @start_cpu_total = cpu_usage
5650
end
5751

5852
def end
59-
@end_cpu_used, @end_cpu_total = load
60-
cpu_util = (@end_cpu_used - @start_cpu_used).to_f / (@end_cpu_total - @start_cpu_total)
53+
@end_cpu_used, @end_cpu_total = cpu_usage
54+
set_cpu_util([@start_cpu_used, @start_cpu_total], [@end_cpu_used, @end_cpu_total])
55+
end
56+
57+
def set_cpu_util(start_data, end_data)
58+
cpu_util = (end_data[0] - start_data[0]).to_f / (end_data[1] - start_data[1]).to_f
6159
@data['cpuutil'] = cpu_util.nan? ? 0.0 : cpu_util
6260
end
6361

62+
def cpu_util
63+
@data['cpuutil']
64+
end
65+
6466
def fill
6567
Proc.new do |result|
6668
result.add_metric('cpuutil', @data['cpuutil'], @hostname)

lib/test/stat_output_end.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cpu 2755034 16808 1456367 173042706 7719 2816574 108368 0 0 0
2+
cpu0 685797 3110 361564 43255672 1177 711966 29935 0 0 0
3+
cpu1 687168 5049 364759 43258167 2339 705184 26893 0 0 0
4+
cpu2 692538 3254 364614 43263697 2158 699850 25656 0 0 0
5+
cpu3 689530 5394 365430 43265168 2044 699572 25883 0 0 0
6+
intr 1445969025 0 6636264 591611701 0 0 0 23 0 0 0 844414262 0 0 0 0 0 0 0 0 0 0 0 0 0 7374 0 0 0 24 0 3 5 0 0 0 16 0 160 402398 0 0 3 0 0 5 0 1404376 0 963393 529018 0 0 0 0 0 0 0 0 0 0 0 0 0 0
7+
ctxt 2507040035
8+
btime 1760582953
9+
processes 702537
10+
procs_running 1
11+
procs_blocked 0
12+
softirq 119678384 0 21829652 122 17066458 1308031 0 430 63152146 0 16321545

lib/test/stat_output_start.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cpu 2745670 16808 1449026 172238106 7687 2803796 107300 0 0 0
2+
cpu0 683460 3110 359716 43054709 1170 708656 29642 0 0 0
3+
cpu1 684784 5049 362895 43057120 2330 701954 26628 0 0 0
4+
cpu2 690206 3254 362803 43062430 2150 696701 25398 0 0 0
5+
cpu3 687219 5394 363611 43063845 2034 696483 25631 0 0 0
6+
intr 1442433360 0 6624196 590984852 0 0 0 23 0 0 0 841531182 0 0 0 0 0 0 0 0 0 0 0 0 0 7340 0 0 0 24 0 3 5 0 0 0 16 0 156 391687 0 0 3 0 0 5 0 1402901 0 962570 528397 0 0 0 0 0 0 0 0 0 0 0 0 0 0
7+
ctxt 2501296161
8+
btime 1760582953
9+
processes 694100
10+
procs_running 2
11+
procs_blocked 0
12+
softirq 118835475 0 21603679 122 17063113 1306592 0 422 62703317 0 16158230

lib/test/test_result_model.rb

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,6 @@ def parameter(name)
2525

2626
class ResultModelTest < Test::Unit::TestCase
2727

28-
def notest_readwrite
29-
begin
30-
f = Tempfile.new('result.xml')
31-
path = f.path
32-
f.close
33-
r = Perf::Result.new('5.0')
34-
35-
r.fbench = { 'runtime' => 555, '99p' => '5.5', '95p' => '1.5', 'successfulrequests' => 5 }
36-
host = flexmock(Perf::System.new('localhost'))
37-
host.should_receive(:data).and_return({ 'cpu_util' => 99.98 })
38-
r.add_host host
39-
40-
r.write(path)
41-
42-
r2 = Perf::Result.read(path)
43-
44-
assert_equal(r.vespa_version, r2.vespa_version)
45-
assert_equal(r.fbench['successfulrequests'], r2.fbench['successfulrequests'])
46-
assert_equal(r.hosts.size, r2.hosts.size)
47-
assert_equal(r.host('localhost').data['cpu_util'], r2.host('localhost').data['cpu_util'])
48-
ensure
49-
File.unlink(path)
50-
end
51-
end
52-
5328
def test_custom_producer_parsing
5429
# Float
5530
m1 = MockModel.new("1.5")

lib/test/test_system.rb

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,17 @@
11
# Copyright Vespa.ai. All rights reserved.
2-
require 'flexmock/test_unit'
3-
require 'rubygems'
4-
require 'test/unit'
5-
62
require 'performance/system'
3+
require 'test/unit'
74

85
class SystemTest < Test::Unit::TestCase
9-
class MockResult
10-
attr_reader :metrics
11-
def initialize
12-
@metrics = {}
13-
end
14-
15-
def add_metric(name, value, tag)
16-
@metrics[name + ':' + tag] = value
17-
end
18-
end
196

207
def test_cpuutil
21-
node = flexmock('node')
22-
node.should_receive(:file?).and_return(true)
23-
node.should_receive(:execute).times(2).
24-
and_return(IO.read(File.join(File.dirname(__FILE__), "ysar_gather_output.txt")),
25-
IO.read(File.join(File.dirname(__FILE__), "ysar_gather_output2.txt")))
26-
node.should_receive(:hostname).
27-
and_return('localhost')
28-
system = flexmock(Perf::System.new(node, {}))
29-
system.start
30-
system.end
31-
filler = system.fill
32-
result = MockResult.new
33-
filler.call(result)
34-
assert_in_delta(0.999900049975012,
35-
result.metrics['cpuutil:localhost'],
8+
system = Perf::System.create_for_testing('localhost')
9+
start_cpu_used, start_cpu_total = system.calculate_cpu_usage(IO.read(File.join(File.dirname(__FILE__), "stat_output_start.txt")))
10+
end_cpu_used, end_cpu_total = system.calculate_cpu_usage(IO.read(File.join(File.dirname(__FILE__), "stat_output_end.txt")))
11+
system.set_cpu_util([start_cpu_used, start_cpu_total], [end_cpu_used, end_cpu_total])
12+
13+
assert_in_delta(0.03661832197254973,
14+
system.cpu_util,
3615
0.00000001)
3716
end
3817

0 commit comments

Comments
 (0)