Skip to content

Commit f7af3af

Browse files
committed
[doc] Update examples
1 parent 85b3a05 commit f7af3af

File tree

9 files changed

+88
-118
lines changed

9 files changed

+88
-118
lines changed

README.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,33 @@ See [here][GitHub releases] for earlier releases.
3737
## Quick example
3838

3939
```clojure
40-
(require '[taoensso.tufte :as tufte :refer [defnp p profiled profile]])
40+
(require '[taoensso.tufte :as tufte)
4141

42-
;; Request to send `profile` stats to `println`:
43-
(tufte/add-basic-println-handler! {})
42+
;; Send `profile` signals to console
43+
(tufte/add-handler! :my-console-handler (tufte/handler:console))
4444

4545
;;; Define a couple dummy fns to simulate doing some expensive work
4646
(defn get-x [] (Thread/sleep 500) "x val")
4747
(defn get-y [] (Thread/sleep (rand-int 1000)) "y val")
4848

4949
;; Let's check how these fns perform:
50-
51-
(profile ; Profile any `p` forms called during body execution
50+
(tufte/profile ; Profile any `p` forms called during body execution
5251
{} ; Profiling options; we'll use the defaults for now
5352
(dotimes [_ 5]
54-
(p :get-x (get-x))
55-
(p :get-y (get-y))))
53+
(tufte/p :get-x (get-x))
54+
(tufte/p :get-y (get-y))))
5655

5756
;; The following will be printed to *out*:
57+
;; 2025-04-18T11:23:08.820786Z INFO MyHost readme-examples[15,1]
58+
;; <<< pstats <<<
59+
;; pId nCalls Min 50% ≤ 90% ≤ 95% ≤ 99% ≤ Max Mean MAD Clock Total
5860
;;
59-
;; pId nCalls Min 50% ≤ 90% ≤ 95% ≤ 99% ≤ Max Mean MAD Clock Total
60-
;; :get-x 5 501ms 503ms 505ms 505ms 505ms 505ms 503ms ±0% 2.52s 53%
61-
;; :get-y 5 78ms 396ms 815ms 815ms 815ms 815ms 452ms ±48% 2.25s 47%
61+
;; :get-y 5 238ms 501ms 981ms 981ms 981ms 981ms 618ms ±42% 3.09s 55%
62+
;; :get-x 5 501ms 502ms 505ms 505ms 505ms 505ms 503ms ±0% 2.51s 45%
6263
;;
63-
;; Accounted 4.78s 100%
64-
;; Clock 4.78s 100%
64+
;; Accounted 5.60s 100%
65+
;; Clock 5.60s 100%
66+
;; >>> pstats >>>
6567
```
6668

6769
## Documentation

examples/README.cljc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
(ns readme-examples
2+
"Basic examples that appear in the Truss README."
3+
(:require [taoensso.tufte :as tufte]))
4+
5+
(comment
6+
7+
;; Send `profile` signals to console
8+
(tufte/add-handler! :my-console-handler (tufte/handler:console))
9+
10+
;;; Define a couple dummy fns to simulate doing some expensive work
11+
(defn get-x [] (Thread/sleep 500) "x val")
12+
(defn get-y [] (Thread/sleep (rand-int 1000)) "y val")
13+
14+
;; Let's check how these fns perform:
15+
(tufte/profile ; Profile any `p` forms called during body execution
16+
{} ; Profiling options; we'll use the defaults for now
17+
(dotimes [_ 5]
18+
(tufte/p :get-x (get-x))
19+
(tufte/p :get-y (get-y))))
20+
21+
;; The following will be printed to *out*:
22+
;; 2025-04-18T11:23:08.820786Z INFO MyHost readme-examples[15,1]
23+
;; <<< pstats <<<
24+
;; pId nCalls Min 50% ≤ 90% ≤ 95% ≤ 99% ≤ Max Mean MAD Clock Total
25+
;;
26+
;; :get-y 5 238ms 501ms 981ms 981ms 981ms 981ms 618ms ±42% 3.09s 55%
27+
;; :get-x 5 501ms 502ms 505ms 505ms 505ms 505ms 503ms ±0% 2.51s 45%
28+
;;
29+
;; Accounted 5.60s 100%
30+
;; Clock 5.60s 100%
31+
;; >>> pstats >>>
32+
33+
)

examples/clj/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Tufte example project
22

3-
Try `lein run`, or `lein repl :headless`, etc.
3+
Try `lein run`, or `lein repl :headless`, etc.

examples/clj/project.clj

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
(defproject com.taoensso.examples/tufte "2.6.3"
1+
(defproject com.taoensso.examples/tufte "2.7.0"
22
:description "Tufte example web-app project"
33
:url "https://github.com/ptaoussanis/tufte"
4-
5-
:license
6-
{:name "Eclipse Public License"
7-
:url "http://www.eclipse.org/legal/epl-v10.html"
8-
:distribution :repo
9-
:comments "Same as Clojure"}
10-
4+
:main example.server
115
:plugins [[lein-ancient "0.7.0"]]
126
:dependencies
13-
[[org.clojure/clojure "1.11.1"]
14-
[ring "1.9.6"]
15-
[ring/ring-defaults "0.3.4"]
16-
[compojure "1.7.0"]
7+
[[org.clojure/clojure "1.12.0"]
8+
[ring "1.14.1"]
9+
[ring/ring-defaults "0.6.0"]
10+
[compojure "1.7.1"]
1711
[hiccup "1.0.5"]
18-
[com.taoensso/tufte "2.6.3"]]
19-
20-
:main example.server)
12+
[com.taoensso/tufte "2.7.0"]])

examples/clj/src/example/server.clj

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
(ns example.server
2-
"Quick Tufte example, showing how to use Tufte for
2+
"Quick Tufte example showing how to use Tufte for
33
simple ongoing application performance monitoring.
44
55
Note that a similar pattern can also be used for Cljs apps.
6-
76
See the Tufte README for more info!"
8-
97
{:author "Peter Taoussanis (@ptaoussanis)"}
108
(:require
119
[clojure.string :as str]
1210
[ring.middleware.defaults]
1311
[ring.adapter.jetty]
14-
[compojure.core :as comp :refer (defroutes GET POST)]
12+
[compojure.core :as comp :refer [defroutes GET POST]]
1513
[compojure.route :as route]
1614
[hiccup.core :as hiccup]
17-
[taoensso.encore :as enc :refer (have have?)]
15+
[taoensso.encore :as enc :refer [have have?]]
1816
[taoensso.tufte :as tufte]))
1917

2018
(enc/defonce stats-accumulator
21-
"On eval, will register a Tufte handler to accumulate the results from
22-
all unfiltered `profile` data. Deref this to get the accumulated data.
23-
24-
(tufte/profile {:id :endpoint1} ...)
25-
(tufte/profile {:id :endpoint2} ...)
26-
...
27-
@stats-accumulator => Return accumulated performance stats"
19+
"Accumulates results from all unfiltered `profile` calls.
20+
Deref this to get the accumulated data."
21+
(tufte/stats-accumulator))
2822

29-
(tufte/add-accumulating-handler! {:ns-pattern "*"}))
23+
;; Register handler for `profile` results
24+
(tufte/add-handler! :my-accumulating-handler
25+
(tufte/handler:accumulating stats-accumulator))
3026

3127
(defroutes ring-routes
3228
(GET "/" ring-req
@@ -75,6 +71,11 @@
7571
(defn -main "For `lein run`, etc." []
7672
(println "Starting example Ring server...")
7773
(ring.adapter.jetty/run-jetty #'main-ring-handler {:port 8088 :join? false})
74+
75+
;; Cleanly stop handlers on shutdown
76+
(tufte/call-on-shutdown!
77+
(fn [] (tufte/stop-handlers!)))
78+
7879
(let [uri "http://localhost:8088/"]
7980
(println (str "Jetty running on: " uri))
8081
(try

examples/cljs/deps.edn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
{:deps {org.clojure/clojurescript {:mvn/version "1.11.60"}
2-
com.taoensso/tufte {:mvn/version "2.6.3"}}}
1+
{:deps {org.clojure/clojurescript {:mvn/version "1.11.132"}
2+
com.taoensso/tufte {:mvn/version "2.7.0"}}}

examples/cljs/src/example/hello.cljs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
(ns example.hello
2-
(:require [taoensso.tufte :as tufte
3-
:refer-macros (defnp p profiled profile)]))
2+
(:require [taoensso.tufte :as tufte]))
43

5-
(tufte/add-basic-println-handler! {})
4+
(tufte/add-handler! :my-console-handler (tufte/handler:console))
65

76
(defn get-x [] (+ 1 1))
87
(defn get-y [] (+ 2 2))
98

10-
(profile ; Profile any `p` forms called during body execution
11-
{} ; Profiling options; we'll use the defaults for now
9+
(tufte/profile {:level :info, :id ::my-profiling-id}
1210
(dotimes [_ 5]
13-
(p :get-x (get-x))
14-
(p :get-y (get-y))))
11+
(tufte/p :get-x (get-x))
12+
(tufte/p :get-y (get-y))))

examples/readme_examples.cljc

Lines changed: 0 additions & 58 deletions
This file was deleted.

wiki/1-Getting-started.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ And setup your namespace imports:
1111

1212
```clojure
1313
(ns my-app
14-
(:require [taoensso.tufte :as tufte :refer [defnp p profiled profile]]))
14+
(:require [taoensso.tufte :as tufte]]))
1515
```
1616

1717
# Usage
@@ -23,7 +23,7 @@ Wrap the forms you'd like to [sometimes] profile with [`p`](https://taoensso.git
2323
```clojure
2424
(defn get-customer-info []
2525
(let [raw-customer-map (p ::get-raw-customer (fetch-from-db))]
26-
(p ::enrich-raw-customer
26+
(tufte/p ::enrich-raw-customer
2727
(do-some-work raw-customer-map))))
2828
```
2929

@@ -80,7 +80,7 @@ See [`tufte/*ns-filter*`](https://taoensso.github.io/tufte/taoensso.tufte.html#v
8080
`p`, `profiled`, and `profile` forms can take an optional **profiling level**`#{0 1 2 3 4 5}`, e.g.:
8181

8282
```clojure
83-
(profiled {:level 3} ...) ; Only activates profiling when (>= 3 *min-level*)
83+
(tufte/profiled {:level 3} ...) ; Only activates profiling when (>= 3 *min-level*)
8484
```
8585

8686
These form levels will be checked against [`tufte/*min-level*`](https://taoensso.github.io/tufte/taoensso.tufte.html#var-*min-level*) which can be an constant integer, or a `[[<ns-pattern> <min-level-int>] ... ["*" <default-min-level-int>]]` for namespace-specific levels.
@@ -97,15 +97,15 @@ Compile-time | `taoensso.tufte.min-level` JVM property or `TAOENSSO_TUFTE_MIN_LE
9797
Finally, `profiled` and `profile` both support an optional arbitrary test expression:
9898

9999
```clojure
100-
(profiled {:when my-cond?} ...) ; Only activates profiling when `my-cond?` is truthy
100+
(tufte/profiled {:when my-cond?} ...) ; Only activates profiling when `my-cond?` is truthy
101101
```
102102

103103
This can be used for a wide range of sophisticated behaviour including smart, **application-aware rate limiting**.
104104

105105
As one simpler example, we can get **sampled profiling** like this:
106106

107107
```clojure
108-
(profiled {:when (tufte/chance 0.5)} ...) ; Only activates profiling with 50% probability
108+
(tufte/profiled {:when (tufte/chance 0.5)} ...) ; Only activates profiling with 50% probability
109109
```
110110

111111
# Formatting `pstats`
@@ -129,15 +129,17 @@ If you're using [`tufte/add-basic-println-handler!`](https://taoensso.github.io/
129129
through the `:format-pstats-opts` option:
130130

131131
```clojure
132-
(tufte/add-basic-println-handler!
133-
{:format-pstats-opts {:columns [:n :p50 :mean :clock :sum]
134-
:format-id-fn name}})
132+
;; See the individual docstrings for options, etc.:
133+
(tufte/add-handler! :my-console-handler
134+
(tufte/handler:console
135+
{:output-fn
136+
(tufte/format-signal-fn
137+
{:format-pstats-opts {:columns [:n :p50 :mean :clock :sum]}})}))
135138

136139
(defnp get-x [] (Thread/sleep 500) "x val")
137140
(defnp get-y [] (Thread/sleep (rand-int 1000)) "y val")
138141

139-
(profile
140-
{}
142+
(tufte/profile {:level :info, :id ::my-profiling-id}
141143
(dotimes [_ 5]
142144
(get-x)
143145
(get-y)))

0 commit comments

Comments
 (0)