duel-mysql 0.1.8 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2839b8ed6c5fd6afa8e7d5d7fa84c99ffd34b169bc7a5cd28706a5dc153fab37
4
- data.tar.gz: 1b95ad9e59ccb9d8450195c1955c30661943b7cc364c53b5f5e598039ed866ae
3
+ metadata.gz: 2c584095f7d338193a078423e5bfb28b9a13875782eb70d319c54c5914325a87
4
+ data.tar.gz: ed5184bb16077a96fb533e4d929ad912c3da70c415f40f7749cfbe76e5b220a6
5
5
  SHA512:
6
- metadata.gz: 78fc25d07516255f740ef0c58f2ff934fc54178e1582dbd9ec3ec2c1056753eef582567c49debb5abe37a967b6a7daf2beb162852fefd8661632f7773e912eb5
7
- data.tar.gz: 1a2a20de6194aec43062ec66aea1f8302b2c9c36db6ef198572a924d1c71f74096ee59c7d6b972c9c2ba21844a00252bc543a15e14117dc710638a5ab77dc055
6
+ metadata.gz: d59b5925afa3ae9159940305ed665168427a39afe0e4f1765e55b317e88ec62d71327df2fc3d53ab94585bdd0619c73b203d89b0778142fe9cd2bf7d2cd52ac1
7
+ data.tar.gz: cfca73f43a632757029a4aa0e8b07d2fb1cc8204b4f690f9d6e633beaefba3e4557e3bd29af8ad17135319d67a85b93136e666fcea6e5fea8a3c16d4c43f94b8
data/CHANGELOG.md CHANGED
@@ -7,6 +7,67 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.3.0] - 2026-11-20
11
+
12
+ ### Added
13
+
14
+ - libmysqlclient's `MYSQL_UNIX_PORT` (but on Windows) and, with `tcp`,
15
+ `MYSQL_TCP_PORT` point at each worker's server, as duel-postgresql's
16
+ `PGHOST` and `PGPORT` do, so a mysql2 client given no socket or port
17
+ connects to it, not to the machine's MySQL.
18
+
19
+ ### Changed
20
+
21
+ - `sql:` is what each worker's database runs once its server starts
22
+ (a schema, say), in place of `setup:`, which is a hook's name
23
+ everywhere else in Duel.
24
+ - `settings:` gives each worker's server more of its system variables,
25
+ as a Hash (`settings: {sort_buffer_size: "4M"}`, passed as
26
+ `--sort_buffer_size=4M`), as duel-postgresql's `settings:` does, in
27
+ place of `args:`, a list of mysqld's options, as duel-redis's and
28
+ duel-memcached's `settings:` do too. They come last, so they win over
29
+ the plugin's own.
30
+ - `server:` is found by Duel's `Duel.find_program`, as duel-postgresql's
31
+ is: a path is expanded (`~/mysql/bin/mysqld`, say), and a directory
32
+ named `mysqld` on the `PATH` isn't taken for the program.
33
+ - It requires Duel 0.3.0 or later (`~> 0.3.0`), the release it's
34
+ tested with, as Duel 0.3.0 changes what plugins and benchmarks call.
35
+
36
+ ### Fixed
37
+
38
+ - With a `server:` path that isn't there, the error no longer says
39
+ it's not on the PATH, and it asks for mysqld's path, as
40
+ duel-postgresql's does for postgres's.
41
+
42
+ ## [0.2.0] - 2026-11-16
43
+
44
+ ### Added
45
+
46
+ - `tcp: true` starts each worker's server on a port of 127.0.0.1's
47
+ alone, its URL `mysql2://root@127.0.0.1:PORT/duel`, with no
48
+ `DUEL_MYSQL_SOCKET`. It's the default on Windows, whose MySQL has no
49
+ Unix socket.
50
+
51
+ ### Changed
52
+
53
+ - The plugin reads the server's statistics with a client of its own,
54
+ in MySQL's protocol, so it no longer needs the trilogy gem (or a
55
+ build of it for Windows). `Duel::MySQL.execute` and `.reset`
56
+ return a `Duel::MySQL::Result`, and a statement that fails raises
57
+ `Duel::MySQL::Error`. Reading a server's statistics allocates a few
58
+ more objects than it did with Trilogy, which parsed MySQL's replies in
59
+ C (58 a reading, against 53.5, in the plugin's own benchmark), and
60
+ takes the same time. It gives up on a server that hasn't answered in
61
+ 10 seconds, raising `IOError`, where it would wait forever.
62
+
63
+ ### Fixed
64
+
65
+ - A `server:` path with a backslash in it (Windows's,
66
+ `C:\mysql\bin\mysqld`) is run as it is: the plugin looked for a
67
+ program by that name on the `PATH`.
68
+ - On Windows, `mysqld.exe` on the `PATH` (or a `server:` path without
69
+ its `.exe`) is found: the plugin looked for a file named `mysqld`.
70
+
10
71
  ## [0.1.8] - 2026-11-12
11
72
 
12
73
  ### Added
@@ -110,7 +171,9 @@ or later (not MariaDB).
110
171
  CI.
111
172
  - RBS signatures in sig/, checked with Steep.
112
173
 
113
- [Unreleased]: https://github.com/sferik/duel-ruby/compare/duel-mysql-v0.1.8...HEAD
174
+ [Unreleased]: https://github.com/sferik/duel-ruby/compare/duel-mysql-v0.3.0...HEAD
175
+ [0.3.0]: https://github.com/sferik/duel-ruby/compare/duel-mysql-v0.2.0...duel-mysql-v0.3.0
176
+ [0.2.0]: https://github.com/sferik/duel-ruby/compare/duel-mysql-v0.1.8...duel-mysql-v0.2.0
114
177
  [0.1.8]: https://github.com/sferik/duel-ruby/compare/duel-mysql-v0.1.7...duel-mysql-v0.1.8
115
178
  [0.1.7]: https://github.com/sferik/duel-ruby/compare/duel-mysql-v0.1.6...duel-mysql-v0.1.7
116
179
  [0.1.6]: https://github.com/sferik/duel-ruby/compare/duel-mysql-v0.1.5...duel-mysql-v0.1.6
data/README.md CHANGED
@@ -48,9 +48,8 @@ end
48
48
 
49
49
  > [!NOTE]
50
50
  > It needs MySQL 8.0.30 or later (not MariaDB: its `performance_schema`
51
- > differs) and the [trilogy](https://github.com/trilogy-libraries/trilogy)
52
- > gem, so CRuby (TruffleRuby 40.0 fails inside trilogy once benchmarks
53
- > run). Your code can use any client: trilogy is only the plugin's own.
51
+ > differs). The plugin reads the server's statistics with a small client
52
+ > of its own, so it needs no client gem, and your code can use any.
54
53
 
55
54
  ## Usage
56
55
 
@@ -62,16 +61,22 @@ Duel.configure { |c| c.use :mysql }
62
61
  `c.use` requires the plugin, so `.duel.rb` doesn't need to.
63
62
 
64
63
  Each worker's server listens only on a Unix socket in the worker's own
65
- directory (no `my.cnf` is read), with no binary log, little memory,
64
+ directory (or with `tcp: true`, on a port of 127.0.0.1's alone, as it
65
+ does by default on Windows, whose MySQL has no Unix socket). No
66
+ `my.cnf` is read, and it runs with no binary log, little memory,
66
67
  nothing written to disk that needn't be, and InnoDB's statistics left as
67
68
  they are (recalculated on their own, they'd change a query's plan while
68
69
  it's measured: `ANALYZE TABLE` in a setup if you need them). Its URL is
69
- in `DUEL_MYSQL_URL`, and in `DATABASE_URL` too, so a client that reads
70
- that finds it and nothing reaches another database. The socket's path
71
- is in `DUEL_MYSQL_SOCKET`. Benchmarks can call `Duel::MySQL.url`,
70
+ in `DUEL_MYSQL_URL`, and in `DATABASE_URL` too, and libmysqlclient's
71
+ `MYSQL_UNIX_PORT` (but on Windows) and, with `tcp`, `MYSQL_TCP_PORT`
72
+ point at it, so a client that reads any of them finds it and nothing
73
+ reaches another database. (mysql2 reads the last two when it's given no
74
+ socket or port, though not its user, `root`. Trilogy reads none of
75
+ them.) The socket's path is in `DUEL_MYSQL_SOCKET` (with `tcp`, there's
76
+ none). Benchmarks can call `Duel::MySQL.url`,
72
77
  `Duel::MySQL.socket`, `Duel::MySQL.execute` and `Duel::MySQL.reset`
73
- (each of these two opens a connection of its own, from the worker's
74
- `DUEL_MYSQL_SOCKET`, and closes it).
78
+ (each of these two opens a connection of its own to the worker's
79
+ server, and closes it).
75
80
 
76
81
  > [!IMPORTANT]
77
82
  > `execute` and `reset` are for setting up: call them in a `setup` hook,
@@ -95,10 +100,11 @@ Options:
95
100
  ```ruby
96
101
  c.use :mysql,
97
102
  server: "/usr/local/mysql/bin/mysqld", # the program (default: DUEL_MYSQL_SERVER, or mysqld)
98
- args: ["--sort-buffer-size=4M"], # more of its options
99
- setup: File.read("db/structure.sql"), # SQL each worker's database runs first
100
- env: %w[DATABASE_URL PRIMARY_URL], # variables given its URL besides DUEL_MYSQL_URL
101
- scheme: "trilogy" # the URL's (Rails takes it for the adapter, mysql2 by default)
103
+ settings: {sort_buffer_size: "4M"}, # more of its system variables, as --sort_buffer_size=4M (they win)
104
+ sql: File.read("db/structure.sql"), # SQL each worker's database runs first
105
+ env: %w[DATABASE_URL PRIMARY_URL], # variables given its URL besides DUEL_MYSQL_URL (default: DATABASE_URL)
106
+ scheme: "trilogy", # the URL's (Rails takes it for the adapter, mysql2 by default)
107
+ tcp: true # on a port of 127.0.0.1's, its URL mysql2://root@127.0.0.1:PORT/duel (default: on Windows only)
102
108
  ```
103
109
 
104
110
  > [!WARNING]
@@ -112,8 +118,8 @@ Initializing a data directory takes seconds, so it's done once (Duel's
112
118
  database created in it, in your project's `tmp/duel/cache`, and every
113
119
  worker (in that run and the next ones) starts with a copy. One is made
114
120
  for each server program (and version of the plugin): another MySQL gets
115
- its own. `duel --clean` deletes them. (`args` are the running server's,
116
- not its initialization's.)
121
+ its own. `duel clean` deletes them. (`settings` are the running
122
+ server's, not its initialization's.)
117
123
 
118
124
  The metrics come from the server's status (`Questions`) and
119
125
  `performance_schema` (its statement summaries), which MySQL updates as
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Duel
4
4
  module MySQL
5
- VERSION = "0.1.8"
5
+ VERSION = "0.3.0"
6
6
  end
7
7
  end
data/lib/duel/mysql.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require "duel"
4
4
  require "digest"
5
5
  require "fileutils"
6
+ require "socket"
6
7
  require_relative "mysql/version"
7
8
 
8
9
  module Duel
@@ -25,25 +26,37 @@ module Duel
25
26
  # .reset in setup (or untimed blocks): while a benchmark is measured,
26
27
  # they fail.
27
28
  #
28
- # The server listens only on a Unix socket in the worker's directory;
29
- # its URL is in DUEL_MYSQL_URL, and in DATABASE_URL too (unless +env+
30
- # names others), and the socket's path in DUEL_MYSQL_SOCKET.
29
+ # The server listens only on a Unix socket in the worker's directory
30
+ # (or with +tcp+, on a port of 127.0.0.1's alone: by default on Windows,
31
+ # whose MySQL has none); its URL is in DUEL_MYSQL_URL, and in
32
+ # DATABASE_URL too (unless +env+ names others), and the socket's path
33
+ # in DUEL_MYSQL_SOCKET (with +tcp+, none). libmysqlclient's
34
+ # MYSQL_UNIX_PORT (where there are Unix sockets) and MYSQL_TCP_PORT
35
+ # (with +tcp+) point at it too. The plugin's connections are a client
36
+ # of its own (see Connection): no gem's.
31
37
  module MySQL
32
38
  Duel.plugin(:mysql, self)
33
39
 
34
40
  URL = "DUEL_MYSQL_URL"
35
41
  SOCKET = "DUEL_MYSQL_SOCKET"
42
+
43
+ # Where libmysqlclient (mysql2's, the mysql command's) connects when
44
+ # it's given no socket or port.
45
+ UNIX_PORT = "MYSQL_UNIX_PORT"
46
+ TCP_PORT = "MYSQL_TCP_PORT"
47
+
36
48
  DATABASE = "duel"
37
49
  USER = "root"
38
50
 
39
51
  # The server's options (after --no-defaults: no my.cnf of the
40
- # machine's): no network, nothing to disk that needn't be, little
52
+ # machine's): errors to its output (its log: on Windows, they'd go to a
53
+ # file of their own), nothing to disk that needn't be, little
41
54
  # memory, no Linux AIO contexts (there are only so many, for every
42
55
  # server on the machine), and nothing running on its own
43
56
  # (recalculating a table's statistics would change its plans while
44
57
  # they're measured).
45
58
  OPTIONS = %w[
46
- --skip-networking --mysqlx=OFF --skip-log-bin
59
+ --console --mysqlx=OFF --skip-log-bin
47
60
  --innodb-buffer-pool-size=32M --innodb-redo-log-capacity=8M --innodb-doublewrite=OFF --innodb-use-native-aio=OFF
48
61
  --innodb-flush-log-at-trx-commit=0 --innodb-stats-auto-recalc=OFF --event-scheduler=OFF --performance-schema=ON
49
62
  ].freeze
@@ -58,49 +71,63 @@ module Duel
58
71
  def self.worker_memory = 450 << 20
59
72
 
60
73
  # Configures Duel for the plugin (c.use :mysql): the server and its
61
- # metrics. +server+ is the program, +args+ more of its options,
62
- # +setup+ SQL each worker's database runs when it's started (a
74
+ # metrics. +server+ is the program, +settings+ more of its system
75
+ # variables (sort_buffer_size: "4M", as --sort_buffer_size=4M), +sql+ what each worker's database runs when it's started (a
63
76
  # schema, say), +env+ the variables given its URL besides
64
77
  # DUEL_MYSQL_URL, and +scheme+ the URL's (Rails takes it for the
65
78
  # adapter). A data directory is initialized once (Resource#cached),
66
79
  # and each worker's is a copy. Returns the plugin's own connection
67
80
  # to it, a worker's (see Config#in_worker, and in tests,
68
81
  # Duel::Testing.in_worker).
69
- def self.configure(config, server: ENV.fetch("DUEL_MYSQL_SERVER", "mysqld"), args: [], setup: nil, env: %w[DATABASE_URL],
70
- scheme: "mysql2")
82
+ def self.configure(config, server: ENV.fetch("DUEL_MYSQL_SERVER", "mysqld"), settings: {}, sql: nil, env: %w[DATABASE_URL],
83
+ scheme: "mysql2", tcp: !Duel.unix_sockets?)
71
84
  config.prepare(:worker) do |w|
72
- program = DataDir.find_server(server) || w.failed("no #{server} on the PATH (give it as server:, or DUEL_MYSQL_SERVER)")
85
+ program = Duel.find_program(server) ||
86
+ w.fail!("no #{server}#{" on the PATH" unless Duel.path?(server)} (give mysqld's path as server:, or DUEL_MYSQL_SERVER)")
73
87
  data = w.path("data")
74
88
  DataDir.new(w, program).create(data)
89
+ # (A Unix socket even with +tcp+, which MySQL can't do without:
90
+ # its own default's in a directory of the system's.)
75
91
  socket = w.path("mysqld.sock")
76
- w.spawn(program, *DataDir.options(data), "--socket=#{socket}", "--pid-file=#{w.path("mysqld.pid")}", "--tmpdir=#{w.dir}",
77
- *(RUNNING + args))
78
- w.wait_until { Connection.ping(socket) }
79
- run(socket, setup) if setup
80
- url = "#{scheme}://#{USER}@localhost/#{DATABASE}?socket=#{socket}"
81
- {URL => url, SOCKET => socket}.merge(env.to_h { |name| [name, url] })
82
- end
83
- connection = config.in_worker { |w| Connection.new(socket).tap { |mysql| w.at_stop { mysql.close } } }
92
+ if tcp
93
+ port = w.free_port
94
+ listen = ["--port=#{port}", "--bind-address=127.0.0.1", "--socket=#{socket}"]
95
+ address = "127.0.0.1:#{port}"
96
+ url = "#{scheme}://#{USER}@#{address}/#{DATABASE}"
97
+ vars = {URL => url, TCP_PORT => port.to_s}
98
+ else
99
+ # (No network at all.)
100
+ address = socket
101
+ listen = ["--skip-networking", "--socket=#{socket}"]
102
+ url = "#{scheme}://#{USER}@localhost/#{DATABASE}?socket=#{address}"
103
+ vars = {URL => url, SOCKET => address}
104
+ end
105
+ # (libmysqlclient's defaults, for a client given no socket or port:
106
+ # its port, for 127.0.0.1, and its socket, which it takes for
107
+ # localhost, wherever there's one, even with +tcp+.)
108
+ vars[UNIX_PORT] = socket if Duel.unix_sockets?
109
+ # (+settings+' last: of an option given twice, mysqld takes the
110
+ # last.)
111
+ w.spawn(program, *(DataDir.options(data) + listen), "--pid-file=#{w.path("mysqld.pid")}", "--tmpdir=#{w.dir}", *RUNNING,
112
+ *settings.map { |name, value| "--#{name}=#{value}" })
113
+ # (Stopped with SHUTDOWN, before it's killed: on Windows, mysqld is
114
+ # a monitor, the server its child, which killing it would leave
115
+ # running.)
116
+ w.at_stop { shutdown(address) }
117
+ w.wait_until { Connection.ping(address) }
118
+ run(address, sql) if sql
119
+ vars.merge(env.to_h { |name| [name, url] })
120
+ end
121
+ connection = config.in_worker { |w| Connection.new(address).tap { |mysql| w.at_stop { mysql.close } } }
84
122
  statements = Statements.new(connection)
85
123
  config.metric(METRICS) { statements.read }.costly.details { |stretches, sample| statements.details(stretches, sample.n) }
86
124
  connection
87
125
  end
88
126
 
89
- # Loads Trilogy, as the first connection is made: not as the plugin
90
- # loads, in .duel.rb, before a worker loads its version. In trilogy's
91
- # own repository, trilogy is what's measured, and the worker would load
92
- # whichever trilogy the load path led to first (the bundle's release,
93
- # say), and keep it. (In the runner, which loads no version, the
94
- # bundle's.)
95
- # mutant:disable -- the tests' process has Trilogy loaded from the start,
96
- # so a require changes nothing there: test_loads_trilogy_only_when_used
97
- # checks it, in a process of its own
98
- def self.require_trilogy = require("trilogy")
99
-
100
- # Runs SQL on a connection of its own to the server on +socket+;
101
- # returns the last statement's result.
102
- def self.run(socket, sql)
103
- connection = Connection.new(socket)
127
+ # Runs SQL on a connection of its own to the server at +address+ (its
128
+ # socket's path, or HOST:PORT); returns the last statement's result.
129
+ def self.run(address, sql)
130
+ connection = Connection.new(address)
104
131
  begin
105
132
  connection.execute(sql)
106
133
  ensure
@@ -108,6 +135,13 @@ module Duel
108
135
  end
109
136
  end
110
137
 
138
+ # Asks the server at +address+ to shut down, if it's there to ask.
139
+ def self.shutdown(address)
140
+ run(address, "SHUTDOWN")
141
+ rescue Error, SystemCallError, IOError
142
+ nil # (gone already, or gone as it answered)
143
+ end
144
+
111
145
  # Server time isn't the worker's CPU time: another process's, on the
112
146
  # clock, so what else the machine does shows in it. It's judged on
113
147
  # each benchmark, but left out across the board, where the margin is
@@ -117,7 +151,7 @@ module Duel
117
151
  "mysql_queries" => {kind: :counted, unit: %w[query queries], calibrate: false},
118
152
  "mysql_rows" => {kind: :counted, unit: %w[row rows], calibrate: false},
119
153
  "mysql_rows_examined" => {kind: :counted, unit: %w[row rows], calibrate: false},
120
- "mysql_time" => {kind: :ratio, unit: "µs", floor: 300, across: false}
154
+ "mysql_time" => {kind: :timed, unit: "µs", floor: 300, across: false}
121
155
  }
122
156
 
123
157
  # What the worker's server has done so far, but for this connection
@@ -167,7 +201,7 @@ module Duel
167
201
  # (Connection#quote'd: the plugin's own readings'). A statement's run
168
202
  # (LAST_SEEN) is when it ended, but it's counted just after.
169
203
  DIGESTS = <<~SQL
170
- SELECT DIGEST, COUNT_STAR, SUM_ROWS_SENT + SUM_ROWS_AFFECTED, SUM_ROWS_EXAMINED, SUM_TIMER_WAIT
204
+ SELECT COUNT_STAR, SUM_ROWS_SENT + SUM_ROWS_AFFECTED, SUM_ROWS_EXAMINED, SUM_TIMER_WAIT, DIGEST
171
205
  FROM performance_schema.events_statements_summary_by_digest
172
206
  WHERE DIGEST IS NOT NULL AND UNIX_TIMESTAMP(LAST_SEEN) >= %d AND DIGEST NOT IN (%s)
173
207
  SQL
@@ -194,15 +228,12 @@ module Duel
194
228
  end
195
229
  end
196
230
 
197
- # The same by measure (time in µs).
198
- def self.stats(connection) = Statements.by_measure(totals(connection))
199
-
200
231
  # What the server has done so far, by statement, read over
201
232
  # +connection+: those run since +since+ (seconds since the epoch), but
202
- # the +own+ digests, each one's [digest, queries, rows, rows examined,
203
- # time (ps)].
233
+ # the +own+ digests, each one's [queries, rows, rows examined, time
234
+ # (ps), digest].
204
235
  def self.digests(connection, since, own)
205
- connection.query(format(DIGESTS, since, own.map { |digest| connection.quote(digest) }.join(", "))).rows
236
+ connection.query(format(DIGESTS, since, connection.quote_all(own))).rows
206
237
  end
207
238
 
208
239
  # The digest of each of +statements+, read over +connection+.
@@ -211,32 +242,41 @@ module Duel
211
242
  # The texts of the statements with these digests, by digest, read
212
243
  # over +connection+.
213
244
  def self.texts(connection, digests)
214
- connection.query(format(TEXTS, digests.map { |digest| connection.quote(digest) }.join(", "))).to_h
245
+ connection.query(format(TEXTS, connection.quote_all(digests))).to_h
215
246
  end
216
247
 
217
248
  # For benchmarks: the worker's server.
218
249
  def self.url = ENV.fetch(URL)
219
250
 
220
- # Its socket's path (for a client that takes it, not a URL).
251
+ # Its socket's path (for a client that takes it, not a URL). With
252
+ # +tcp+, there's none (see .url).
221
253
  def self.socket = ENV.fetch(SOCKET)
222
254
 
255
+ # Where it is, for the plugin's own connections: its socket's path, or
256
+ # (with +tcp+) its URL's HOST:PORT, in +env+ (a worker's).
257
+ def self.address(env = ENV)
258
+ # (Or the URL's: one the plugin made, so it has a HOST:PORT, which
259
+ # Steep can't tell.)
260
+ env.fetch(SOCKET) { env.fetch(URL)[%r{@([^/]+)/}, 1] } # steep:ignore BlockBodyTypeMismatch
261
+ end
262
+
223
263
  # Runs SQL (statements, ;-separated) on it, over a connection of its
224
264
  # own; returns the last one's result. For setting up: it fails while
225
265
  # a benchmark is measured (Duel.measuring?), as what it runs would be
226
266
  # counted.
227
267
  def self.execute(sql)
228
268
  Duel.unmeasured!("Duel::MySQL.execute")
229
- run(socket, sql)
269
+ run(address, sql)
230
270
  end
231
271
 
232
272
  # Empties every table in its database, restarting their
233
273
  # AUTO_INCREMENTs. For setting up, as execute is.
234
274
  def self.reset
235
275
  Duel.unmeasured!("Duel::MySQL.reset")
236
- tables = run(socket, "SELECT table_name FROM information_schema.tables " \
276
+ tables = run(address, "SELECT table_name FROM information_schema.tables " \
237
277
  "WHERE table_schema = DATABASE() AND table_type = 'BASE TABLE'").map(&:first)
238
278
  # (Only this connection's: it's closed after.)
239
- run(socket, "SET FOREIGN_KEY_CHECKS = 0#{tables.map { |table| "; TRUNCATE TABLE `#{table}`" }.join}")
279
+ run(address, "SET FOREIGN_KEY_CHECKS = 0#{tables.map { |table| "; TRUNCATE TABLE `#{table}`" }.join}")
240
280
  end
241
281
 
242
282
  # Running totals of what the worker's server did, all told (#read),
@@ -251,10 +291,6 @@ module Duel
251
291
  SHOWN = 3
252
292
  TEXT = 80
253
293
 
254
- # A statement not read yet, as a row of MySQL.digests: it did
255
- # nothing.
256
- UNREAD = [nil, 0, 0, 0, 0].freeze
257
-
258
294
  # A statement that did nothing yet.
259
295
  NOTHING = [0, 0, 0, 0].freeze
260
296
 
@@ -264,9 +300,10 @@ module Duel
264
300
  # No statement changed.
265
301
  UNCHANGED = [] #: Array[String]
266
302
 
267
- # A reading's context: every statement's row as of it, by digest (a
268
- # Hash it shares with the last reading, if none changed), the last
269
- # reading's, and the digests of those that changed since.
303
+ # A reading's context: every statement's row as of it (its numbers,
304
+ # then its digest), by digest (a Hash it shares with the last
305
+ # reading, if none changed), the last reading's, and the digests of
306
+ # those that changed since.
270
307
  Reading = Data.define(:rows, :previous, :changed)
271
308
 
272
309
  # How long before a reading (in seconds) the next one reads the
@@ -340,7 +377,7 @@ module Duel
340
377
  rows = previous
341
378
  changed = UNCHANGED
342
379
  MySQL.digests(connection, since, own).each do |row|
343
- digest = row.fetch(0)
380
+ digest = row.last
344
381
  next if row == previous[digest]
345
382
 
346
383
  # (The last reading's Hash is its own: copied, at the first change.)
@@ -372,9 +409,9 @@ module Duel
372
409
  digests = after.previous.equal?(before) ? after.changed : after.rows.keys
373
410
  digests.each do |digest|
374
411
  now = after.rows.fetch(digest)
375
- was = before.fetch(digest, UNREAD)
412
+ was = before.fetch(digest, NOTHING)
376
413
  so_far = grew.fetch(digest, NOTHING)
377
- grew[digest] = Array.new(NAMES.size) { |i| so_far.fetch(i) + now.fetch(i + 1) - was.fetch(i + 1) }
414
+ grew[digest] = Array.new(NAMES.size) { |i| so_far.fetch(i) + now.fetch(i) - was.fetch(i) }
378
415
  end
379
416
  end
380
417
  grew.select { |_, more| more.any?(&:positive?) }
@@ -391,48 +428,372 @@ module Duel
391
428
  def shorten(text) = (text.length > TEXT) ? "#{text[0, TEXT - 1]}…" : text
392
429
  end
393
430
 
394
- # A connection to a worker's server, as root, to its database.
431
+ # What a connection to a worker's server raises where it fails: a
432
+ # statement's error (its code and message), or a reply it can't read.
433
+ class Error < Duel::Error
434
+ # The server's error code, or nil (not the server's error).
435
+ attr_reader :code
436
+
437
+ def initialize(message, code: nil)
438
+ super(message)
439
+ @code = code
440
+ end
441
+ end
442
+
443
+ # A query's result: its columns' names, and its rows, each value a
444
+ # String, but an integer column's an Integer, a floating-point one's a
445
+ # Float, and NULL nil. (One that returns none, an INSERT's, say, has
446
+ # neither.)
447
+ class Result
448
+ include Enumerable
449
+
450
+ attr_reader :fields, :rows
451
+
452
+ # (Given in order, not by keyword: a keyword's Hash would be made for
453
+ # every query.)
454
+ def initialize(fields, rows)
455
+ @fields = fields
456
+ @rows = rows
457
+ end
458
+
459
+ def each(&) = rows.each(&)
460
+ end
461
+
462
+ # The least a connection needs to read a server's statistics: not the
463
+ # code's own, whose statements are what's measured. As root (with no
464
+ # password: the data directory's initialized with none), to its
465
+ # database, over its Unix socket (a path), or over TCP (HOST:PORT: on
466
+ # Windows), in MySQL's text protocol: statements, several to a query
467
+ # if need be, and their results, the last one's returned.
395
468
  class Connection
396
- # Whether a server answers on +socket+.
397
- def self.ping(socket)
398
- new(socket).close
399
- rescue Trilogy::Error
469
+ # What the client says it can do: protocol 4.1's packets, a
470
+ # database to connect to, transactions' statuses, 4.1's (and 8.0's)
471
+ # authentication, several statements to a query, and their results.
472
+ LONG_PASSWORD = 0x1
473
+ CONNECT_WITH_DB = 0x8
474
+ PROTOCOL_41 = 0x200
475
+ TRANSACTIONS = 0x2000
476
+ SECURE_CONNECTION = 0x8000
477
+ MULTI_STATEMENTS = 0x10000
478
+ MULTI_RESULTS = 0x20000
479
+ PLUGIN_AUTH = 0x80000
480
+ CAPABILITIES = LONG_PASSWORD | CONNECT_WITH_DB | PROTOCOL_41 | TRANSACTIONS | SECURE_CONNECTION | MULTI_STATEMENTS | MULTI_RESULTS |
481
+ PLUGIN_AUTH
482
+ # The largest packet it takes, and its character set (utf8mb4).
483
+ MAX_PACKET = (1 << 24) - 1
484
+ UTF8MB4 = 45
485
+
486
+ # A packet's first byte: an OK, an error, an end of rows (EOF, of
487
+ # fewer than 9 bytes), or more of authentication's (caching_sha2_
488
+ # password's: FAST_AUTH, the password matched what it had cached).
489
+ OK = 0x00
490
+ ERR = 0xFF
491
+ EOF = 0xFE
492
+ AUTH_SWITCH = 0xFE
493
+ AUTH_MORE = 0x01
494
+ FAST_AUTH = 0x03
495
+ COM_QUERY = 0x03
496
+ # A NULL value in a row, and lenenc integers' prefixes.
497
+ NULL = 0xFB
498
+ # An OK's or EOF's status: more results to come.
499
+ MORE_RESULTS = 0x08
500
+
501
+ # The column types read as Integers (TINY, SHORT, LONG, LONGLONG,
502
+ # INT24, YEAR) and as Floats (FLOAT, DOUBLE).
503
+ INTEGERS = [1, 2, 3, 8, 9, 13].freeze
504
+ FLOATS = [4, 5].freeze
505
+
506
+ # What the client says it is when the server's gone.
507
+ CLOSED = "MySQL closed the connection"
508
+
509
+ # How long, in seconds, it waits for the server's next packet before
510
+ # it gives up: a server that's stuck (or sent a packet's length
511
+ # wrong) would have it wait forever.
512
+ TIMEOUT = 10
513
+
514
+ # Whether a server answers at +address+.
515
+ def self.ping(address)
516
+ new(address).close
517
+ rescue Error, SystemCallError, IOError
400
518
  false
401
519
  end
402
520
 
403
- def initialize(socket)
404
- MySQL.require_trilogy
405
- # (As root: trilogy's user, unless it's given another.)
406
- @client = Trilogy.new(socket: socket, database: DATABASE, multi_statement: true)
521
+ # The host and port of a HOST:PORT address, or nil (a Unix socket's
522
+ # path).
523
+ def self.address(address)
524
+ match = %r{\A([^:/]+):(\d+)\z}.match(address) or return
525
+ [match[1], Integer(match[2])]
407
526
  end
408
527
 
409
- def query(sql) = @client.query(sql)
528
+ # A socket to +address+, without TCP's delay before a small write
529
+ # (a statement), whose reply would wait for it.
530
+ def self.socket(address)
531
+ host_and_port = address(address)
532
+ return UNIXSocket.new(address) unless host_and_port
410
533
 
411
- # +text+ as a string literal.
412
- def quote(text) = "'#{@client.escape(text)}'"
534
+ host, port = host_and_port
535
+ # (RBS's setsockopt takes an Integer for the value, but Ruby's
536
+ # takes true too.)
537
+ TCPSocket.new(host, port).tap { |socket| socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, true) } # steep:ignore ArgumentTypeMismatch
538
+ end
413
539
 
414
- # Runs statements; returns the last one's result.
415
- def execute(sql)
416
- result = query(sql)
417
- result = @client.next_result while @client.more_results_exist?
418
- result
540
+ def initialize(address, database: DATABASE, timeout: TIMEOUT)
541
+ @socket = Connection.socket(address)
542
+ @timeout = timeout
543
+ # (Buffers of its own, binary (one byte a byte), read into
544
+ # and written from again and again: a packet's header, and its
545
+ # payload, which is read before the next one is, and what's written
546
+ # before a payload. Where the packet's read up to (@at), the last
547
+ # one's sequence number, and whether more results follow the last
548
+ # one read, are set as each is read.)
549
+ @header = "".b
550
+ @packet = "".b
551
+ @out = "".b
552
+ begin
553
+ authenticate(database)
554
+ rescue Exception # rubocop:disable Lint/RescueException
555
+ close
556
+ raise
557
+ end
558
+ end
559
+
560
+ # Runs +sql+ (statements, ;-separated); returns the last one's
561
+ # result. A statement that fails raises its Error.
562
+ def query(sql)
563
+ write(sql, 0, COM_QUERY)
564
+ loop do
565
+ result = next_result
566
+ return result unless @more
567
+ end
419
568
  end
420
569
 
570
+ # Runs statements; returns the last one's result.
571
+ def execute(sql) = query(sql)
572
+
573
+ # +text+ as a string literal.
574
+ def quote(text) = "'#{text.gsub(/[\0\n\r\\'"\x1a]/, ESCAPES)}'"
575
+
576
+ # +texts+ as a list of string literals, for IN (...).
577
+ def quote_all(texts) = texts.map { |text| quote(text) }.join(", ")
578
+
579
+ ESCAPES = {"\0" => "\\0", "\n" => "\\n", "\r" => "\\r", "\\" => "\\\\", "'" => "\\'", "\"" => "\\\"", "\x1a" => "\\Z"}.freeze
580
+
421
581
  # Closes it without telling the server (no QUIT, which the server
422
582
  # could read after a benchmark's started, and count); true.
423
- def close = @client.discard!
583
+ def close
584
+ @socket.close
585
+ true
586
+ end
587
+
588
+ private
589
+
590
+ # The server's greeting (a handshake, protocol 10), and the client's
591
+ # reply: root, with no password (an empty response, whichever plugin
592
+ # the server authenticates with), to +database+. Fails unless it's
593
+ # let in.
594
+ def authenticate(database)
595
+ greeting = read
596
+ error!(greeting) if greeting.getbyte(0) == ERR
597
+ # (Its last string: the scramble before it has no NUL in it.)
598
+ plugin = greeting.split("\0").last #: String
599
+ # (23 bytes reserved, and the response's length, 0, before the
600
+ # database.)
601
+ # (The greeting's numbered 0, and the reply 1.)
602
+ write([CAPABILITIES, MAX_PACKET, UTF8MB4, USER, database, plugin].pack("VVCx23Z*xZ*Z*"), 1)
603
+ loop do
604
+ reply = read
605
+ case reply.getbyte(0)
606
+ when OK then return
607
+ when ERR then error!(reply)
608
+ # (Another plugin: an empty response again.)
609
+ when AUTH_SWITCH then write("", @sequence + 1)
610
+ # (caching_sha2_password's: the empty password matched.)
611
+ when AUTH_MORE then raise Error, "MySQL wants a password (root's has none)" unless reply.getbyte(1) == FAST_AUTH
612
+ else raise Error, "MySQL: an authentication reply the connection doesn't read: #{reply.getbyte(0)}"
613
+ end
614
+ end
615
+ end
616
+
617
+ # A statement's result (and in @more, whether more follow): an OK's
618
+ # (no rows), or a result set's (its columns, then its rows, each
619
+ # ended by an EOF). An error raises it.
620
+ def next_result
621
+ packet = read
622
+ case packet.getbyte(0)
623
+ when OK
624
+ # (Its status after its affected rows and insert id.)
625
+ @at = 1
626
+ lenenc(packet)
627
+ lenenc(packet)
628
+ @more = more?(packet, @at)
629
+ Result.new(NONE, NONE)
630
+ when ERR then error!(packet)
631
+ else
632
+ @at = 0
633
+ count = lenenc(packet)
634
+ names = [] #: Array[String]
635
+ types = [] #: Array[Integer]
636
+ count.times { column(read, names, types) }
637
+ read # (the columns' EOF)
638
+ rows = [] #: Array[Array[untyped]]
639
+ rows << row(packet, types) until eof?(packet = read)
640
+ # (Its status after its warnings.)
641
+ @more = more?(packet, 3)
642
+ Result.new(names, rows)
643
+ end
644
+ end
645
+
646
+ # An OK's result: no fields, and no rows.
647
+ NONE = Array.new(0).freeze
648
+
649
+ # Whether an OK's or an EOF's status, at +at+, says more results
650
+ # follow. (Its first byte, of two, little-endian, has the flag.)
651
+ def more?(packet, at)
652
+ status = packet.getbyte(at) #: Integer
653
+ (status & MORE_RESULTS).positive?
654
+ end
655
+
656
+ # An EOF: one of fewer than 9 bytes that starts with EOF (a row can
657
+ # too, with a value's 8-byte length).
658
+ def eof?(packet) = packet.getbyte(0) == EOF && packet.bytesize < 9
659
+
660
+ # A column's name and its type, added to +names+ and +types+: after
661
+ # four lenenc strings (its catalog, schema, table, original table),
662
+ # its name, its original name, and its fixed fields' length, its
663
+ # character set (2 bytes) and length (4), then its type.
664
+ def column(packet, names, types)
665
+ @at = 0
666
+ 4.times { skip(packet) }
667
+ names << string(packet, lenenc(packet)).force_encoding(Encoding::UTF_8)
668
+ skip(packet)
669
+ lenenc(packet)
670
+ type = packet.getbyte(@at + 6) #: Integer
671
+ types << type
672
+ end
673
+
674
+ # A row's values, by its columns' +types+.
675
+ def row(packet, types)
676
+ @at = 0
677
+ types.map do |type|
678
+ if packet.getbyte(@at) == NULL
679
+ @at += 1
680
+ next
681
+ end
682
+
683
+ length = lenenc(packet)
684
+ if INTEGERS.include?(type)
685
+ integer(packet, length)
686
+ elsif FLOATS.include?(type)
687
+ Float(string(packet, length))
688
+ else
689
+ string(packet, length).force_encoding(Encoding::UTF_8)
690
+ end
691
+ end
692
+ end
693
+
694
+ # The integer written (in decimal, with a sign if it's negative) in
695
+ # the +length+ bytes at the packet's @at, read on past them. (Read
696
+ # byte by byte, so that no String is made of them.)
697
+ def integer(packet, length)
698
+ stop = @at + length
699
+ negative = packet.getbyte(@at) == MINUS
700
+ @at += 1 if negative
701
+ value = 0
702
+ while @at < stop
703
+ digit = packet.getbyte(@at) #: Integer
704
+ value = (value * 10) + digit - ZERO
705
+ @at += 1
706
+ end
707
+ negative ? -value : value
708
+ end
709
+
710
+ # A minus sign's byte, and a 0's.
711
+ MINUS = 0x2D
712
+ ZERO = 0x30
713
+
714
+ # The +length+ bytes at the packet's @at, as a String of their own,
715
+ # read on past them.
716
+ def string(packet, length)
717
+ string = packet.byteslice(@at, length) #: String
718
+ @at += length
719
+ string
720
+ end
721
+
722
+ # A length-encoded string at the packet's @at, skipped. (Its length
723
+ # read first: it moves @at past itself.)
724
+ def skip(packet)
725
+ length = lenenc(packet)
726
+ @at += length
727
+ end
728
+
729
+ # The length-encoded integer at the packet's @at, read on past it.
730
+ def lenenc(packet)
731
+ at = @at
732
+ first = packet.getbyte(at) #: Integer
733
+ # (Looked up, not fetched with a block, which would make a Proc
734
+ # for each one read.)
735
+ size = LENGTHS[first]
736
+ unless size
737
+ @at = at + 1
738
+ return first
739
+ end
740
+ # (Its bytes, little-endian.)
741
+ value = 0
742
+ size.times do |i|
743
+ byte = packet.getbyte(at + 1 + i) #: Integer
744
+ value += byte << (8 * i)
745
+ end
746
+ @at = at + 1 + size
747
+ value
748
+ end
749
+
750
+ # A length-encoded integer's prefixes, and its bytes after them (one
751
+ # under 0xFB is the integer itself).
752
+ LENGTHS = {0xFC => 2, 0xFD => 3, 0xFE => 8}.freeze
753
+
754
+ # The next packet's payload (in a buffer that the one after it is
755
+ # read into), its sequence number in @sequence.
756
+ def read
757
+ # (Its length in 3 bytes, then its sequence number.)
758
+ header = read_exactly(4, @header).unpack1("V") #: Integer
759
+ @sequence = header >> 24
760
+ read_exactly(header & MAX_PACKET, @packet)
761
+ end
762
+
763
+ def read_exactly(size, buffer)
764
+ raise IOError, "MySQL didn't answer in #{@timeout} seconds" unless @socket.wait_readable(@timeout)
765
+
766
+ data = @socket.read(size, buffer)
767
+ raise IOError, CLOSED unless data && data.bytesize == size
768
+
769
+ data
770
+ end
771
+
772
+ # Writes a packet: +payload+, after +command+'s byte if there's one,
773
+ # numbered +sequence+. (Its header: its length in 3 bytes, then its
774
+ # sequence number.)
775
+ def write(payload, sequence, command = nil)
776
+ size = payload.bytesize + (command ? 1 : 0)
777
+ out = @out.clear
778
+ out << (size & 0xFF) << ((size >> 8) & 0xFF) << (size >> 16) << sequence
779
+ out << command if command
780
+ @socket.write(out, payload)
781
+ end
782
+
783
+ # Raises an error packet's error: its code (2 bytes), its SQL state
784
+ # (# and 5 characters), and its message.
785
+ def error!(packet)
786
+ code = packet.unpack1("v", offset: 1) #: Integer
787
+ start = (packet.getbyte(3) == 35) ? 9 : 3 # (after the SQL state, where there's one)
788
+ message = packet.byteslice(start..) #: String
789
+ message.force_encoding(Encoding::UTF_8)
790
+ raise Error.new("MySQL: #{message} (#{code})", code: code)
791
+ end
424
792
  end
425
793
 
426
794
  # A worker's data directory: a copy of one initialized once, with the
427
795
  # database created in it.
428
796
  class DataDir
429
- # The server program: +server+ if it's a path, or the one by that
430
- # name on +path+; nil if there's none.
431
- def self.find_server(server, path = ENV.fetch("PATH", ""))
432
- files = server.include?(File::SEPARATOR) ? [server] : path.split(File::PATH_SEPARATOR).map { |dir| File.join(dir, server) }
433
- files.find { |file| File.executable?(file) }
434
- end
435
-
436
797
  # The options the server's run with, initializing +data+ or not.
437
798
  # (They come first: mysqld takes --no-defaults only there.)
438
799
  def self.options(data) = ["--no-defaults", "--datadir=#{data}", *OPTIONS]
data/sig/duel/mysql.rbs CHANGED
@@ -7,6 +7,10 @@ module Duel
7
7
 
8
8
  URL: String
9
9
  SOCKET: String
10
+
11
+ # Where libmysqlclient connects when it's given no socket or port.
12
+ UNIX_PORT: String
13
+ TCP_PORT: String
10
14
  DATABASE: String
11
15
  USER: String
12
16
 
@@ -38,26 +42,27 @@ module Duel
38
42
  # rows examined, time (ps).
39
43
  type numbers = Array[Integer]
40
44
 
41
- # A statement's row, as MySQL.digests reads it: [digest, *numbers].
45
+ # A statement's row, as MySQL.digests reads it: [*numbers, digest].
42
46
  type row = Array[untyped]
43
47
 
44
48
  # Configures Duel for the plugin (c.use :mysql): the server (+server+
45
- # the program, +args+ more of its options, +setup+ SQL each worker's
46
- # database runs when it's started, +env+ the variables besides
47
- # DUEL_MYSQL_URL given its URL, +scheme+ the URL's) and its metrics.
48
- # Returns the plugin's own connection to it, a worker's.
49
- def self.configure: (Config config, ?server: String, ?args: Array[String], ?setup: String?, ?env: Array[String],
50
- ?scheme: String) -> InWorker[Connection]
51
-
52
- # Runs SQL on a connection of its own to the server on +socket+;
53
- # returns the last statement's result.
54
- # Loads Trilogy, as the first connection is made.
55
- def self.require_trilogy: () -> bool
49
+ # the program, +settings+ more of its system variables, +sql+ what each
50
+ # worker's database runs when it's started, +env+ the variables besides
51
+ # DUEL_MYSQL_URL given its URL, +scheme+ the URL's, +tcp+ on a port
52
+ # of 127.0.0.1's: by default on Windows) and its metrics. Returns the
53
+ # plugin's own connection to it, a worker's.
54
+ def self.configure: (Config config, ?server: String, ?settings: Hash[String | Symbol, String | Integer], ?sql: String?, ?env: Array[String],
55
+ ?scheme: String, ?tcp: bool) -> InWorker[Connection]
56
56
 
57
57
  # The most memory a worker's server takes.
58
58
  def self.worker_memory: () -> Integer
59
59
 
60
- def self.run: (String socket, String sql) -> ::Trilogy::Result
60
+ # Runs SQL on a connection of its own to the server at +address+;
61
+ # returns the last statement's result.
62
+ def self.run: (String address, String sql) -> Result
63
+
64
+ # Asks the server at +address+ to shut down, if it's there to ask.
65
+ def self.shutdown: (String address) -> void
61
66
 
62
67
  # What the worker's server has done so far, but for the plugin's own
63
68
  # connection, which reads it.
@@ -67,12 +72,9 @@ module Duel
67
72
  # running a statement.
68
73
  def self.settle: (Connection connection) -> void
69
74
 
70
- # The same by measure.
71
- def self.stats: (Connection connection) -> Snapshot::totals[Metric::value]
72
-
73
75
  # What the server has done so far, by statement, read over
74
76
  # +connection+: those run since +since+, but the +own+ digests, each
75
- # one's [digest, *numbers].
77
+ # one's [*numbers, digest].
76
78
  def self.digests: (Connection connection, Integer since, Array[String] own) -> Array[row]
77
79
 
78
80
  # The digest of each of +statements+, read over +connection+.
@@ -88,13 +90,23 @@ module Duel
88
90
  # Its socket's path.
89
91
  def self.socket: () -> String
90
92
 
93
+ # The environment (ENV, or a Hash in its place).
94
+ interface _Env
95
+ def fetch: (String name) -> String
96
+ | (String name) { (String name) -> String } -> String
97
+ end
98
+
99
+ # Where it is, for the plugin's own connections: its socket's path, or
100
+ # its URL's HOST:PORT.
101
+ def self.address: (?_Env env) -> String
102
+
91
103
  # Runs SQL on it, over a connection of its own; returns the last
92
104
  # statement's result. For setting up: it fails while a benchmark is
93
105
  # measured.
94
- def self.execute: (String sql) -> ::Trilogy::Result
106
+ def self.execute: (String sql) -> Result
95
107
 
96
108
  # Empties every table in its database. For setting up, as execute is.
97
- def self.reset: () -> ::Trilogy::Result
109
+ def self.reset: () -> Result
98
110
 
99
111
  # Running totals of what the worker's server did, all told, and by
100
112
  # statement, to explain a regression.
@@ -107,9 +119,6 @@ module Duel
107
119
  SHOWN: Integer
108
120
  TEXT: Integer
109
121
 
110
- # A statement not read yet, as a row of MySQL.digests.
111
- UNREAD: row
112
-
113
122
  # A statement that did nothing yet.
114
123
  NOTHING: numbers
115
124
 
@@ -169,25 +178,127 @@ module Duel
169
178
  def shorten: (String text) -> String
170
179
  end
171
180
 
172
- # A connection to a worker's server, as root, to its database.
173
- class Connection
174
- @client: ::Trilogy
181
+ # What a connection to a worker's server raises where it fails.
182
+ class Error < Duel::Error
183
+ @code: Integer?
175
184
 
176
- # Whether a server answers on +socket+.
177
- def self.ping: (String socket) -> bool
185
+ # The server's error code, or nil.
186
+ attr_reader code: Integer?
178
187
 
179
- def initialize: (String socket) -> void
188
+ def initialize: (String message, ?code: Integer?) -> void
189
+ end
190
+
191
+ # A query's result: its columns' names, and its rows.
192
+ class Result
193
+ include Enumerable[Array[untyped]]
194
+
195
+ attr_reader fields: Array[String]
196
+ attr_reader rows: Array[Array[untyped]]
180
197
 
181
- def query: (String sql) -> ::Trilogy::Result
198
+ def initialize: (Array[String] fields, Array[Array[untyped]] rows) -> void
199
+
200
+ def each: () { (Array[untyped] row) -> void } -> Array[Array[untyped]]
201
+ end
202
+
203
+ # The least a connection needs to read a server's statistics, in
204
+ # MySQL's text protocol: as root, to its database.
205
+ class Connection
206
+ LONG_PASSWORD: Integer
207
+ CONNECT_WITH_DB: Integer
208
+ PROTOCOL_41: Integer
209
+ TRANSACTIONS: Integer
210
+ SECURE_CONNECTION: Integer
211
+ MULTI_STATEMENTS: Integer
212
+ MULTI_RESULTS: Integer
213
+ PLUGIN_AUTH: Integer
214
+ CAPABILITIES: Integer
215
+ MAX_PACKET: Integer
216
+ UTF8MB4: Integer
217
+ OK: Integer
218
+ ERR: Integer
219
+ EOF: Integer
220
+ AUTH_SWITCH: Integer
221
+ AUTH_MORE: Integer
222
+ FAST_AUTH: Integer
223
+ COM_QUERY: Integer
224
+ NULL: Integer
225
+ MORE_RESULTS: Integer
226
+ INTEGERS: Array[Integer]
227
+ FLOATS: Array[Integer]
228
+ CLOSED: String
229
+ TIMEOUT: Integer
230
+ ESCAPES: Hash[String, String]
231
+ LENGTHS: Hash[Integer, Integer]
232
+ NONE: Array[untyped]
233
+ MINUS: Integer
234
+ ZERO: Integer
235
+
236
+ @socket: UNIXSocket | TCPSocket
237
+ @timeout: Integer | Float
238
+ @header: String
239
+ @packet: String
240
+ @out: String
241
+ @at: Integer
242
+ @sequence: Integer
243
+ @more: bool
244
+
245
+ # Whether a server answers at +address+.
246
+ def self.ping: (String address) -> bool
247
+
248
+ # The host and port of a HOST:PORT address, or nil.
249
+ def self.address: (String address) -> [String, Integer]?
250
+
251
+ # A socket to +address+.
252
+ def self.socket: (String address) -> (UNIXSocket | TCPSocket)
253
+
254
+ def initialize: (String address, ?database: String, ?timeout: Integer | Float) -> void
255
+
256
+ def query: (String sql) -> Result
257
+
258
+ # Runs statements; returns the last one's result.
259
+ def execute: (String sql) -> Result
182
260
 
183
261
  # +text+ as a string literal.
184
262
  def quote: (String text) -> String
185
263
 
186
- # Runs statements; returns the last one's result.
187
- def execute: (String sql) -> ::Trilogy::Result
264
+ # +texts+ as a list of string literals, for IN (...).
265
+ def quote_all: (Array[String] texts) -> String
188
266
 
189
267
  # Closes it without telling the server (no QUIT); true.
190
268
  def close: () -> bool
269
+
270
+ private
271
+
272
+ def authenticate: (String database) -> void
273
+
274
+ # A statement's result (and in @more, whether more follow).
275
+ def next_result: () -> Result
276
+
277
+ def more?: (String packet, Integer at) -> bool
278
+
279
+ def eof?: (String packet) -> bool
280
+
281
+ # A column's name and its type, the +i+th of +names+ and +types+.
282
+ def column: (String packet, Array[String] names, Array[Integer] types) -> Array[Integer]
283
+
284
+ def row: (String packet, Array[Integer] types) -> Array[untyped]
285
+
286
+ def integer: (String packet, Integer length) -> Integer
287
+
288
+ def string: (String packet, Integer length) -> String
289
+
290
+ def skip: (String packet) -> Integer
291
+
292
+ def lenenc: (String packet) -> Integer
293
+
294
+ # The next packet's payload.
295
+ def read: () -> String
296
+
297
+ def read_exactly: (Integer size, String buffer) -> String
298
+
299
+ def write: (String payload, Integer sequence, ?Integer? command) -> Integer
300
+
301
+ def error!: (String packet) -> bot
191
302
  end
192
303
 
193
304
  # A worker's data directory: a copy of one initialized once.
@@ -195,10 +306,6 @@ module Duel
195
306
  @resource: Resource
196
307
  @server: String
197
308
 
198
- # The server program: +server+ if it's a path, or the one by that
199
- # name on +path+; nil if there's none.
200
- def self.find_server: (String server, ?String path) -> String?
201
-
202
309
  # The options the server's run with, initializing a data directory
203
310
  # or not.
204
311
  def self.options: (String data) -> Array[String]
metadata CHANGED
@@ -1,80 +1,68 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duel-mysql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
- - Erik Berlin
7
+ - Erik Berlin
8
8
  bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
- - !ruby/object:Gem::Dependency
13
- name: duel
14
- requirement: !ruby/object:Gem::Requirement
15
- requirements:
16
- - - "~>"
17
- - !ruby/object:Gem::Version
18
- version: '0.1'
19
- type: :runtime
20
- prerelease: false
21
- version_requirements: !ruby/object:Gem::Requirement
22
- requirements:
23
- - - "~>"
24
- - !ruby/object:Gem::Version
25
- version: '0.1'
26
- - !ruby/object:Gem::Dependency
27
- name: trilogy
28
- requirement: !ruby/object:Gem::Requirement
29
- requirements:
30
- - - "~>"
31
- - !ruby/object:Gem::Version
32
- version: '2.9'
33
- type: :runtime
34
- prerelease: false
35
- version_requirements: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - "~>"
38
- - !ruby/object:Gem::Version
39
- version: '2.9'
40
- description: 'A Duel plugin: each worker gets a MySQL server of its own, and every
41
- benchmark is judged on the statements it sent, the rows they read and their time
42
- too.'
12
+ - !ruby/object:Gem::Dependency
13
+ name: duel
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ -
17
+ - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ -
25
+ - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: 0.3.0
28
+ description: "A Duel plugin: each worker gets a MySQL server of its own, and every benchmark is judged on the statements it sent, the rows they read and their time too."
43
29
  email:
44
- - sferik@gmail.com
30
+ - sferik@gmail.com
45
31
  executables: []
46
32
  extensions: []
47
33
  extra_rdoc_files: []
48
34
  files:
49
- - CHANGELOG.md
50
- - LICENSE
51
- - README.md
52
- - lib/duel/mysql.rb
53
- - lib/duel/mysql/version.rb
54
- - sig/duel/mysql.rbs
55
- homepage: https://github.com/sferik/duel-ruby/tree/main/plugins/mysql
35
+ - CHANGELOG.md
36
+ - LICENSE
37
+ - README.md
38
+ - lib/duel/mysql.rb
39
+ - lib/duel/mysql/version.rb
40
+ - sig/duel/mysql.rbs
41
+ homepage: "https://github.com/sferik/duel-ruby/tree/main/plugins/mysql"
56
42
  licenses:
57
- - MIT
43
+ - MIT
58
44
  metadata:
59
- source_code_uri: https://github.com/sferik/duel-ruby/tree/main/plugins/mysql
60
- changelog_uri: https://github.com/sferik/duel-ruby/blob/main/plugins/mysql/CHANGELOG.md
61
- bug_tracker_uri: https://github.com/sferik/duel-ruby/issues
62
- rubygems_mfa_required: 'true'
45
+ source_code_uri: "https://github.com/sferik/duel-ruby/tree/main/plugins/mysql"
46
+ changelog_uri: "https://github.com/sferik/duel-ruby/blob/main/plugins/mysql/CHANGELOG.md"
47
+ bug_tracker_uri: "https://github.com/sferik/duel-ruby/issues"
48
+ rubygems_mfa_required: "true"
63
49
  rdoc_options: []
64
50
  require_paths:
65
- - lib
51
+ - lib
66
52
  required_ruby_version: !ruby/object:Gem::Requirement
67
53
  requirements:
68
- - - ">="
69
- - !ruby/object:Gem::Version
70
- version: '3.4'
54
+ -
55
+ - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "3.4"
71
58
  required_rubygems_version: !ruby/object:Gem::Requirement
72
59
  requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
60
+ -
61
+ - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
76
64
  requirements: []
77
- rubygems_version: 4.0.24
65
+ rubygems_version: 4.1.0
78
66
  specification_version: 4
79
- summary: A MySQL server for each Duel worker, and what it did for each benchmark
67
+ summary: "A MySQL server for each Duel worker, and what it did for each benchmark"
80
68
  test_files: []