all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob c0f4e806b20af867e106fcb8b5a7795b780d7bf2 16930 bytes (raw)
name: test/manual/perf/perf.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
 
;; -*- lexical-binding:t -*-

;; Copyright (C) 2015-2024 Free Software Foundation, Inc.

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Code:

(require 'cl-lib)
(require 'subr-x)
(require 'seq)
(require 'hi-lock)

\f
;; +===================================================================================+
;; | Framework
;; +===================================================================================+

(defmacro perf-define-constant-test (name &optional doc &rest body)
  (declare (indent 1) (debug (symbol &optional string &rest form)))
  `(progn
     (put ',name 'perf-constant-test t)
     (defun ,name nil ,doc ,@body)))

(defmacro perf-define-variable-test (name args &optional doc &rest body)
  (declare (indent 2) (debug defun))
  (unless (and (consp args)
               (= (length args) 1))
    (error "Function %s should accept exactly one argument." name))
  `(progn
     (put ',name 'perf-variable-test t)
     (defun ,name ,args ,doc ,@body)))

(defmacro perf-define-test-suite (name &rest tests)
  (declare (indent 1))
  `(put ',name 'perf-test-suite
        ,(cons 'list tests)))

(defun perf-constant-test-p (test)
  (get test 'perf-constant-test))

(defun perf-variable-test-p (test)
  (get test 'perf-variable-test))

(defun perf-test-suite-p (suite)
  (not (null (perf-test-suite-elements suite))))

(defun perf-test-suite-elements (suite)
  (get suite 'perf-test-suite))

(defun perf-expand-suites (test-and-suites)
  (apply #' append (mapcar (lambda (elt)
                             (if (perf-test-suite-p elt)
                                 (perf-test-suite-elements elt)
                               (list elt)))
                           test-and-suites)))
(defun perf-test-p (symbol)
  (or (perf-variable-test-p symbol)
      (perf-constant-test-p symbol)))

(defun perf-all-tests ()
  (let (result)
    (mapatoms (lambda (symbol)
                (when (and (fboundp symbol)
                           (perf-test-p symbol))
                  (push symbol result))))
    (sort result #'string-lessp)))

(defvar perf-default-test-argument 4096)

(defun perf-run-1 (&optional k n &rest tests)
  "Run TESTS K times using N as argument for non-constant ones.

Return test-total elapsed time."
  (random "")
  (when (and n (not (numberp n)))
    (push k tests)
    (push n tests)
    (setq n nil k nil))
  (when (and k (not (numberp k)))
    (push k tests)
    (setq k nil))
  (let* ((k (or k 1))
         (n (or n perf-default-test-argument))
         (tests (perf-expand-suites (or tests
                                        (perf-all-tests))))
         (variable-tests (seq-filter #'perf-variable-test-p tests))
         (constant-tests (seq-filter #'perf-constant-test-p tests))
         (max-test-string-width (perf-max-symbol-length tests)))
    (unless (seq-every-p #'perf-test-p tests)
      (error "Some of these are not tests: %s" tests))
    (cl-labels ((format-result (result)
                  (cond
                   ((numberp result) (format "%.2f" result))
                   ((stringp result) result)
                   ((null result) "N/A")))
                (format-test (fn)
                  (concat (symbol-name fn)
                          (make-string
                           (+ (- max-test-string-width
                                 (length (symbol-name fn)))
                              1)
                           ?\s)))
                (format-summary (results _total)
                  (let ((min (apply #'min results))
                        (max (apply #'max results))
                        (avg (/ (apply #'+ results) (float (length results)))))
                    (format "n=%d min=%.2f avg=%.2f max=%.2f" (length results) min avg max)))
                (run-test (fn)
                  (let ((total 0) results)
                    (dotimes (_ (max 0 k))
                      (garbage-collect)
                      (princ (concat " " (format-test fn)))
                      (let ((result  (condition-case-unless-debug err
                                         (cond
                                          ((perf-variable-test-p fn)
                                           (random "") (car (funcall fn n)))
                                          ((perf-constant-test-p fn)
                                           (random "") (car (funcall fn)))
                                          (t "skip"))
                                       (error (error-message-string err)))))
                        (when (numberp result)
                          (cl-incf total result)
                          (push result results))
                        (princ (format-result result))
                        (terpri)))
                    (when (> (length results) 1)
                      (princ (concat "#" (format-test fn)
                                     (format-summary results total)))
                      (terpri)))))
      (when variable-tests
        (terpri)
        (dolist (fn variable-tests)
          (run-test fn)
          (terpri)))
      (when constant-tests
        (dolist (fn constant-tests)
          (run-test fn)
          (terpri))))))

(defun perf-run (&optional k n &rest tests)
  (interactive
   (let* ((n (if current-prefix-arg
                 (prefix-numeric-value current-prefix-arg)
               perf-default-test-argument))
          (tests (mapcar #'intern
                         (completing-read-multiple
                          (format "Run tests (n=%d): " n)
                          (perf-all-tests) nil t nil 'perf-test-history))))
     (cons 1 (cons n tests))))
  (with-current-buffer (get-buffer-create "*perf-results*")
    (let ((inhibit-read-only t)
          (standard-output (current-buffer)))
      (erase-buffer)
      (apply #'perf-run-1 k n tests)
      (display-buffer (current-buffer)))))


(defun perf-batch-parse-command-line (args)
  (let ((k 1)
        (n perf-default-test-argument)
        tests)
    (while args
      (cond ((string-match-p "\\`-[cn]\\'" (car args))
             (unless (and (cdr args)
                          (string-match-p "\\`[0-9]+\\'" (cadr args)))
               (error "%s expects a natnum argument" (car args)))
             (if (equal (car args) "-c")
                 (setq k (string-to-number (cadr args)))
               (setq n (string-to-number (cadr args))))
             (setq args (cddr args)))
            (t (push (intern (pop args)) tests))))
    (list k n tests)))


(defun perf-run-batch ()
  "Runs tests from `command-line-args-left' and kill emacs."
  (let ((standard-output #'external-debugging-output))
    (condition-case err
        (cl-destructuring-bind (k n tests)
            (perf-batch-parse-command-line command-line-args-left)
          (apply #'perf-run-1 k n tests)
          (save-buffers-kill-emacs))
      (error
       (princ (error-message-string err))
       (save-buffers-kill-emacs)))))

(defconst perf-number-of-columns 70)

(defun perf-insert-lines (n)
  "Insert N lines into the current buffer."
  (dotimes (i n)
    (insert (make-string perf-number-of-columns
                         (if (= (% i 2) 0)
                             ?.
                           ?O))
            ?\n)))

(defun perf-random-string (n)
  "Create a string of N random characters."
  (cl-loop with v = (make-vector n 0)
           for i upfrom 0 below n
           ;; This generates printable ASCII characters.
           for c = (+ ?! (random (- ?~ ?!)))
           do (aset v i c)
           finally return (concat v)))

(defun perf-insert-random (n)
  "Insert N random characters into the current buffer."
  (insert (perf-random-string n)))

(defun perf-switch-to-buffer-scroll-random (n &optional buffer)
  (interactive)
  (set-window-buffer nil (or buffer (current-buffer)))
  (goto-char (point-min))
  (redisplay t)
  (dotimes (_ n)
    (goto-char (random (point-max)))
    (recenter)
    (redisplay t)))

(defun perf-insert-overlays (n &optional create-callback random-p)
  (if random-p
      (perf-insert-overlays-random n create-callback)
    (perf-insert-overlays-sequential n create-callback)))

(defun perf-insert-overlays-sequential (n &optional create-callback)
  "Insert an overlay every Nth line."
  (declare (indent 1))
  (let ((i 0)
        (create-callback (or create-callback #'ignore)))
    (save-excursion
      (goto-char (point-min))
      (while (not (eobp))
        (when (= 0 (% i n))
          (let ((ov (make-overlay (point-at-bol) (point-at-eol))))
            (funcall create-callback ov)
            (overlay-put ov 'priority (random (buffer-size)))))
        (cl-incf i)
        (forward-line)))))

(defun perf-insert-overlays-random (n &optional create-callback)
  "Insert an overlay every Nth line."
  (declare (indent 1))
  (let ((create-callback (or create-callback #'ignore)))
    (save-excursion
      (while (>= (cl-decf n) 0)
        (let* ((beg (1+ (random (point-max))))
               (ov (make-overlay
                    beg (+ beg (random perf-number-of-columns)))))
          (funcall create-callback ov)
          (overlay-put ov 'priority (random (buffer-size))))))))

(defun perf-insert-overlays-hierarchical (n &optional create-callback)
  (let ((create-callback (or create-callback #'ignore)))
    (save-excursion
      (goto-char (point-min))
      (let ((spacing (floor (/ (/ (count-lines (point-min) (point-max))
                                  (float 3))
                               n))))
        (when (< spacing 1)
          (error "Hierarchical overlay overflow !!"))
        (dotimes (i n)
          (funcall create-callback
                   (make-overlay (point)
                                 (save-excursion
                                   (goto-char (point-max))
                                   (forward-line (- (* spacing i)))
                                   (point))))

          (when (eobp)
            (error "End of buffer in hierarchical overlays"))
          (forward-line spacing))))))

(defun perf-overlay-ascii-chart (&optional buffer width)
  (interactive)
  (save-current-buffer
    (when buffer (set-buffer buffer))
    (unless width (setq width 100))
    (let* ((ovl (sort (overlays-in (point-min) (point-max))
                      (lambda (ov1 ov2)
                        (or (<= (overlay-start ov1)
                                (overlay-start ov2))
                            (and
                             (= (overlay-start ov1)
                                (overlay-start ov2))
                             (< (overlay-end ov1)
                                (overlay-end ov2)))))))
           (ov-width (apply #'max (mapcar (lambda (ov)
                                            (- (overlay-end ov)
                                               (overlay-start ov)))
                                          ovl)))
           (ov-min (apply #'min (mapcar #'overlay-start ovl)))
           (ov-max (apply #'max (mapcar #'overlay-end ovl)))
           (scale (/ (float width) (+ ov-min ov-width))))
      (with-current-buffer (get-buffer-create "*overlay-ascii-chart*")
        (let ((inhibit-read-only t))
          (erase-buffer)
          (buffer-disable-undo)
          (insert (format "%06d%s%06d\n" ov-min (make-string (- width 12) ?\s) ov-max))
          (dolist (ov ovl)
            (let ((length (round (* scale (- (overlay-end ov)
                                             (overlay-start ov))))))
              (insert (make-string (round (* scale (overlay-start ov))) ?\s))
              (cl-case length
                (0 (insert "O"))
                (1 (insert "|"))
                (t (insert (format "|%s|" (make-string (- length 2) ?-)))))
              (insert "\n")))
          (goto-char (point-min)))
        (read-only-mode 1)
        (pop-to-buffer (current-buffer))))))

(defconst perf-overlay-faces (mapcar #'intern (seq-take hi-lock-face-defaults 3)))

(defun perf-overlay-face-callback (ov)
  (overlay-put ov 'face (nth (random (length perf-overlay-faces))
                             perf-overlay-faces)))

(defun perf-overlay-invisible-callback (ov)
  (overlay-put ov 'invisble (= 1 (random 2))))

(defun perf-overlay-display-callback (ov)
  (overlay-put ov 'display (make-string perf-number-of-columns ?*)))

(defmacro perf-define-display-test (overlay-type property-type scroll-type)
  (let ((name (intern (format "perf-display-%s/%s/%s"
                              overlay-type property-type scroll-type)))
        (arg (make-symbol "n")))

    `(perf-define-variable-test ,name (,arg)
       (with-temp-buffer
         (perf-insert-lines ,arg)
         (overlay-recenter (point-max))
         ,@(perf-define-display-test-1 arg overlay-type property-type scroll-type)))))

(defun perf-define-display-test-1 (arg overlay-type property-type scroll-type)
  (list (append (cl-case overlay-type
                  (sequential
                   (list 'perf-insert-overlays-sequential 2))
                  (hierarchical
                   `(perf-insert-overlays-hierarchical (/ ,arg 10)))
                  (random
                   `(perf-insert-overlays-random (/ ,arg 2)))
                  (t (error "Invalid insert type: %s" overlay-type)))
                (list
                 (cl-case property-type
                   (display '#'perf-overlay-display-callback)
                   (face '#'perf-overlay-face-callback)
                   (invisible '#'perf-overlay-invisible-callback)
                   (t (error "Invalid overlay type: %s" overlay-type)))))
        (list 'benchmark-run 1
              (cl-case scroll-type
                (scroll '(perf-switch-to-buffer-scroll-up-and-down))
                (random `(perf-switch-to-buffer-scroll-random (/ ,arg 50)))
                (t (error "Invalid scroll type: %s" overlay-type))))))

(defun perf-max-symbol-length (symbols)
  "Return the longest symbol in SYMBOLS, or -1 if symbols is nil."
  (if (null symbols)
      -1
    (apply #'max (mapcar
                  (lambda (elt)
                    (length (symbol-name elt)))
                  symbols))))

(defun perf-insert-text (n)
  "Insert N character into the current buffer."
  (let ((ncols 68)
        (char ?.))
    (dotimes (_  (/ n ncols))
      (insert (make-string (1- ncols) char) ?\n))
    (when (> (% n ncols) 0)
      (insert (make-string (1- (% n ncols)) char) ?\n))))

(defconst perf-insert-overlays-default-length 24)

(defun perf-insert-overlays-scattered (n &optional length)
  "Insert N overlays of max length 24 randomly."
  (dotimes (_ n)
    (let ((begin (random (1+ (point-max)))))
      (make-overlay
       begin (+ begin (random (1+ (or length perf-insert-overlays-default-length 0))))))))

(defvar perf-marker-gc-protection nil)

(defun perf-insert-marker-scattered (n)
  "Insert N marker randomly."
  (setq perf-marker-gc-protection nil)
  (dotimes (_ n)
    (push (copy-marker (random (1+ (point-max))))
          perf-marker-gc-protection)))

(defun perf-switch-to-buffer-scroll-up-and-down (&optional buffer)
  (interactive)
  (set-window-buffer nil (or buffer (current-buffer)))
  (goto-char (point-min))
  (redisplay t)
  (while (condition-case nil
             (progn (scroll-up) t)
           (end-of-buffer nil))
    (redisplay t))
  (while (condition-case nil
             (progn (scroll-down) t)
           (beginning-of-buffer nil))
    (redisplay t)))

(defmacro perf-define-marker-test (type where)
  (let ((name (intern (format "perf-%s-%s-marker" type where))))
    `(perf-define-variable-test ,name (n)
       (with-temp-buffer
         (perf-insert-text n)
         (perf-insert-marker-scattered n)
         (goto-char ,(cl-case where
                       (after (list 'point-max))
                       (t (list 'point-min))))
         (benchmark-run 1
           (dotimes (_ (/ n 2))
             ,@(when (eq where 'scatter)
                 (list '(goto-char (max 1 (random (point-max))))))
             ,(cl-case type
                (insert (list 'insert ?X))
                (delete (list 'delete-char (if (eq where 'after) -1 1))))))))))

(defun perf-emacs-lisp-setup ()
  (add-to-list 'imenu-generic-expression
               '(nil "^\\s-*(perf-define\\(?:\\w\\|\\s_\\)*\\s-*\\(\\(?:\\w\\|\\s_\\)+\\)" 1)))

(add-hook 'emacs-lisp-mode 'perf-emacs-lisp-setup)

debug log:

solving c0f4e806b20 ...
found c0f4e806b20 in https://yhetil.org/emacs/1VzhyYQmzkY8ZudqyPNciqngpUIHE7bpzCHQJQJhmJUvyq0OSOCMOKiAOS3s7FVO67oKaW-67ZrLQhvT_4Pth1TlHl8Z9XfGEvV8WD16m0c=@pm.me/ ||
	https://yhetil.org/emacs/N0O-eiIpxKNtUY2ZEohzUF--05o8uBc2wHsLVw7KrVJbG_Lt6kzkiKaN_ZTRdPv8jIxc_yAj-2-DVrzFi6MyYHwrgxHl11h6MYvHeqz1p9E=@hypnicjerk.ai/ ||
	https://yhetil.org/emacs/obyPxjc1-gUH7aZNNaGIIzybT6m7sLarevWp3z6KXBhb4rCC9G_FzTOuPtDs2saC3vQOVoMNPO6Bn3UQFa5KvbgtbNUjefNA3ArTasTnDy4=@hypnicjerk.ai/ ||
	https://yhetil.org/emacs/2LOLmIp1X8w4CGbqq3qDrzmKVA0KzYNL1N9lBtWdB-MtEv9oCuYgJMYprG170wMPjYxeQImAmWOPatGTTl4KxZMlptNo9A9hnHt84vdN9EA=@hypnicjerk.ai/

applying [1/1] https://yhetil.org/emacs/1VzhyYQmzkY8ZudqyPNciqngpUIHE7bpzCHQJQJhmJUvyq0OSOCMOKiAOS3s7FVO67oKaW-67ZrLQhvT_4Pth1TlHl8Z9XfGEvV8WD16m0c=@pm.me/
diff --git a/test/manual/perf/perf.el b/test/manual/perf/perf.el
new file mode 100644
index 00000000000..c0f4e806b20

Checking patch test/manual/perf/perf.el...
Applied patch test/manual/perf/perf.el cleanly.

skipping https://yhetil.org/emacs/N0O-eiIpxKNtUY2ZEohzUF--05o8uBc2wHsLVw7KrVJbG_Lt6kzkiKaN_ZTRdPv8jIxc_yAj-2-DVrzFi6MyYHwrgxHl11h6MYvHeqz1p9E=@hypnicjerk.ai/ for c0f4e806b20
skipping https://yhetil.org/emacs/obyPxjc1-gUH7aZNNaGIIzybT6m7sLarevWp3z6KXBhb4rCC9G_FzTOuPtDs2saC3vQOVoMNPO6Bn3UQFa5KvbgtbNUjefNA3ArTasTnDy4=@hypnicjerk.ai/ for c0f4e806b20
skipping https://yhetil.org/emacs/2LOLmIp1X8w4CGbqq3qDrzmKVA0KzYNL1N9lBtWdB-MtEv9oCuYgJMYprG170wMPjYxeQImAmWOPatGTTl4KxZMlptNo9A9hnHt84vdN9EA=@hypnicjerk.ai/ for c0f4e806b20
index at:
100644 c0f4e806b20af867e106fcb8b5a7795b780d7bf2	test/manual/perf/perf.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.