<commit_before>;;; Copyright (c) 2011, <NAME>. All rights reserved.
;;; 
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;; 
;;;     * Redistributions of source code must retain the above copyright
;;;       notice, this list of conditions and the following disclaimer.
;;; 
;;;     * Redistributions in binary form must reproduce the above
;;;       copyright notice, this list of conditions and the following
;;;       disclaimer in the documentation and/or other materials provided
;;;       with the distribution.
;;; 
;;;     * Neither the name of the project nor the names of its
;;;       contributors may be used to endorse or promote products derived
;;;       from this software without specific prior written permission.
;;; 
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
;;; HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
;;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
;;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
;;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
;;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
;;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

(in-package #:lparallel-bench)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; helpers
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defmacro collecting1 (&body body)
  (with-gensyms (result value)
    `(let1 ,result nil
       (flet ((collect (,value) (push ,value ,result)))
         ,@body)
       (nreverse ,result))))

(defun compose (&rest fns)
  (flet ((compose2 (f g) (lambda (x) (funcall g (funcall f x)))))
    (destructuring-bind (right &rest lefts) (reverse fns)
      (let1 left (reduce #'compose2 lefts)
        (lambda (&rest args) (funcall left (apply right args)))))))

(defun groups-of (n list)
  (loop
     :for pos :on list :by (curry 'nthcdr n)
     :collect (subseq pos 0 n)))

(defun identity-args (&rest args)
  args)

(defun zip (&rest args)
  (apply #'mapcar 'identity-args args))

(defun riffle (groups deck)
  (apply #'zip (groups-of (/ (length deck) groups) deck)))

(defun flatten (list)
  (mapcan (lambda (x) (if (consp x) (flatten x) (list x))) list))

(defun wall-time (fn args)
  (let1 start (get-internal-real-time)
    (apply fn args)
    (- (get-internal-real-time) start)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; bench
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defslots bench-spec ()
  ((args-fn :reader args-fn)
   (exec-fn :reader exec-fn)
   (desc-fn :reader desc-fn))
  (:documentation
   "A benchmark specification.

`args-fn' creates the arguments to be passed to `exec-fn'. The
execution time of `exec-fn' is passed to `desc-fn', which returns a
descriptive string."))

(setf (symbol-function 'make-bench-spec) #'make-bench-spec-instance)

(defun print-chunk (chunk)
  (format t "~&")
  (mapcar 'princ chunk)
  (format t "~%"))

(defun ping (x)
  (format t ".")
  x)

(defun bench (num-fns num-trials num-rehearsals specs)
  "Run bench specs. 

To minimize GC interactions, all arguments are generated at the outset
and each benchmarked function is held constant while the generated
argument lists are applied successively.

When benchmarks are complete, the rehearsals are discarded and the
results are riffled for comparison."
  (mapcar 'print-chunk
   (mapcar 'flatten
    (riffle num-fns
     (mapcar (curry 'nthcdr num-rehearsals)
      (groups-of num-trials
       (mapcar 'funcall
               (mapcar 'desc-fn specs)
               (mapcar (compose 'ping 'wall-time)
                       (mapcar 'exec-fn specs)
                       (mapcar (compose 'funcall 'args-fn) specs)))))))))

(defun call-with-temp-kernel (worker-count fn)
  (let1 *kernel* (make-kernel worker-count)
    (unwind-protect (funcall fn)
      (end-kernel))))

(defun run-suite (worker-count reset-fn &rest fns)<commit_msg>benchmarks: wait for temp kernel to finish<commit_after>;;; Copyright (c) 2011, <NAME>. All rights reserved.
;;; 
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;; 
;;;     * Redistributions of source code must retain the above copyright
;;;       notice, this list of conditions and the following disclaimer.
;;; 
;;;     * Redistributions in binary form must reproduce the above
;;;       copyright notice, this list of conditions and the following
;;;       disclaimer in the documentation and/or other materials provided
;;;       with the distribution.
;;; 
;;;     * Neither the name of the project nor the names of its
;;;       contributors may be used to endorse or promote products derived
;;;       from this software without specific prior written permission.
;;; 
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
;;; HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
;;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
;;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
;;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
;;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
;;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

(in-package #:lparallel-bench)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; helpers
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defmacro collecting1 (&body body)
  (with-gensyms (result value)
    `(let1 ,result nil
       (flet ((collect (,value) (push ,value ,result)))
         ,@body)
       (nreverse ,result))))

(defun compose (&rest fns)
  (flet ((compose2 (f g) (lambda (x) (funcall g (funcall f x)))))
    (destructuring-bind (right &rest lefts) (reverse fns)
      (let1 left (reduce #'compose2 lefts)
        (lambda (&rest args) (funcall left (apply right args)))))))

(defun groups-of (n list)
  (loop
     :for pos :on list :by (curry 'nthcdr n)
     :collect (subseq pos 0 n)))

(defun identity-args (&rest args)
  args)

(defun zip (&rest args)
  (apply #'mapcar 'identity-args args))

(defun riffle (groups deck)
  (apply #'zip (groups-of (/ (length deck) groups) deck)))

(defun flatten (list)
  (mapcan (lambda (x) (if (consp x) (flatten x) (list x))) list))

(defun wall-time (fn args)
  (let1 start (get-internal-real-time)
    (apply fn args)
    (- (get-internal-real-time) start)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; bench
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defslots bench-spec ()
  ((args-fn :reader args-fn)
   (exec-fn :reader exec-fn)
   (desc-fn :reader desc-fn))
  (:documentation
   "A benchmark specification.

`args-fn' creates the arguments to be passed to `exec-fn'. The
execution time of `exec-fn' is passed to `desc-fn', which returns a
descriptive string."))

(setf (symbol-function 'make-bench-spec) #'make-bench-spec-instance)

(defun print-chunk (chunk)
  (format t "~&")
  (mapcar 'princ chunk)
  (format t "~%"))

(defun ping (x)
  (format t ".")
  x)

(defun bench (num-fns num-trials num-rehearsals specs)
  "Run bench specs. 

To minimize GC interactions, all arguments are generated at the outset
and each benchmarked function is held constant while the generated
argument lists are applied successively.

When benchmarks are complete, the rehearsals are discarded and the
results are riffled for comparison."
  (mapcar 'print-chunk
   (mapcar 'flatten
    (riffle num-fns
     (mapcar (curry 'nthcdr num-rehearsals)
      (groups-of num-trials
       (mapcar 'funcall
               (mapcar 'desc-fn specs)
               (mapcar (compose 'ping 'wall-time)
                       (mapcar 'exec-fn specs)
                       (mapcar (compose 'funcall 'args-fn) specs)))))))))

(defun call-with-temp-kernel (worker-count fn)
  (let1 *kernel* (make-kernel worker-count)
    (unwind-protect (funcall fn)
      (end-kernel :wait t))))

(defun run-suite (worker-count reset-fn &rest fns)
{'fraction_non_alphanumeric': 0.17502524402558062, 'fraction_numerical': 0.0026926960619320095, 'mean_word_length': 3.857765667574932, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4691243', 'n_tokens_mistral': 2964, 'n_tokens_neox': 2666, 'n_words': 1007}