Skip to content

Commit

Permalink
enforce benchmark names be valid identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
jgpc42 committed Jan 12, 2023
1 parent 9f14c71 commit ca59c72
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.4.2-SNAPSHOT (20XX-XX-XX)

* Upgrade JMH to version `1.35`
* Explicit support for JDK 15, 16, 17, and 18
* Explicit support for Clojure 1.11
* Improve error message when benchmark `:name` is invalid Java method identifier ([#10][issue10])

## 0.4.1 (2021-08-24)

* Upgrade JMH to version `1.32`
Expand Down Expand Up @@ -70,5 +77,6 @@



[issue1]: https://github.com/jgpc42/jmh-clojure/issues/1
[issue2]: https://github.com/jgpc42/jmh-clojure/issues/2
[issue1]: https://github.com/jgpc42/jmh-clojure/issues/1
[issue2]: https://github.com/jgpc42/jmh-clojure/issues/2
[issue10]: https://github.com/jgpc42/jmh-clojure/issues/10
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</dependency>
```

JDK versions 8 to 16 and Clojure versions 1.7 to 1.10 are currently [tested against][ci].
JDK versions 8 to 18 and Clojure versions 1.7 to 1.11 are currently [tested against][ci].
</details>

### What is it?
Expand Down Expand Up @@ -133,7 +133,7 @@ Or, `lein test-all` for all supported Clojure versions.

### License

Copyright © 2017-2022 Justin Conklin
Copyright © 2017-2023 Justin Conklin

Distributed under the Eclipse Public License, the same as Clojure.

Expand Down
15 changes: 14 additions & 1 deletion src/jmh/env.clj
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,19 @@
(concat (->> benchmarks (remove (set warmups)))
(->> warmups (map #(assoc % :warmup true))))))

(defn- java-identifier? [^String s]
(let [xs (-> s .codePoints .iterator iterator-seq)]
(and xs
(Character/isJavaIdentifierStart ^int (first xs))
(every? #(Character/isJavaIdentifierPart ^int %) (next xs)))))

(defn- munge-method-suffix [b]
(let [s (munge (name (:name b (:fn b))))]
(if (or (not (:name b)) (java-identifier? s))
s
(throw (ex-info (format "benchmark name %s, after munging, is invalid identifier" (:name b))
{:name (:name b), :munged s})))))

(defn finalize-benchmarks
"Return the final benchmarks for running."
[{resolver :jmh/resolver :as env}]
Expand All @@ -176,7 +189,7 @@

finalize
(fn [idx b]
(let [meth (format "_%03d_%s" idx (munge (name (:name b (:fn b)))))
(let [meth (format "_%03d_%s" idx (munge-method-suffix b))
merged (merge-options (:options b) opt-selectors opts)]
(assoc b :class (:jmh/benchmark-class env)
:method meth, :index idx, :options merged
Expand Down

0 comments on commit ca59c72

Please sign in to comment.