wsdirector-cli 0.5.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,115 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "erb"
4
- require "json"
5
- require "wsdirector/ext/deep_dup"
6
-
7
- module WSDirector
8
- # Read and parse different scenarios
9
- class ScenarioReader
10
- using WSDirector::Ext::DeepDup
11
-
12
- class << self
13
- include WSDirector::Utils
14
-
15
- def parse(scenario)
16
- contents =
17
- if File.file?(scenario)
18
- parse_file(scenario)
19
- else
20
- [JSON.parse(scenario)]
21
- end.flatten
22
-
23
- if contents.first.key?("client")
24
- contents = transform_with_loop(contents, multiple: true)
25
- parse_multiple_scenarios(contents)
26
- else
27
- contents = transform_with_loop(contents)
28
- {"total" => 1, "clients" => [parse_simple_scenario(contents)]}
29
- end
30
- end
31
-
32
- private
33
-
34
- JSON_FILE_FORMAT = /.+.(json)\z/.freeze
35
- private_constant :JSON_FILE_FORMAT
36
-
37
- def parse_file(file)
38
- if file.match?(JSON_FILE_FORMAT)
39
- JSON.parse(File.read(file))
40
- else
41
- ::YAML.load(ERB.new(File.read(file)).result) # rubocop:disable Security/YAMLLoad
42
- end
43
- end
44
-
45
- def handle_steps(steps)
46
- steps.flat_map.with_index do |step, id|
47
- if step.is_a?(Hash)
48
- type, data = step.to_a.first
49
-
50
- data["sample"] = [1, parse_multiplier(data["sample"])].max if data["sample"]
51
-
52
- multiplier = parse_multiplier(data.delete("multiplier") || "1")
53
- Array.new(multiplier) { {"type" => type, "id" => id}.merge(data) }
54
- else
55
- {"type" => step, "id" => id}
56
- end
57
- end
58
- end
59
-
60
- def parse_simple_scenario(
61
- steps,
62
- multiplier: 1, name: "default", ignore: nil, protocol: "base"
63
- )
64
- {
65
- "multiplier" => multiplier,
66
- "steps" => handle_steps(steps),
67
- "name" => name,
68
- "ignore" => ignore,
69
- "protocol" => protocol
70
- }
71
- end
72
-
73
- def parse_multiple_scenarios(definitions)
74
- total_count = 0
75
- clients = definitions.map.with_index do |client, i|
76
- _, client = client.to_a.first
77
- multiplier = parse_multiplier(client.delete("multiplier") || "1")
78
- name = client.delete("name") || (i + 1).to_s
79
- total_count += multiplier
80
- ignore = parse_ingore(client.fetch("ignore", nil))
81
- parse_simple_scenario(
82
- client.fetch("actions", []),
83
- multiplier: multiplier,
84
- name: name,
85
- ignore: ignore,
86
- protocol: client.fetch("protocol", "base")
87
- )
88
- end
89
- {"total" => total_count, "clients" => clients}
90
- end
91
-
92
- def transform_with_loop(contents, multiple: false)
93
- contents.flat_map do |content|
94
- loop_data = content.dig("client", "loop") || content.dig("loop")
95
- next content unless loop_data
96
-
97
- loop_multiplier = parse_multiplier(loop_data["multiplier"] || "1")
98
-
99
- if multiple
100
- content["client"]["actions"] = (loop_data["actions"] * loop_multiplier).map(&:deep_dup)
101
- content
102
- else
103
- loop_data["actions"] * loop_multiplier
104
- end
105
- end
106
- end
107
-
108
- def parse_ingore(str)
109
- return unless str
110
-
111
- Array(str)
112
- end
113
- end
114
- end
115
- end
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "wsdirector/client"
4
- require "wsdirector/protocols"
5
-
6
- module WSDirector
7
- # Single client operator
8
- class Task
9
- attr_reader :global_holder, :client
10
-
11
- def initialize(config, global_holder:, result:)
12
- @ignore = config.fetch("ignore")
13
- @steps = config.fetch("steps")
14
- @global_holder = global_holder
15
- @result = result
16
-
17
- protocol_class = Protocols.get(config.fetch("protocol", "base"))
18
- @protocol = protocol_class.new(self)
19
- end
20
-
21
- def run
22
- connect!
23
-
24
- steps.each(&protocol)
25
-
26
- result.succeed
27
- rescue Error => e
28
- result.failed(e.message)
29
- end
30
-
31
- def sampled?(step)
32
- return true unless step["sample"]
33
-
34
- id, max = step["id"], step["sample"]
35
-
36
- result.track_sample(id, max)
37
- end
38
-
39
- private
40
-
41
- attr_reader :steps, :result, :protocol
42
-
43
- def connect!
44
- protocol.init_client(ignore: @ignore)
45
- end
46
- end
47
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module WSDirector
4
- module Utils # :nodoc:
5
- MULTIPLIER_FORMAT = /^[-+*\/\\\d ]+$/
6
-
7
- def parse_multiplier(str)
8
- prepared = str.to_s.gsub(":scale", WSDirector.config.scale.to_s)
9
- raise WSDirector::Error, "Unknown multiplier format: #{str}" unless
10
- MULTIPLIER_FORMAT.match?(prepared)
11
-
12
- eval(prepared) # rubocop:disable Security/Eval
13
- end
14
- end
15
- end
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module WSDirector
4
- VERSION = "0.5.0"
5
- end
@@ -1,3 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "wsdirector/cli"
data/lib/wsdirector.rb DELETED
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "concurrent"
4
- require "yaml"
5
- require "json"
6
-
7
- require "wsdirector/version"
8
- require "wsdirector/configuration"
9
- require "wsdirector/utils"
10
-
11
- # Command line tool for testing websocket servers using scenarios.
12
- module WSDirector
13
- class Error < StandardError
14
- end
15
-
16
- class << self
17
- def config
18
- @config ||= Configuration.new
19
- end
20
- end
21
- end
22
-
23
- require "wsdirector/cli"