typenative 0.7.0 → 0.7.1

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.
Files changed (217) hide show
  1. package/bin/node/assert.js +2 -2
  2. package/bin/node/child_process.js +66 -8
  3. package/bin/node/fs.js +1012 -4
  4. package/bin/node/histogram.js +1991 -0
  5. package/bin/node/process.js +14 -2
  6. package/bin/node/sqlite.js +1488 -5
  7. package/bin/node/stream.js +2 -1
  8. package/bin/node/system_extra.js +3 -270
  9. package/bin/node/test.js +39 -1
  10. package/bin/node/test_report.js +47 -1
  11. package/bin/node/test_run.js +57 -2
  12. package/bin/node/util.js +8 -5
  13. package/bin/node/webassembly.js +20 -5
  14. package/bin/node/webview.js +3257 -244
  15. package/bin/node/zlib.js +118 -14
  16. package/bin/node/zlib_iter.js +585 -0
  17. package/bin/transpiler.js +1370 -258
  18. package/bin/version.js +1 -1
  19. package/govendor/software.sslmate.com/src/go-pkcs12/.gitattributes +10 -0
  20. package/govendor/software.sslmate.com/src/go-pkcs12/LICENSE +28 -0
  21. package/govendor/software.sslmate.com/src/go-pkcs12/README.md +31 -0
  22. package/govendor/software.sslmate.com/src/go-pkcs12/bmp-string.go +63 -0
  23. package/govendor/software.sslmate.com/src/go-pkcs12/crypto.go +331 -0
  24. package/govendor/software.sslmate.com/src/go-pkcs12/errors.go +23 -0
  25. package/govendor/software.sslmate.com/src/go-pkcs12/go.mod +3 -0
  26. package/govendor/software.sslmate.com/src/go-pkcs12/internal/rc2/rc2.go +271 -0
  27. package/govendor/software.sslmate.com/src/go-pkcs12/mac.go +202 -0
  28. package/govendor/software.sslmate.com/src/go-pkcs12/pbkdf.go +184 -0
  29. package/govendor/software.sslmate.com/src/go-pkcs12/pkcs12.go +1000 -0
  30. package/govendor/software.sslmate.com/src/go-pkcs12/safebags.go +107 -0
  31. package/package.json +10 -1
  32. package/types/tsconfig.json +17 -0
  33. package/types/typenative-npm.d.ts +492 -12
  34. package/types/typenative.d.ts +88 -2
  35. package/.gitattributes +0 -1
  36. package/.github/workflows/ci.yml +0 -68
  37. package/.github/workflows/npm-publish-github-packages.yml +0 -88
  38. package/CHANGELOG.md +0 -1746
  39. package/CONTRIBUTION.md +0 -133
  40. package/conformance.md +0 -1497
  41. package/examples/README.md +0 -114
  42. package/examples/angular-templates/README.md +0 -141
  43. package/examples/angular-templates/bench.mjs +0 -173
  44. package/examples/angular-templates/dom-type-globals.d.ts +0 -21
  45. package/examples/angular-templates/dom-type-stubs.ts +0 -21
  46. package/examples/angular-templates/main.ts +0 -135
  47. package/examples/angular-templates/parity.mjs +0 -157
  48. package/examples/angular-templates/run-node.mjs +0 -69
  49. package/examples/angular-templates/vendor/angular/compiler/LICENSE +0 -21
  50. package/examples/desktop-notes/README.md +0 -106
  51. package/examples/desktop-notes/bench.mjs +0 -125
  52. package/examples/desktop-notes/main.ts +0 -131
  53. package/examples/desktop-notes/package-lock.json +0 -726
  54. package/examples/desktop-notes/package.json +0 -9
  55. package/examples/desktop-notes/ui.ts +0 -176
  56. package/examples/dupe-finder/README.md +0 -78
  57. package/examples/dupe-finder/bench.mjs +0 -120
  58. package/examples/dupe-finder/main.ts +0 -87
  59. package/examples/fibonacci/README.md +0 -89
  60. package/examples/fibonacci/bench.mjs +0 -87
  61. package/examples/fibonacci/main.ts +0 -21
  62. package/examples/hello-world/README.md +0 -62
  63. package/examples/hello-world/bench.mjs +0 -66
  64. package/examples/hello-world/main.ts +0 -7
  65. package/examples/json-server/README.md +0 -132
  66. package/examples/json-server/bench.mjs +0 -263
  67. package/examples/json-server/main.ts +0 -67
  68. package/examples/mandelbrot-png/README.md +0 -63
  69. package/examples/mandelbrot-png/bench.mjs +0 -95
  70. package/examples/mandelbrot-png/main.ts +0 -98
  71. package/examples/mandelbrot-png/preview.png +0 -0
  72. package/examples/marked-cli/README.md +0 -174
  73. package/examples/marked-cli/bench.mjs +0 -178
  74. package/examples/marked-cli/main.ts +0 -410
  75. package/examples/marked-cli/parity.mjs +0 -185
  76. package/examples/marked-cli/vendor/marked/LICENSE +0 -44
  77. package/examples/marked-cli/vendor/marked/VENDOR.md +0 -26
  78. package/examples/openai-chat/README.md +0 -148
  79. package/examples/openai-chat/bench.mjs +0 -175
  80. package/examples/openai-chat/main.ts +0 -488
  81. package/examples/react-compiler/README.md +0 -185
  82. package/examples/react-compiler/bench.mjs +0 -181
  83. package/examples/react-compiler/main.ts +0 -324
  84. package/examples/react-compiler/parity.mjs +0 -362
  85. package/examples/react-compiler/run-node.mjs +0 -90
  86. package/examples/react-compiler/vendor/babel/LICENSE +0 -22
  87. package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-lower.json +0 -15
  88. package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-upper.json +0 -51
  89. package/examples/react-compiler/vendor/charcodes/package.json +0 -25
  90. package/examples/react-compiler/vendor/invariant/LICENSE +0 -21
  91. package/examples/react-compiler/vendor/invariant/invariant.cjs +0 -51
  92. package/examples/react-compiler/vendor/invariant/invariant.d.cts +0 -11
  93. package/examples/react-compiler/vendor/invariant/types/LICENSE +0 -21
  94. package/examples/react-compiler/vendor/jridgewell/gen-mapping/LICENSE +0 -19
  95. package/examples/react-compiler/vendor/jridgewell/resolve-uri/LICENSE +0 -19
  96. package/examples/react-compiler/vendor/jridgewell/sourcemap-codec/LICENSE +0 -19
  97. package/examples/react-compiler/vendor/jridgewell/trace-mapping/LICENSE +0 -19
  98. package/examples/react-compiler/vendor/js-tokens/LICENSE +0 -21
  99. package/examples/react-compiler/vendor/js-tokens/index.cjs +0 -23
  100. package/examples/react-compiler/vendor/js-tokens/index.d.cts +0 -13
  101. package/examples/react-compiler/vendor/jsesc/LICENSE-MIT.txt +0 -20
  102. package/examples/react-compiler/vendor/jsesc/jsesc.cjs +0 -337
  103. package/examples/react-compiler/vendor/jsesc/jsesc.d.cts +0 -384
  104. package/examples/react-compiler/vendor/jsesc/types/LICENSE +0 -21
  105. package/examples/react-compiler/vendor/picocolors/LICENSE +0 -15
  106. package/examples/react-compiler/vendor/picocolors/picocolors.cjs +0 -75
  107. package/examples/react-compiler/vendor/picocolors/picocolors.d.cts +0 -5
  108. package/examples/react-compiler/vendor/picocolors/types.d.ts +0 -51
  109. package/examples/react-compiler/vendor/react-compiler/LICENSE +0 -21
  110. package/examples/react-compiler/vendor/stubs/babel-core.ts +0 -31
  111. package/examples/react-compiler/vendor/stubs/debug.ts +0 -18
  112. package/examples/react-compiler/vendor/stubs/js-tokens.d.ts +0 -13
  113. package/examples/react-compiler/vendor/stubs/pretty-format.ts +0 -18
  114. package/examples/react-compiler/vendor/zod/LICENSE +0 -21
  115. package/examples/react-compiler/vendor/zod-validation-error/LICENSE +0 -9
  116. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/NonEmptyArray.ts +0 -5
  117. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinPath.ts +0 -49
  118. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinValues.ts +0 -48
  119. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/stringify.ts +0 -50
  120. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/titleCase.ts +0 -6
  121. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/MessageBuilder.ts +0 -126
  122. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/ValidationError.ts +0 -39
  123. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/custom.ts +0 -12
  124. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/errorMap.ts +0 -78
  125. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/index.ts +0 -2
  126. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidElement.ts +0 -12
  127. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidKey.ts +0 -12
  128. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidStringFormat.ts +0 -131
  129. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidType.ts +0 -54
  130. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidUnion.ts +0 -12
  131. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidValue.ts +0 -40
  132. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/notMultipleOf.ts +0 -12
  133. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooBig.ts +0 -76
  134. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooSmall.ts +0 -76
  135. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/types.ts +0 -27
  136. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/unrecognizedKeys.ts +0 -27
  137. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromError.ts +0 -13
  138. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodError.ts +0 -61
  139. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodIssue.ts +0 -38
  140. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/index.ts +0 -20
  141. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationError.ts +0 -5
  142. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationErrorLike.ts +0 -8
  143. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isZodErrorLike.ts +0 -11
  144. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/toValidationError.ts +0 -20
  145. package/examples/react-compiler/vendor.mjs +0 -688
  146. package/examples/todo-cli/README.md +0 -69
  147. package/examples/todo-cli/bench.mjs +0 -89
  148. package/examples/todo-cli/main.ts +0 -92
  149. package/examples/yaml-cli/README.md +0 -152
  150. package/examples/yaml-cli/bench.mjs +0 -265
  151. package/examples/yaml-cli/main.ts +0 -251
  152. package/examples/yaml-cli/parity.mjs +0 -156
  153. package/examples/yaml-cli/vendor/yaml/LICENSE +0 -13
  154. package/examples/yaml-cli/vendor/yaml/VENDOR.md +0 -25
  155. package/performance/README.md +0 -248
  156. package/performance/any-dynamic.ts +0 -21
  157. package/performance/any-record-view.ts +0 -20
  158. package/performance/arr-for-of.ts +0 -29
  159. package/performance/arr-hof.ts +0 -21
  160. package/performance/arr-push-index.ts +0 -15
  161. package/performance/arr-push-local.ts +0 -39
  162. package/performance/arr-slice-concat.ts +0 -24
  163. package/performance/arr-sort.ts +0 -21
  164. package/performance/async-await.ts +0 -24
  165. package/performance/async-promise-all.ts +0 -38
  166. package/performance/bigint-arith.ts +0 -36
  167. package/performance/buf-encode.ts +0 -29
  168. package/performance/class-accessors.ts +0 -48
  169. package/performance/class-dispatch.ts +0 -52
  170. package/performance/date-format.ts +0 -18
  171. package/performance/events-emit.ts +0 -27
  172. package/performance/exc-throw-rare.ts +0 -25
  173. package/performance/exc-try-catch.ts +0 -26
  174. package/performance/fn-closures.ts +0 -29
  175. package/performance/fn-recursion-tree.ts +0 -31
  176. package/performance/fn-rest-spread.ts +0 -43
  177. package/performance/gen-for-of.ts +0 -46
  178. package/performance/gen-next.ts +0 -32
  179. package/performance/iter-helpers.ts +0 -38
  180. package/performance/json-any.ts +0 -30
  181. package/performance/json-roundtrip.ts +0 -27
  182. package/performance/map-delete.ts +0 -24
  183. package/performance/map-iterate.ts +0 -26
  184. package/performance/map-number-keys.ts +0 -37
  185. package/performance/map-string-keys.ts +0 -22
  186. package/performance/num-arith-loop.ts +0 -14
  187. package/performance/num-bitwise.ts +0 -21
  188. package/performance/num-hash-imul.ts +0 -36
  189. package/performance/num-mod-int.ts +0 -16
  190. package/performance/num-to-string.ts +0 -16
  191. package/performance/obj-alloc.ts +0 -32
  192. package/performance/obj-keys-entries.ts +0 -44
  193. package/performance/obj-optional-chain.ts +0 -42
  194. package/performance/obj-record-dict.ts +0 -24
  195. package/performance/obj-spread-destructure.ts +0 -40
  196. package/performance/regex-exec.ts +0 -29
  197. package/performance/regex-replace-fn.ts +0 -24
  198. package/performance/regex-site.ts +0 -28
  199. package/performance/set-ops.ts +0 -20
  200. package/performance/str-char-scan.ts +0 -35
  201. package/performance/str-concat-plus.ts +0 -18
  202. package/performance/str-concat.ts +0 -24
  203. package/performance/str-methods.ts +0 -24
  204. package/performance/str-short-scan.ts +0 -26
  205. package/performance/str-split-join.ts +0 -23
  206. package/performance/str-switch-compare.ts +0 -50
  207. package/performance/str-template.ts +0 -16
  208. package/performance/str-unicode.ts +0 -25
  209. package/performance/typed-array.ts +0 -30
  210. package/performance/union-switch.ts +0 -57
  211. package/performance/union-typeof.ts +0 -40
  212. package/performance/wasm-call.ts +0 -63
  213. package/tsparser/check.go +0 -158
  214. package/tsparser/go.mod +0 -14
  215. package/tsparser/go.sum +0 -22
  216. package/tsparser/main.go +0 -363
  217. package/tsparser/tsparser.exe +0 -4
@@ -0,0 +1,1991 @@
1
+ // node:perf_hooks HdrHistogram, statistical tests, EWMA/SLO burn rate,
2
+ // CBOR v2 export/importHistogram, and SlidingWindowHistogram (Node 26).
3
+ import { defineClass, defineHelper } from './registry.js';
4
+ const histogramHelper = `// HdrHistogram + statistical tests + CBOR v2 export/import + SlidingWindowHistogram
5
+ type TnPercentileCI struct {
6
+ value float64
7
+ lower float64
8
+ upper float64
9
+ }
10
+
11
+ type TnMeanCI struct {
12
+ mean float64
13
+ lower float64
14
+ upper float64
15
+ }
16
+
17
+ type TnConfidenceInterval struct {
18
+ lower float64
19
+ upper float64
20
+ }
21
+
22
+ type TnWelchResult struct {
23
+ tStatistic float64
24
+ degreesOfFreedom float64
25
+ pValue float64
26
+ confidenceInterval *TnConfidenceInterval
27
+ }
28
+
29
+ type TnMannWhitneyResult struct {
30
+ uStatistic float64
31
+ zScore float64
32
+ pValue float64
33
+ }
34
+
35
+ type TnQrdeResult struct {
36
+ probabilities *TnFloat64Array
37
+ quantiles *TnFloat64Array
38
+ densities *TnFloat64Array
39
+ count *tnbig.Int
40
+ bucketCount float64
41
+ corrections float64
42
+ dequantize string
43
+ }
44
+
45
+ type TnHistogram struct {
46
+ lowest int64
47
+ highest int64
48
+ figures int
49
+ unitMagnitude int
50
+ subBucketHalfCountMagnitude int
51
+ subBucketHalfCount int64
52
+ subBucketCount int64
53
+ subBucketMask int64
54
+ bucketCount int
55
+ countsLen int
56
+ counts []int64
57
+ totalCount int64
58
+ minVal int64
59
+ maxVal int64
60
+ exceeds int64
61
+ resetCount int64
62
+
63
+ // EWMA / SLO tracking
64
+ ewmaEnabled bool
65
+ ewmaAlpha float64
66
+ ewmaMean float64
67
+ ewmaVar float64
68
+ ewmaErrorRate float64
69
+ ewmaThreshold float64
70
+ ewmaHasThreshold bool
71
+ ewmaCount int64
72
+
73
+ // recordDelta / monitorEventLoopDelay
74
+ enabled bool
75
+ stop chan struct{}
76
+ interval float64
77
+ last time.Time
78
+ }
79
+
80
+ func tnHdrInit(lowest, highest int64, figures int) *TnHistogram {
81
+ if lowest < 1 {
82
+ lowest = 1
83
+ }
84
+ if highest < lowest*2 {
85
+ highest = lowest * 2
86
+ }
87
+ if figures < 1 {
88
+ figures = 1
89
+ }
90
+ if figures > 5 {
91
+ figures = 5
92
+ }
93
+ unitMag := int(math.Floor(math.Log2(float64(lowest))))
94
+ largestSingleUnit := 2.0 * math.Pow(10, float64(figures))
95
+ subBucketCountMag := int(math.Ceil(math.Log2(largestSingleUnit)))
96
+ subBucketHalfCountMag := subBucketCountMag - 1
97
+ if subBucketHalfCountMag < 0 {
98
+ subBucketHalfCountMag = 0
99
+ }
100
+ subBucketCount := int64(1) << uint(subBucketHalfCountMag+1)
101
+ subBucketHalfCount := subBucketCount >> 1
102
+ subBucketMask := (subBucketCount - 1) << uint(unitMag)
103
+
104
+ // Buckets needed to cover highest
105
+ bucketsNeeded := 1
106
+ smallestUntrackable := uint64(subBucketCount) << uint(unitMag)
107
+ for smallestUntrackable <= uint64(highest) && smallestUntrackable > 0 {
108
+ smallestUntrackable <<= 1
109
+ bucketsNeeded++
110
+ }
111
+ countsLen := (bucketsNeeded + 1) * int(subBucketHalfCount)
112
+ return &TnHistogram{
113
+ lowest: lowest,
114
+ highest: highest,
115
+ figures: figures,
116
+ unitMagnitude: unitMag,
117
+ subBucketHalfCountMagnitude: subBucketHalfCountMag,
118
+ subBucketHalfCount: subBucketHalfCount,
119
+ subBucketCount: subBucketCount,
120
+ subBucketMask: subBucketMask,
121
+ bucketCount: bucketsNeeded,
122
+ countsLen: countsLen,
123
+ counts: make([]int64, countsLen),
124
+ minVal: math.MaxInt64,
125
+ maxVal: 0,
126
+ }
127
+ }
128
+
129
+ func tnToInt64Val(v interface{}) (int64, bool) {
130
+ switch x := v.(type) {
131
+ case float64:
132
+ if math.IsNaN(x) || math.IsInf(x, 0) {
133
+ return 0, false
134
+ }
135
+ return int64(x), true
136
+ case int:
137
+ return int64(x), true
138
+ case int64:
139
+ return x, true
140
+ case *tnbig.Int:
141
+ if x == nil {
142
+ return 0, false
143
+ }
144
+ return x.Int64(), true
145
+ }
146
+ return 0, false
147
+ }
148
+
149
+ func TnCreateHistogram(options ...interface{}) *TnHistogram {
150
+ lowest := int64(1)
151
+ highest := int64(9007199254740991) // Number.MAX_SAFE_INTEGER
152
+ figures := 3
153
+ var halfLife float64
154
+ hasHalfLife := false
155
+ var threshold float64
156
+ hasThreshold := false
157
+
158
+ if len(options) > 0 && options[0] != nil {
159
+ opt := options[0]
160
+ if v, ok := tnToInt64Val(TnGetValue(opt, "lowest")); ok {
161
+ if v < 1 {
162
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", "The value of \\"options.lowest\\" is out of range. It must be >= 1."))
163
+ }
164
+ lowest = v
165
+ }
166
+ if v, ok := tnToInt64Val(TnGetValue(opt, "highest")); ok {
167
+ if v < lowest*2 {
168
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", "The value of \\"options.highest\\" is out of range."))
169
+ }
170
+ highest = v
171
+ }
172
+ if v, ok := TnGetValue(opt, "figures").(float64); ok {
173
+ if v < 1 || v > 5 || v != math.Trunc(v) {
174
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", "The value of \\"options.figures\\" is out of range. It must be >= 1 && <= 5."))
175
+ }
176
+ figures = int(v)
177
+ }
178
+ if v, ok := TnGetValue(opt, "halfLife").(float64); ok {
179
+ if !(v > 0) || math.IsInf(v, 0) {
180
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", "The value of \\"options.halfLife\\" is out of range. It must be > 0."))
181
+ }
182
+ halfLife = v
183
+ hasHalfLife = true
184
+ }
185
+ if v, ok := TnGetValue(opt, "threshold").(float64); ok {
186
+ if !(v >= 0) || math.IsInf(v, 0) {
187
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", "The value of \\"options.threshold\\" is out of range."))
188
+ }
189
+ threshold = v
190
+ hasThreshold = true
191
+ } else if bi, ok := TnGetValue(opt, "threshold").(*tnbig.Int); ok && bi != nil {
192
+ f, _ := new(tnbig.Float).SetInt(bi).Float64()
193
+ threshold = f
194
+ hasThreshold = true
195
+ }
196
+ }
197
+
198
+ h := tnHdrInit(lowest, highest, figures)
199
+ if hasHalfLife {
200
+ h.ewmaEnabled = true
201
+ h.ewmaAlpha = 1.0 - math.Pow(0.5, 1.0/halfLife)
202
+ if hasThreshold {
203
+ h.ewmaHasThreshold = true
204
+ h.ewmaThreshold = threshold
205
+ }
206
+ }
207
+ return h
208
+ }
209
+
210
+ func (h *TnHistogram) getBucketIndex(v int64) int {
211
+ if v < 0 {
212
+ return 0
213
+ }
214
+ pow2Ceiling := 64 - bits.LeadingZeros64(uint64(v|h.subBucketMask))
215
+ idx := pow2Ceiling - h.unitMagnitude - (h.subBucketHalfCountMagnitude + 1)
216
+ if idx < 0 {
217
+ return 0
218
+ }
219
+ return idx
220
+ }
221
+
222
+ func (h *TnHistogram) getSubBucketIndex(v int64, bucketIdx int) int {
223
+ return int(uint64(v) >> uint(bucketIdx+h.unitMagnitude))
224
+ }
225
+
226
+ func (h *TnHistogram) countsIndex(v int64) int {
227
+ bucketIdx := h.getBucketIndex(v)
228
+ subIdx := h.getSubBucketIndex(v, bucketIdx)
229
+ return (bucketIdx+1)*int(h.subBucketHalfCount) + subIdx - int(h.subBucketHalfCount)
230
+ }
231
+
232
+ func (h *TnHistogram) valueFromIndex(index int) int64 {
233
+ bucketIdx := (index >> uint(h.subBucketHalfCountMagnitude)) - 1
234
+ subIdx := int64(index&(int(h.subBucketHalfCount)-1)) + h.subBucketHalfCount
235
+ if bucketIdx < 0 {
236
+ subIdx -= h.subBucketHalfCount
237
+ bucketIdx = 0
238
+ }
239
+ return subIdx << uint(bucketIdx+h.unitMagnitude)
240
+ }
241
+
242
+ func (h *TnHistogram) sizeOfEquivalentValueRange(v int64) int64 {
243
+ bucketIdx := h.getBucketIndex(v)
244
+ subIdx := h.getSubBucketIndex(v, bucketIdx)
245
+ shift := bucketIdx + h.unitMagnitude
246
+ if int64(subIdx) >= h.subBucketCount {
247
+ shift++
248
+ }
249
+ return int64(1) << uint(shift)
250
+ }
251
+
252
+ func (h *TnHistogram) lowestEquivalentValue(v int64) int64 {
253
+ bucketIdx := h.getBucketIndex(v)
254
+ subIdx := h.getSubBucketIndex(v, bucketIdx)
255
+ return int64(subIdx) << uint(bucketIdx+h.unitMagnitude)
256
+ }
257
+
258
+ func (h *TnHistogram) nextNonEquivalentValue(v int64) int64 {
259
+ return h.lowestEquivalentValue(v) + h.sizeOfEquivalentValueRange(v)
260
+ }
261
+
262
+ func (h *TnHistogram) highestEquivalentValue(v int64) int64 {
263
+ return h.nextNonEquivalentValue(v) - 1
264
+ }
265
+
266
+ func (h *TnHistogram) medianEquivalentValue(v int64) int64 {
267
+ return h.lowestEquivalentValue(v) + (h.sizeOfEquivalentValueRange(v) >> 1)
268
+ }
269
+
270
+ func (h *TnHistogram) updateEwma(val float64) {
271
+ if !h.ewmaEnabled {
272
+ return
273
+ }
274
+ if h.ewmaCount == 0 {
275
+ h.ewmaMean = val
276
+ h.ewmaVar = 0
277
+ if h.ewmaHasThreshold {
278
+ if val > h.ewmaThreshold {
279
+ h.ewmaErrorRate = 1.0
280
+ } else {
281
+ h.ewmaErrorRate = 0.0
282
+ }
283
+ }
284
+ h.ewmaCount = 1
285
+ return
286
+ }
287
+ alpha := h.ewmaAlpha
288
+ diff := val - h.ewmaMean
289
+ incr := alpha * diff
290
+ h.ewmaMean += incr
291
+ h.ewmaVar = (1.0 - alpha) * (h.ewmaVar + diff*incr)
292
+ if h.ewmaHasThreshold {
293
+ errVal := 0.0
294
+ if val > h.ewmaThreshold {
295
+ errVal = 1.0
296
+ }
297
+ h.ewmaErrorRate = (1.0-alpha)*h.ewmaErrorRate + alpha*errVal
298
+ }
299
+ h.ewmaCount++
300
+ }
301
+
302
+ func (h *TnHistogram) recordInt64(v int64, count int64) bool {
303
+ if v < 0 || count <= 0 {
304
+ return false
305
+ }
306
+ if v > h.highest {
307
+ h.exceeds += count
308
+ return false
309
+ }
310
+ idx := h.countsIndex(v)
311
+ if idx < 0 || idx >= h.countsLen {
312
+ h.exceeds += count
313
+ return false
314
+ }
315
+ h.counts[idx] += count
316
+ h.totalCount += count
317
+ if v < h.minVal {
318
+ h.minVal = v
319
+ }
320
+ if v > h.maxVal {
321
+ h.maxVal = v
322
+ }
323
+ return true
324
+ }
325
+
326
+ func (h *TnHistogram) record(value interface{}) {
327
+ v, ok := tnToInt64Val(value)
328
+ if !ok {
329
+ return
330
+ }
331
+ if v < 1 {
332
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", "The value of \\"val\\" is out of range. It must be >= 1."))
333
+ }
334
+ if h.recordInt64(v, 1) {
335
+ h.updateEwma(float64(v))
336
+ }
337
+ }
338
+
339
+ func (h *TnHistogram) recordDelta() {
340
+ now := time.Now()
341
+ if !h.last.IsZero() {
342
+ delta := now.Sub(h.last).Nanoseconds()
343
+ if delta < 1 {
344
+ delta = 1
345
+ }
346
+ if h.recordInt64(delta, 1) {
347
+ h.updateEwma(float64(delta))
348
+ }
349
+ }
350
+ h.last = now
351
+ }
352
+
353
+ func (h *TnHistogram) recordCorrected(value interface{}, interval float64) {
354
+ v, ok := tnToInt64Val(value)
355
+ if !ok {
356
+ return
357
+ }
358
+ h.record(value)
359
+ iv := int64(interval)
360
+ if iv <= 0 {
361
+ return
362
+ }
363
+ for missing := v - iv; missing >= iv; missing -= iv {
364
+ if h.recordInt64(missing, 1) {
365
+ h.updateEwma(float64(missing))
366
+ }
367
+ }
368
+ }
369
+
370
+ func (h *TnHistogram) recomputeMinMax() {
371
+ h.minVal = math.MaxInt64
372
+ h.maxVal = 0
373
+ var total int64
374
+ for i, c := range h.counts {
375
+ if c > 0 {
376
+ total += c
377
+ v := h.valueFromIndex(i)
378
+ if v < h.minVal {
379
+ h.minVal = v
380
+ }
381
+ if v > h.maxVal {
382
+ h.maxVal = v
383
+ }
384
+ }
385
+ }
386
+ h.totalCount = total
387
+ }
388
+
389
+ func (h *TnHistogram) Get_count() float64 { return float64(h.totalCount) }
390
+ func (h *TnHistogram) Get_countBigInt() *tnbig.Int { return tnbig.NewInt(h.totalCount) }
391
+
392
+ func (h *TnHistogram) minInt64() int64 {
393
+ if h.totalCount == 0 {
394
+ return math.MaxInt64
395
+ }
396
+ return h.lowestEquivalentValue(h.minVal)
397
+ }
398
+
399
+ func (h *TnHistogram) maxInt64() int64 {
400
+ if h.totalCount == 0 {
401
+ return 0
402
+ }
403
+ return h.highestEquivalentValue(h.maxVal)
404
+ }
405
+
406
+ func (h *TnHistogram) Get_min() float64 { return float64(h.minInt64()) }
407
+ func (h *TnHistogram) Get_minBigInt() *tnbig.Int { return tnbig.NewInt(h.minInt64()) }
408
+ func (h *TnHistogram) Get_max() float64 { return float64(h.maxInt64()) }
409
+ func (h *TnHistogram) Get_maxBigInt() *tnbig.Int { return tnbig.NewInt(h.maxInt64()) }
410
+ func (h *TnHistogram) Get_exceeds() float64 { return float64(h.exceeds) }
411
+ func (h *TnHistogram) Get_exceedsBigInt() *tnbig.Int { return tnbig.NewInt(h.exceeds) }
412
+ func (h *TnHistogram) Get_resetCount() float64 { return float64(h.resetCount) }
413
+
414
+ func (h *TnHistogram) Get_ewmaMean() float64 { return h.ewmaMean }
415
+ func (h *TnHistogram) Get_ewmaStddev() float64 {
416
+ if h.ewmaVar <= 0 {
417
+ return 0
418
+ }
419
+ return math.Sqrt(h.ewmaVar)
420
+ }
421
+ func (h *TnHistogram) Get_ewmaErrorRate() float64 { return h.ewmaErrorRate }
422
+
423
+ func (h *TnHistogram) burnRate(sloTarget float64) float64 {
424
+ if !(sloTarget > 0 && sloTarget < 1) || math.IsNaN(sloTarget) {
425
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", "The value of \\"sloTarget\\" is out of range. It must be > 0 && < 1."))
426
+ }
427
+ if !h.ewmaEnabled || !h.ewmaHasThreshold || h.ewmaCount == 0 {
428
+ return 0
429
+ }
430
+ return h.ewmaErrorRate / (1.0 - sloTarget)
431
+ }
432
+
433
+ func (h *TnHistogram) Get_mean() float64 {
434
+ if h.totalCount == 0 {
435
+ return math.NaN()
436
+ }
437
+ total := 0.0
438
+ for i, c := range h.counts {
439
+ if c > 0 {
440
+ total += float64(c) * float64(h.medianEquivalentValue(h.valueFromIndex(i)))
441
+ }
442
+ }
443
+ return total / float64(h.totalCount)
444
+ }
445
+
446
+ func (h *TnHistogram) Get_stddev() float64 {
447
+ if h.totalCount == 0 {
448
+ return math.NaN()
449
+ }
450
+ mean := h.Get_mean()
451
+ sumSq := 0.0
452
+ for i, c := range h.counts {
453
+ if c > 0 {
454
+ d := float64(h.medianEquivalentValue(h.valueFromIndex(i))) - mean
455
+ sumSq += float64(c) * d * d
456
+ }
457
+ }
458
+ return math.Sqrt(sumSq / float64(h.totalCount))
459
+ }
460
+
461
+ func (h *TnHistogram) moments() (float64, float64, float64) {
462
+ if h.totalCount == 0 {
463
+ return 0, 0, 0
464
+ }
465
+ n := float64(h.totalCount)
466
+ mean := h.Get_mean()
467
+ var m2, m3, m4 float64
468
+ for i, c := range h.counts {
469
+ if c > 0 {
470
+ fc := float64(c)
471
+ d := float64(h.medianEquivalentValue(h.valueFromIndex(i))) - mean
472
+ d2 := d * d
473
+ m2 += fc * d2
474
+ m3 += fc * d2 * d
475
+ m4 += fc * d2 * d2
476
+ }
477
+ }
478
+ return m2 / n, m3 / n, m4 / n
479
+ }
480
+
481
+ func (h *TnHistogram) Get_skewness() float64 {
482
+ m2, m3, _ := h.moments()
483
+ if m2 == 0 {
484
+ return 0
485
+ }
486
+ return m3 / math.Pow(m2, 1.5)
487
+ }
488
+
489
+ func (h *TnHistogram) Get_kurtosis() float64 {
490
+ m2, _, m4 := h.moments()
491
+ if m2 == 0 {
492
+ return 0
493
+ }
494
+ return m4/(m2*m2) - 3.0
495
+ }
496
+
497
+ func (h *TnHistogram) valueAtCount(target int64) int64 {
498
+ if h.totalCount == 0 {
499
+ return 0
500
+ }
501
+ if target < 1 {
502
+ target = 1
503
+ }
504
+ if target > h.totalCount {
505
+ target = h.totalCount
506
+ }
507
+ var total int64
508
+ for i, c := range h.counts {
509
+ if c > 0 {
510
+ total += c
511
+ if total >= target {
512
+ return h.highestEquivalentValue(h.valueFromIndex(i))
513
+ }
514
+ }
515
+ }
516
+ return h.maxInt64()
517
+ }
518
+
519
+ func (h *TnHistogram) percentileInt64(p float64) int64 {
520
+ if h.totalCount == 0 {
521
+ return 0
522
+ }
523
+ if p > 100 {
524
+ p = 100
525
+ }
526
+ if p <= 0 {
527
+ return h.valueAtCount(1)
528
+ }
529
+ target := int64(math.Ceil((p / 100.0) * float64(h.totalCount)))
530
+ if target < 1 {
531
+ target = 1
532
+ }
533
+ return h.valueAtCount(target)
534
+ }
535
+
536
+ func (h *TnHistogram) percentile(p float64) float64 {
537
+ if !(p > 0 && p <= 100) || math.IsNaN(p) {
538
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", "The value of \\"percentile\\" is out of range. It must be > 0 && <= 100."))
539
+ }
540
+ return float64(h.percentileInt64(p))
541
+ }
542
+
543
+ func (h *TnHistogram) percentileBigInt(p float64) *tnbig.Int {
544
+ if !(p > 0 && p <= 100) || math.IsNaN(p) {
545
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", "The value of \\"percentile\\" is out of range. It must be > 0 && <= 100."))
546
+ }
547
+ return tnbig.NewInt(h.percentileInt64(p))
548
+ }
549
+
550
+ func (h *TnHistogram) percentilesAt(ps []float64) *TnMap[float64, float64] {
551
+ m := TnNewMap[float64, float64]()
552
+ for _, p := range ps {
553
+ m.Set(p, h.percentile(p))
554
+ }
555
+ return m
556
+ }
557
+
558
+ func (h *TnHistogram) Get_percentiles() *TnMap[float64, float64] {
559
+ m := TnNewMap[float64, float64]()
560
+ if h.totalCount == 0 {
561
+ return m
562
+ }
563
+ // Standard HdrHistogram percentile ticks (halving distance to 100%)
564
+ m.Set(0, float64(h.minInt64()))
565
+ p := 50.0
566
+ for {
567
+ v := float64(h.percentileInt64(p))
568
+ m.Set(p, v)
569
+ if p >= 99.9999 || v >= float64(h.maxInt64()) {
570
+ break
571
+ }
572
+ p = 100.0 - (100.0-p)/2.0
573
+ }
574
+ m.Set(100, float64(h.maxInt64()))
575
+ return m
576
+ }
577
+
578
+ func (h *TnHistogram) Get_percentilesBigInt() *TnMap[float64, *tnbig.Int] {
579
+ m := TnNewMap[float64, *tnbig.Int]()
580
+ if h.totalCount == 0 {
581
+ return m
582
+ }
583
+ m.Set(0, tnbig.NewInt(h.minInt64()))
584
+ p := 50.0
585
+ for {
586
+ v := h.percentileInt64(p)
587
+ m.Set(p, tnbig.NewInt(v))
588
+ if p >= 99.9999 || v >= h.maxInt64() {
589
+ break
590
+ }
591
+ p = 100.0 - (100.0-p)/2.0
592
+ }
593
+ m.Set(100, tnbig.NewInt(h.maxInt64()))
594
+ return m
595
+ }
596
+
597
+ func (h *TnHistogram) countAt(v float64) float64 {
598
+ iv := int64(v)
599
+ if iv < 0 || iv > h.highest {
600
+ return 0
601
+ }
602
+ idx := h.countsIndex(iv)
603
+ if idx < 0 || idx >= h.countsLen {
604
+ return 0
605
+ }
606
+ return float64(h.counts[idx])
607
+ }
608
+
609
+ func (h *TnHistogram) cdf(v float64) float64 {
610
+ if h.totalCount == 0 {
611
+ return 0
612
+ }
613
+ iv := int64(v)
614
+ if iv < 0 {
615
+ return 0
616
+ }
617
+ if iv >= h.highest {
618
+ return 1
619
+ }
620
+ targetIdx := h.countsIndex(iv)
621
+ if targetIdx >= h.countsLen {
622
+ return 1
623
+ }
624
+ var sum int64
625
+ for i := 0; i <= targetIdx; i++ {
626
+ sum += h.counts[i]
627
+ }
628
+ return float64(sum) / float64(h.totalCount)
629
+ }
630
+
631
+ func (h *TnHistogram) ccdf(v float64) float64 {
632
+ if h.totalCount == 0 {
633
+ return 0
634
+ }
635
+ return 1.0 - h.cdf(v)
636
+ }
637
+
638
+ func (h *TnHistogram) add(other *TnHistogram) {
639
+ if other == nil {
640
+ return
641
+ }
642
+ if h.lowest == other.lowest && h.highest == other.highest && h.figures == other.figures && h.countsLen == other.countsLen {
643
+ for i, c := range other.counts {
644
+ if c > 0 {
645
+ h.counts[i] += c
646
+ }
647
+ }
648
+ h.totalCount += other.totalCount
649
+ h.exceeds += other.exceeds
650
+ if other.totalCount > 0 {
651
+ if other.minVal < h.minVal {
652
+ h.minVal = other.minVal
653
+ }
654
+ if other.maxVal > h.maxVal {
655
+ h.maxVal = other.maxVal
656
+ }
657
+ }
658
+ return
659
+ }
660
+ for i, c := range other.counts {
661
+ if c > 0 {
662
+ h.recordInt64(other.valueFromIndex(i), c)
663
+ }
664
+ }
665
+ h.exceeds += other.exceeds
666
+ }
667
+
668
+ func (h *TnHistogram) subtract(other *TnHistogram) {
669
+ if other == nil {
670
+ return
671
+ }
672
+ if h.lowest == other.lowest && h.highest == other.highest && h.figures == other.figures && h.countsLen == other.countsLen {
673
+ for i, c := range other.counts {
674
+ if c > 0 {
675
+ if h.counts[i] < c {
676
+ h.counts[i] = 0
677
+ } else {
678
+ h.counts[i] -= c
679
+ }
680
+ }
681
+ }
682
+ } else {
683
+ for i, c := range other.counts {
684
+ if c > 0 {
685
+ v := other.valueFromIndex(i)
686
+ idx := h.countsIndex(v)
687
+ if idx >= 0 && idx < h.countsLen {
688
+ if h.counts[idx] < c {
689
+ h.counts[idx] = 0
690
+ } else {
691
+ h.counts[idx] -= c
692
+ }
693
+ }
694
+ }
695
+ }
696
+ }
697
+ h.recomputeMinMax()
698
+ }
699
+
700
+ func (h *TnHistogram) clone() *TnHistogram {
701
+ cp := &TnHistogram{
702
+ lowest: h.lowest,
703
+ highest: h.highest,
704
+ figures: h.figures,
705
+ unitMagnitude: h.unitMagnitude,
706
+ subBucketHalfCountMagnitude: h.subBucketHalfCountMagnitude,
707
+ subBucketHalfCount: h.subBucketHalfCount,
708
+ subBucketCount: h.subBucketCount,
709
+ subBucketMask: h.subBucketMask,
710
+ bucketCount: h.bucketCount,
711
+ countsLen: h.countsLen,
712
+ counts: make([]int64, h.countsLen),
713
+ totalCount: h.totalCount,
714
+ minVal: h.minVal,
715
+ maxVal: h.maxVal,
716
+ exceeds: h.exceeds,
717
+ resetCount: h.resetCount,
718
+ ewmaEnabled: h.ewmaEnabled,
719
+ ewmaAlpha: h.ewmaAlpha,
720
+ ewmaMean: h.ewmaMean,
721
+ ewmaVar: h.ewmaVar,
722
+ ewmaErrorRate: h.ewmaErrorRate,
723
+ ewmaThreshold: h.ewmaThreshold,
724
+ ewmaHasThreshold: h.ewmaHasThreshold,
725
+ ewmaCount: h.ewmaCount,
726
+ }
727
+ copy(cp.counts, h.counts)
728
+ return cp
729
+ }
730
+
731
+ func (h *TnHistogram) snapshot() *TnHistogram {
732
+ return h.clone()
733
+ }
734
+
735
+ func (h *TnHistogram) diff(other *TnHistogram) *TnHistogram {
736
+ if other == nil {
737
+ panic(TnNodeError("TypeError", "ERR_INVALID_ARG_TYPE", "The \\"other\\" argument must be an instance of Histogram."))
738
+ }
739
+ out := h.clone()
740
+ out.exceeds = 0
741
+ if h.lowest == other.lowest && h.highest == other.highest && h.figures == other.figures && h.countsLen == other.countsLen {
742
+ for i, c := range other.counts {
743
+ if c > 0 {
744
+ if out.counts[i] < c {
745
+ panic(TnNewError("other contains values that are not in the histogram"))
746
+ }
747
+ out.counts[i] -= c
748
+ }
749
+ }
750
+ } else {
751
+ for i, c := range other.counts {
752
+ if c > 0 {
753
+ v := other.valueFromIndex(i)
754
+ idx := out.countsIndex(v)
755
+ if idx < 0 || idx >= out.countsLen || out.counts[idx] < c {
756
+ panic(TnNewError("other contains values that are not in the histogram"))
757
+ }
758
+ out.counts[idx] -= c
759
+ }
760
+ }
761
+ }
762
+ out.recomputeMinMax()
763
+ return out
764
+ }
765
+
766
+ func (h *TnHistogram) toJSON() *TnMap[string, interface{}] {
767
+ m := TnNewMap[string, interface{}]()
768
+ m.Set("count", h.Get_count())
769
+ m.Set("min", h.Get_min())
770
+ m.Set("max", h.Get_max())
771
+ m.Set("mean", h.Get_mean())
772
+ m.Set("exceeds", h.Get_exceeds())
773
+ m.Set("stddev", h.Get_stddev())
774
+ m.Set("skewness", h.Get_skewness())
775
+ m.Set("kurtosis", h.Get_kurtosis())
776
+ m.Set("ewmaMean", h.Get_ewmaMean())
777
+ m.Set("ewmaStddev", h.Get_ewmaStddev())
778
+ m.Set("ewmaErrorRate", h.Get_ewmaErrorRate())
779
+ percentiles := TnNewMap[string, interface{}]()
780
+ pm := h.Get_percentiles()
781
+ for _, k := range pm.Keys() {
782
+ percentiles.Set(TnNumStr(k), pm.Get(k))
783
+ }
784
+ m.Set("percentiles", percentiles)
785
+ return m
786
+ }
787
+
788
+ func (h *TnHistogram) reset() {
789
+ for i := range h.counts {
790
+ h.counts[i] = 0
791
+ }
792
+ h.totalCount = 0
793
+ h.minVal = math.MaxInt64
794
+ h.maxVal = 0
795
+ h.exceeds = 0
796
+ h.ewmaMean = 0
797
+ h.ewmaVar = 0
798
+ h.ewmaErrorRate = 0
799
+ h.ewmaCount = 0
800
+ h.last = time.Time{}
801
+ h.resetCount++
802
+ }
803
+
804
+ // --- Special math functions (incomplete beta, Student-t, Normal quantile) ---
805
+
806
+ func tnBetaCF(a, b, x float64) float64 {
807
+ const maxIter = 200
808
+ const eps = 3e-15
809
+ const fpMin = 1e-300
810
+ qab := a + b
811
+ qap := a + 1.0
812
+ qam := a - 1.0
813
+ c := 1.0
814
+ d := 1.0 - qab*x/qap
815
+ if math.Abs(d) < fpMin {
816
+ d = fpMin
817
+ }
818
+ d = 1.0 / d
819
+ h := d
820
+ for m := 1; m <= maxIter; m++ {
821
+ mf := float64(m)
822
+ m2 := 2.0 * mf
823
+ aa := mf * (b - mf) * x / ((qam + m2) * (a + m2))
824
+ d = 1.0 + aa*d
825
+ if math.Abs(d) < fpMin {
826
+ d = fpMin
827
+ }
828
+ c = 1.0 + aa/c
829
+ if math.Abs(c) < fpMin {
830
+ c = fpMin
831
+ }
832
+ d = 1.0 / d
833
+ h *= d * c
834
+ aa = -(a + mf) * (qab + mf) * x / ((a + m2) * (qap + m2))
835
+ d = 1.0 + aa*d
836
+ if math.Abs(d) < fpMin {
837
+ d = fpMin
838
+ }
839
+ c = 1.0 + aa/c
840
+ if math.Abs(c) < fpMin {
841
+ c = fpMin
842
+ }
843
+ d = 1.0 / d
844
+ del := d * c
845
+ h *= del
846
+ if math.Abs(del-1.0) < eps {
847
+ break
848
+ }
849
+ }
850
+ return h
851
+ }
852
+
853
+ func tnRegIncBeta(x, a, b float64) float64 {
854
+ if x <= 0 {
855
+ return 0
856
+ }
857
+ if x >= 1 {
858
+ return 1
859
+ }
860
+ lgA, _ := math.Lgamma(a)
861
+ lgB, _ := math.Lgamma(b)
862
+ lgAB, _ := math.Lgamma(a + b)
863
+ bt := math.Exp(lgAB - lgA - lgB + a*math.Log(x) + b*math.Log(1.0-x))
864
+ if x < (a+1.0)/(a+b+2.0) {
865
+ return bt * tnBetaCF(a, b, x) / a
866
+ }
867
+ return 1.0 - bt*tnBetaCF(b, a, 1.0-x)/b
868
+ }
869
+
870
+ func tnStudentTPValue(t, df float64) float64 {
871
+ if df <= 0 || math.IsNaN(t) {
872
+ return 1.0
873
+ }
874
+ x := df / (df + t*t)
875
+ p := tnRegIncBeta(x, df/2.0, 0.5)
876
+ if p < 0 {
877
+ return 0
878
+ }
879
+ if p > 1 {
880
+ return 1
881
+ }
882
+ return p
883
+ }
884
+
885
+ // Inverse standard normal CDF (Acklam's rational approximation)
886
+ func tnNormalQuantile(p float64) float64 {
887
+ if p <= 0 {
888
+ return math.Inf(-1)
889
+ }
890
+ if p >= 1 {
891
+ return math.Inf(1)
892
+ }
893
+ const (
894
+ a1 = -3.969683028665376e+01
895
+ a2 = 2.209460984245205e+02
896
+ a3 = -2.759285104469687e+02
897
+ a4 = 1.383577518672690e+02
898
+ a5 = -3.066479806614716e+01
899
+ a6 = 2.506628277459239e+00
900
+
901
+ b1 = -5.447609879822406e+01
902
+ b2 = 1.615858368580409e+02
903
+ b3 = -1.556989798598866e+02
904
+ b4 = 6.680131188771972e+01
905
+ b5 = -1.328068155288572e+01
906
+
907
+ c1 = -7.784894002430293e-03
908
+ c2 = -3.223964580411365e-01
909
+ c3 = -2.400758277161838e+00
910
+ c4 = -2.549732539343734e+00
911
+ c5 = 4.374664141464968e+00
912
+ c6 = 2.938163982698783e+00
913
+
914
+ d1 = 7.784695709041462e-03
915
+ d2 = 3.224671290700398e-01
916
+ d3 = 2.445134137142996e+00
917
+ d4 = 3.754408661907416e+00
918
+
919
+ pLow = 0.02425
920
+ pHigh = 1.0 - pLow
921
+ )
922
+ if p < pLow {
923
+ q := math.Sqrt(-2.0 * math.Log(p))
924
+ return (((((c1*q+c2)*q+c3)*q+c4)*q+c5)*q + c6) / ((((d1*q+d2)*q+d3)*q+d4)*q + 1.0)
925
+ }
926
+ if p <= pHigh {
927
+ q := p - 0.5
928
+ r := q * q
929
+ return (((((a1*r+a2)*r+a3)*r+a4)*r+a5)*r + a6) * q / (((((b1*r+b2)*r+b3)*r+b4)*r+b5)*r + 1.0)
930
+ }
931
+ q := math.Sqrt(-2.0 * math.Log(1.0-p))
932
+ return -(((((c1*q+c2)*q+c3)*q+c4)*q+c5)*q + c6) / ((((d1*q+d2)*q+d3)*q+d4)*q + 1.0)
933
+ }
934
+
935
+ // Positive critical value t > 0 such that P(|T_df| >= t) = alpha (where alpha = 1 - confidence)
936
+ func tnStudentTCrit(confidence, df float64) float64 {
937
+ alpha := 1.0 - confidence
938
+ if df == 1.0 {
939
+ return math.Tan(math.Pi * (0.5 - alpha/2.0))
940
+ }
941
+ if df > 1e7 {
942
+ return tnNormalQuantile(1.0 - alpha/2.0)
943
+ }
944
+ // Bracket and bisect on tnStudentTPValue(t, df) == alpha
945
+ lo := 0.0
946
+ hi := tnNormalQuantile(1.0 - alpha/2.0)
947
+ if hi < 1.0 {
948
+ hi = 1.0
949
+ }
950
+ for tnStudentTPValue(hi, df) > alpha {
951
+ hi *= 2.0
952
+ if hi > 1e12 {
953
+ break
954
+ }
955
+ }
956
+ for i := 0; i < 80; i++ {
957
+ mid := 0.5 * (lo + hi)
958
+ if tnStudentTPValue(mid, df) > alpha {
959
+ lo = mid
960
+ } else {
961
+ hi = mid
962
+ }
963
+ }
964
+ return 0.5 * (lo + hi)
965
+ }
966
+
967
+ func tnConfidenceOpt(options []interface{}) float64 {
968
+ conf := 0.95
969
+ if len(options) > 0 && options[0] != nil {
970
+ if v, ok := TnGetValue(options[0], "confidence").(float64); ok {
971
+ if !(v > 0 && v < 1) || math.IsNaN(v) {
972
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", "The value of \\"options.confidence\\" is out of range. It must be > 0 && < 1."))
973
+ }
974
+ conf = v
975
+ }
976
+ }
977
+ return conf
978
+ }
979
+
980
+ // --- Confidence Intervals ---
981
+
982
+ func (h *TnHistogram) percentileCI(p float64, options ...interface{}) *TnPercentileCI {
983
+ if !(p > 0 && p <= 100) || math.IsNaN(p) {
984
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", "The value of \\"percentile\\" is out of range. It must be > 0 && <= 100."))
985
+ }
986
+ conf := tnConfidenceOpt(options)
987
+ if h.totalCount == 0 {
988
+ return &TnPercentileCI{value: 0, lower: 0, upper: 0}
989
+ }
990
+ val := float64(h.percentileInt64(p))
991
+ if h.totalCount == 1 {
992
+ return &TnPercentileCI{value: val, lower: val, upper: val}
993
+ }
994
+ n := float64(h.totalCount)
995
+ q := p / 100.0
996
+ if q >= 1.0 {
997
+ return &TnPercentileCI{value: val, lower: val, upper: val}
998
+ }
999
+ alpha := 1.0 - conf
1000
+ // Exact binomial order-statistic bounds via regularized incomplete beta:
1001
+ // P(Bin(n, q) <= k-1) = I_{1-q}(n - k + 1, k)
1002
+ // Lower rank k_lo: largest k in [1..n] with P(Bin(n,q) < k) <= alpha/2
1003
+ // Upper rank k_hi: smallest k in [1..n] with P(Bin(n,q) < k) >= 1 - alpha/2
1004
+ // Use binary search over [1..totalCount]
1005
+ z := tnNormalQuantile(1.0 - alpha/2.0)
1006
+ se := math.Sqrt(n * q * (1.0 - q))
1007
+ var loRank, hiRank int64
1008
+ if h.totalCount <= 100000 {
1009
+ lo, hi := int64(1), h.totalCount
1010
+ loRank = 1
1011
+ for lo <= hi {
1012
+ mid := lo + (hi-lo)/2
1013
+ cdf := tnRegIncBeta(1.0-q, float64(h.totalCount-mid+1), float64(mid))
1014
+ if cdf <= alpha/2.0 {
1015
+ loRank = mid
1016
+ lo = mid + 1
1017
+ } else {
1018
+ hi = mid - 1
1019
+ }
1020
+ }
1021
+ lo, hi = int64(1), h.totalCount
1022
+ hiRank = h.totalCount
1023
+ for lo <= hi {
1024
+ mid := lo + (hi-lo)/2
1025
+ cdf := tnRegIncBeta(1.0-q, float64(h.totalCount-mid+1), float64(mid))
1026
+ if cdf >= 1.0-alpha/2.0 {
1027
+ hiRank = mid
1028
+ hi = mid - 1
1029
+ } else {
1030
+ lo = mid + 1
1031
+ }
1032
+ }
1033
+ } else {
1034
+ loRank = int64(math.Floor(n*q - z*se))
1035
+ hiRank = int64(math.Ceil(n*q + z*se + 1.0))
1036
+ }
1037
+ if loRank < 1 {
1038
+ loRank = 1
1039
+ }
1040
+ if hiRank > h.totalCount {
1041
+ hiRank = h.totalCount
1042
+ }
1043
+ if loRank > hiRank {
1044
+ loRank = hiRank
1045
+ }
1046
+ return &TnPercentileCI{
1047
+ value: val,
1048
+ lower: float64(h.valueAtCount(loRank)),
1049
+ upper: float64(h.valueAtCount(hiRank)),
1050
+ }
1051
+ }
1052
+
1053
+ func (h *TnHistogram) sampleVariance(mean float64) float64 {
1054
+ if h.totalCount < 2 {
1055
+ return math.NaN()
1056
+ }
1057
+ sumSq := 0.0
1058
+ for i, c := range h.counts {
1059
+ if c > 0 {
1060
+ d := float64(h.medianEquivalentValue(h.valueFromIndex(i))) - mean
1061
+ sumSq += float64(c) * d * d
1062
+ }
1063
+ }
1064
+ return sumSq / float64(h.totalCount-1)
1065
+ }
1066
+
1067
+ func (h *TnHistogram) meanCI(options ...interface{}) *TnMeanCI {
1068
+ conf := tnConfidenceOpt(options)
1069
+ if h.totalCount == 0 {
1070
+ return &TnMeanCI{mean: math.NaN(), lower: math.NaN(), upper: math.NaN()}
1071
+ }
1072
+ mean := h.Get_mean()
1073
+ if h.totalCount == 1 {
1074
+ return &TnMeanCI{mean: mean, lower: math.NaN(), upper: math.NaN()}
1075
+ }
1076
+ s2 := h.sampleVariance(mean)
1077
+ se := math.Sqrt(s2 / float64(h.totalCount))
1078
+ tCrit := tnStudentTCrit(conf, float64(h.totalCount-1))
1079
+ margin := tCrit * se
1080
+ return &TnMeanCI{
1081
+ mean: mean,
1082
+ lower: mean - margin,
1083
+ upper: mean + margin,
1084
+ }
1085
+ }
1086
+
1087
+ // --- Two-Sample Statistical Tests & Effect Sizes ---
1088
+
1089
+ func (h *TnHistogram) welchTest(other *TnHistogram, options ...interface{}) *TnWelchResult {
1090
+ if other == nil {
1091
+ panic(TnNodeError("TypeError", "ERR_INVALID_ARG_TYPE", "The \\"other\\" argument must be an instance of Histogram."))
1092
+ }
1093
+ conf := tnConfidenceOpt(options)
1094
+ zero := &TnWelchResult{
1095
+ tStatistic: 0,
1096
+ degreesOfFreedom: 0,
1097
+ pValue: 1,
1098
+ confidenceInterval: &TnConfidenceInterval{lower: 0, upper: 0},
1099
+ }
1100
+ if h.totalCount < 2 || other.totalCount < 2 {
1101
+ return zero
1102
+ }
1103
+ n1 := float64(h.totalCount)
1104
+ n2 := float64(other.totalCount)
1105
+ m1 := h.Get_mean()
1106
+ m2 := other.Get_mean()
1107
+ s1Sq := h.sampleVariance(m1)
1108
+ s2Sq := other.sampleVariance(m2)
1109
+ v1 := s1Sq / n1
1110
+ v2 := s2Sq / n2
1111
+ vSum := v1 + v2
1112
+ if vSum == 0 {
1113
+ return zero
1114
+ }
1115
+ se := math.Sqrt(vSum)
1116
+ diff := m1 - m2
1117
+ tStat := diff / se
1118
+ df := (vSum * vSum) / ((v1*v1)/(n1-1.0) + (v2*v2)/(n2-1.0))
1119
+ pVal := tnStudentTPValue(tStat, df)
1120
+ tCrit := tnStudentTCrit(conf, df)
1121
+ margin := tCrit * se
1122
+ return &TnWelchResult{
1123
+ tStatistic: tStat,
1124
+ degreesOfFreedom: df,
1125
+ pValue: pVal,
1126
+ confidenceInterval: &TnConfidenceInterval{lower: diff - margin, upper: diff + margin},
1127
+ }
1128
+ }
1129
+
1130
+ type tnHistBucket struct {
1131
+ val int64
1132
+ count int64
1133
+ }
1134
+
1135
+ func (h *TnHistogram) nonZeroBuckets() []tnHistBucket {
1136
+ var out []tnHistBucket
1137
+ for i, c := range h.counts {
1138
+ if c > 0 {
1139
+ out = append(out, tnHistBucket{val: h.medianEquivalentValue(h.valueFromIndex(i)), count: c})
1140
+ }
1141
+ }
1142
+ return out
1143
+ }
1144
+
1145
+ func (h *TnHistogram) ksTest(other *TnHistogram) float64 {
1146
+ if other == nil {
1147
+ panic(TnNodeError("TypeError", "ERR_INVALID_ARG_TYPE", "The \\"other\\" argument must be an instance of Histogram."))
1148
+ }
1149
+ if h.totalCount == 0 || other.totalCount == 0 {
1150
+ return 0
1151
+ }
1152
+ b1 := h.nonZeroBuckets()
1153
+ b2 := other.nonZeroBuckets()
1154
+ n1 := float64(h.totalCount)
1155
+ n2 := float64(other.totalCount)
1156
+ i, j := 0, 0
1157
+ var c1, c2 int64
1158
+ maxDiff := 0.0
1159
+ for i < len(b1) || j < len(b2) {
1160
+ var curVal int64
1161
+ if i < len(b1) && (j >= len(b2) || b1[i].val <= b2[j].val) {
1162
+ curVal = b1[i].val
1163
+ } else {
1164
+ curVal = b2[j].val
1165
+ }
1166
+ for i < len(b1) && b1[i].val == curVal {
1167
+ c1 += b1[i].count
1168
+ i++
1169
+ }
1170
+ for j < len(b2) && b2[j].val == curVal {
1171
+ c2 += b2[j].count
1172
+ j++
1173
+ }
1174
+ d := math.Abs(float64(c1)/n1 - float64(c2)/n2)
1175
+ if d > maxDiff {
1176
+ maxDiff = d
1177
+ }
1178
+ }
1179
+ return maxDiff
1180
+ }
1181
+
1182
+ func (h *TnHistogram) mannWhitneyTest(other *TnHistogram) *TnMannWhitneyResult {
1183
+ if other == nil {
1184
+ panic(TnNodeError("TypeError", "ERR_INVALID_ARG_TYPE", "The \\"other\\" argument must be an instance of Histogram."))
1185
+ }
1186
+ if h.totalCount == 0 || other.totalCount == 0 {
1187
+ return &TnMannWhitneyResult{uStatistic: 0, zScore: 0, pValue: 1}
1188
+ }
1189
+ b1 := h.nonZeroBuckets()
1190
+ b2 := other.nonZeroBuckets()
1191
+ n1 := float64(h.totalCount)
1192
+ n2 := float64(other.totalCount)
1193
+ N := n1 + n2
1194
+
1195
+ // Merge sorted buckets to compute U1 = sum(a_i > b_j) + 0.5*sum(a_i == b_j) and tie sum
1196
+ i, j := 0, 0
1197
+ var b2Less int64
1198
+ u1 := 0.0
1199
+ tieSum := 0.0
1200
+ for i < len(b1) || j < len(b2) {
1201
+ var curVal int64
1202
+ if i < len(b1) && (j >= len(b2) || b1[i].val <= b2[j].val) {
1203
+ curVal = b1[i].val
1204
+ } else {
1205
+ curVal = b2[j].val
1206
+ }
1207
+ var cnt1, cnt2 int64
1208
+ for i < len(b1) && b1[i].val == curVal {
1209
+ cnt1 += b1[i].count
1210
+ i++
1211
+ }
1212
+ for j < len(b2) && b2[j].val == curVal {
1213
+ cnt2 += b2[j].count
1214
+ j++
1215
+ }
1216
+ if cnt1 > 0 {
1217
+ u1 += float64(cnt1) * (float64(b2Less) + 0.5*float64(cnt2))
1218
+ }
1219
+ b2Less += cnt2
1220
+ t := float64(cnt1 + cnt2)
1221
+ if t > 1 {
1222
+ tieSum += t*t*t - t
1223
+ }
1224
+ }
1225
+ mu := n1 * n2 / 2.0
1226
+ var term float64
1227
+ if N > 1 {
1228
+ term = (N + 1.0) - tieSum/(N*(N-1.0))
1229
+ }
1230
+ variance := (n1 * n2 / 12.0) * term
1231
+ if variance <= 0 {
1232
+ return &TnMannWhitneyResult{uStatistic: u1, zScore: 0, pValue: 1}
1233
+ }
1234
+ z := (u1 - mu) / math.Sqrt(variance)
1235
+ pVal := math.Erfc(math.Abs(z) / math.Sqrt2)
1236
+ return &TnMannWhitneyResult{
1237
+ uStatistic: u1,
1238
+ zScore: z,
1239
+ pValue: pVal,
1240
+ }
1241
+ }
1242
+
1243
+ func (h *TnHistogram) cohensD(other *TnHistogram) float64 {
1244
+ if other == nil {
1245
+ panic(TnNodeError("TypeError", "ERR_INVALID_ARG_TYPE", "The \\"other\\" argument must be an instance of Histogram."))
1246
+ }
1247
+ if h.totalCount < 2 || other.totalCount < 2 {
1248
+ return 0
1249
+ }
1250
+ n1 := float64(h.totalCount)
1251
+ n2 := float64(other.totalCount)
1252
+ m1 := h.Get_mean()
1253
+ m2 := other.Get_mean()
1254
+ s1Sq := h.sampleVariance(m1)
1255
+ s2Sq := other.sampleVariance(m2)
1256
+ pooledVar := ((n1-1.0)*s1Sq + (n2-1.0)*s2Sq) / (n1 + n2 - 2.0)
1257
+ if pooledVar <= 0 {
1258
+ return 0
1259
+ }
1260
+ return (m1 - m2) / math.Sqrt(pooledVar)
1261
+ }
1262
+
1263
+ func (h *TnHistogram) cliffsD(other *TnHistogram) float64 {
1264
+ if other == nil {
1265
+ panic(TnNodeError("TypeError", "ERR_INVALID_ARG_TYPE", "The \\"other\\" argument must be an instance of Histogram."))
1266
+ }
1267
+ if h.totalCount == 0 || other.totalCount == 0 {
1268
+ return 0
1269
+ }
1270
+ b1 := h.nonZeroBuckets()
1271
+ b2 := other.nonZeroBuckets()
1272
+ i, j := 0, 0
1273
+ var b2Less int64
1274
+ greater := 0.0
1275
+ less := 0.0
1276
+ for i < len(b1) || j < len(b2) {
1277
+ var curVal int64
1278
+ if i < len(b1) && (j >= len(b2) || b1[i].val <= b2[j].val) {
1279
+ curVal = b1[i].val
1280
+ } else {
1281
+ curVal = b2[j].val
1282
+ }
1283
+ var cnt1, cnt2 int64
1284
+ for i < len(b1) && b1[i].val == curVal {
1285
+ cnt1 += b1[i].count
1286
+ i++
1287
+ }
1288
+ for j < len(b2) && b2[j].val == curVal {
1289
+ cnt2 += b2[j].count
1290
+ j++
1291
+ }
1292
+ if cnt1 > 0 {
1293
+ greater += float64(cnt1) * float64(b2Less)
1294
+ b2Greater := other.totalCount - b2Less - cnt2
1295
+ less += float64(cnt1) * float64(b2Greater)
1296
+ }
1297
+ b2Less += cnt2
1298
+ }
1299
+ return (greater - less) / (float64(h.totalCount) * float64(other.totalCount))
1300
+ }
1301
+
1302
+ // --- Buckets & QRDE ---
1303
+
1304
+ func (h *TnHistogram) linearBuckets(stepSize float64) *TnMap[float64, float64] {
1305
+ if !(stepSize >= 1 && stepSize <= 9007199254740991) || math.IsNaN(stepSize) {
1306
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", "The value of \\"stepSize\\" is out of range. It must be >= 1 && <= 9007199254740991."))
1307
+ }
1308
+ m := TnNewMap[float64, float64]()
1309
+ if h.totalCount == 0 {
1310
+ return m
1311
+ }
1312
+ maxV := float64(h.maxInt64())
1313
+ // Pre-populate buckets up to ceil(maxV / stepSize) * stepSize
1314
+ numBuckets := int(math.Ceil(maxV / stepSize))
1315
+ if numBuckets < 1 {
1316
+ numBuckets = 1
1317
+ }
1318
+ counts := make([]int64, numBuckets)
1319
+ for i, c := range h.counts {
1320
+ if c > 0 {
1321
+ v := float64(h.medianEquivalentValue(h.valueFromIndex(i)))
1322
+ bi := int(math.Ceil(v/stepSize)) - 1
1323
+ if bi < 0 {
1324
+ bi = 0
1325
+ }
1326
+ if bi >= numBuckets {
1327
+ bi = numBuckets - 1
1328
+ }
1329
+ counts[bi] += c
1330
+ }
1331
+ }
1332
+ for i, c := range counts {
1333
+ m.Set(float64(i+1)*stepSize, float64(c))
1334
+ }
1335
+ return m
1336
+ }
1337
+
1338
+ func (h *TnHistogram) logBuckets(start float64, factor float64) *TnMap[float64, float64] {
1339
+ if !(start >= 1) || math.IsNaN(start) || math.IsInf(start, 0) {
1340
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", "The value of \\"start\\" is out of range. It must be >= 1."))
1341
+ }
1342
+ if !(factor > 1) || math.IsNaN(factor) || math.IsInf(factor, 0) {
1343
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", "The value of \\"factor\\" is out of range. It must be > 1."))
1344
+ }
1345
+ m := TnNewMap[float64, float64]()
1346
+ if h.totalCount == 0 {
1347
+ return m
1348
+ }
1349
+ maxV := float64(h.maxInt64())
1350
+ var edges []float64
1351
+ edge := start
1352
+ for {
1353
+ edges = append(edges, edge)
1354
+ if edge >= maxV {
1355
+ break
1356
+ }
1357
+ edge *= factor
1358
+ if math.IsInf(edge, 0) || len(edges) > 10000 {
1359
+ break
1360
+ }
1361
+ }
1362
+ counts := make([]int64, len(edges))
1363
+ for i, c := range h.counts {
1364
+ if c > 0 {
1365
+ v := float64(h.medianEquivalentValue(h.valueFromIndex(i)))
1366
+ idx := sort.SearchFloat64s(edges, v)
1367
+ if idx >= len(edges) {
1368
+ idx = len(edges) - 1
1369
+ }
1370
+ counts[idx] += c
1371
+ }
1372
+ }
1373
+ for i, e := range edges {
1374
+ m.Set(e, float64(counts[i]))
1375
+ }
1376
+ return m
1377
+ }
1378
+
1379
+ func (h *TnHistogram) qrde(options ...interface{}) chan *TnQrdeResult {
1380
+ bins := 100
1381
+ dequantize := "hdr"
1382
+ if len(options) > 0 && options[0] != nil {
1383
+ opt := options[0]
1384
+ if v, ok := TnGetValue(opt, "bins").(float64); ok {
1385
+ if v < 1 || v != math.Trunc(v) {
1386
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", "The value of \\"options.bins\\" is out of range."))
1387
+ }
1388
+ bins = int(v)
1389
+ }
1390
+ if s, ok := TnGetValue(opt, "dequantize").(string); ok {
1391
+ if s != "hdr" && s != "none" {
1392
+ panic(TnNodeError("TypeError", "ERR_INVALID_ARG_VALUE", "The value of \\"options.dequantize\\" must be 'hdr' or 'none'."))
1393
+ }
1394
+ dequantize = s
1395
+ }
1396
+ }
1397
+
1398
+ probs := make([]float64, bins+1)
1399
+ for i := 0; i <= bins; i++ {
1400
+ probs[i] = float64(i) / float64(bins)
1401
+ }
1402
+
1403
+ if h.totalCount == 0 {
1404
+ res := &TnQrdeResult{
1405
+ probabilities: &TnFloat64Array{data: probs, kind: "Float64Array"},
1406
+ quantiles: &TnFloat64Array{data: []float64{}, kind: "Float64Array"},
1407
+ densities: &TnFloat64Array{data: []float64{}, kind: "Float64Array"},
1408
+ count: tnbig.NewInt(0),
1409
+ bucketCount: 0,
1410
+ corrections: 0,
1411
+ dequantize: dequantize,
1412
+ }
1413
+ return TnPromiseResolve(res)
1414
+ }
1415
+
1416
+ type qBucket struct {
1417
+ lo float64
1418
+ width float64
1419
+ count int64
1420
+ }
1421
+ var buckets []qBucket
1422
+ for i, c := range h.counts {
1423
+ if c > 0 {
1424
+ v := h.valueFromIndex(i)
1425
+ lo := float64(h.lowestEquivalentValue(v))
1426
+ w := float64(h.sizeOfEquivalentValueRange(v))
1427
+ buckets = append(buckets, qBucket{lo: lo, width: w, count: c})
1428
+ }
1429
+ }
1430
+
1431
+ n := float64(h.totalCount)
1432
+ quantiles := make([]float64, bins+1)
1433
+ quantiles[0] = buckets[0].lo
1434
+ quantiles[bins] = buckets[len(buckets)-1].lo
1435
+
1436
+ for bi := 1; bi < bins; bi++ {
1437
+ p := probs[bi]
1438
+ a := (n + 1.0) * p
1439
+ b := (n + 1.0) * (1.0 - p)
1440
+ qVal := 0.0
1441
+ var cum int64
1442
+ prevCDF := 0.0
1443
+ for _, bk := range buckets {
1444
+ if dequantize == "hdr" && bk.width > 1.0 {
1445
+ // Spread samples uniformly across [lo, lo + width)
1446
+ for k := int64(0); k < bk.count; k++ {
1447
+ cum++
1448
+ curCDF := tnRegIncBeta(float64(cum)/n, a, b)
1449
+ w := curCDF - prevCDF
1450
+ prevCDF = curCDF
1451
+ sampleVal := bk.lo + bk.width*(float64(k)+0.5)/float64(bk.count)
1452
+ qVal += w * sampleVal
1453
+ }
1454
+ } else {
1455
+ cum += bk.count
1456
+ curCDF := tnRegIncBeta(float64(cum)/n, a, b)
1457
+ w := curCDF - prevCDF
1458
+ prevCDF = curCDF
1459
+ qVal += w * bk.lo
1460
+ }
1461
+ }
1462
+ quantiles[bi] = qVal
1463
+ }
1464
+
1465
+ // Enforce monotonicity (isotonic correction)
1466
+ corrections := 0
1467
+ for i := 1; i <= bins; i++ {
1468
+ if quantiles[i] < quantiles[i-1] {
1469
+ quantiles[i] = quantiles[i-1]
1470
+ corrections++
1471
+ }
1472
+ }
1473
+
1474
+ densities := make([]float64, bins)
1475
+ for i := 0; i < bins; i++ {
1476
+ dq := quantiles[i+1] - quantiles[i]
1477
+ if dq > 0 {
1478
+ densities[i] = (probs[i+1] - probs[i]) / dq
1479
+ }
1480
+ }
1481
+
1482
+ res := &TnQrdeResult{
1483
+ probabilities: &TnFloat64Array{data: probs, kind: "Float64Array"},
1484
+ quantiles: &TnFloat64Array{data: quantiles, kind: "Float64Array"},
1485
+ densities: &TnFloat64Array{data: densities, kind: "Float64Array"},
1486
+ count: tnbig.NewInt(h.totalCount),
1487
+ bucketCount: float64(len(buckets)),
1488
+ corrections: float64(corrections),
1489
+ dequantize: dequantize,
1490
+ }
1491
+ return TnPromiseResolve(res)
1492
+ }
1493
+
1494
+ // --- CBOR v2 Export / Import ---
1495
+
1496
+ func tnCborWriteHead(dst []byte, major byte, val uint64) []byte {
1497
+ m := major << 5
1498
+ switch {
1499
+ case val < 24:
1500
+ return append(dst, m|byte(val))
1501
+ case val <= 0xff:
1502
+ return append(dst, m|24, byte(val))
1503
+ case val <= 0xffff:
1504
+ var b [2]byte
1505
+ binary.BigEndian.PutUint16(b[:], uint16(val))
1506
+ return append(dst, m|25, b[0], b[1])
1507
+ case val <= 0xffffffff:
1508
+ var b [4]byte
1509
+ binary.BigEndian.PutUint32(b[:], uint32(val))
1510
+ return append(dst, m|26, b[0], b[1], b[2], b[3])
1511
+ default:
1512
+ var b [8]byte
1513
+ binary.BigEndian.PutUint64(b[:], val)
1514
+ return append(dst, m|27, b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7])
1515
+ }
1516
+ }
1517
+
1518
+ func tnCborWriteFloat64(dst []byte, f float64) []byte {
1519
+ var b [8]byte
1520
+ binary.BigEndian.PutUint64(b[:], math.Float64bits(f))
1521
+ return append(dst, (7<<5)|27, b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7])
1522
+ }
1523
+
1524
+ func (h *TnHistogram) export() []byte {
1525
+ var buf []byte
1526
+ mapLen := uint64(11)
1527
+ if h.ewmaEnabled {
1528
+ mapLen = 12
1529
+ }
1530
+ buf = tnCborWriteHead(buf, 5, mapLen)
1531
+ // 0: version = 2
1532
+ buf = tnCborWriteHead(buf, 0, 0)
1533
+ buf = tnCborWriteHead(buf, 0, 2)
1534
+ // 1: lowest
1535
+ buf = tnCborWriteHead(buf, 0, 1)
1536
+ buf = tnCborWriteHead(buf, 0, uint64(h.lowest))
1537
+ // 2: highest
1538
+ buf = tnCborWriteHead(buf, 0, 2)
1539
+ buf = tnCborWriteHead(buf, 0, uint64(h.highest))
1540
+ // 3: figures
1541
+ buf = tnCborWriteHead(buf, 0, 3)
1542
+ buf = tnCborWriteHead(buf, 0, uint64(h.figures))
1543
+ // 4: totalCount
1544
+ buf = tnCborWriteHead(buf, 0, 4)
1545
+ buf = tnCborWriteHead(buf, 0, uint64(h.totalCount))
1546
+ // 5: minVal
1547
+ buf = tnCborWriteHead(buf, 0, 5)
1548
+ buf = tnCborWriteHead(buf, 0, uint64(h.minInt64()))
1549
+ // 6: maxVal
1550
+ buf = tnCborWriteHead(buf, 0, 6)
1551
+ buf = tnCborWriteHead(buf, 0, uint64(h.maxInt64()))
1552
+ // 7: exceeds
1553
+ buf = tnCborWriteHead(buf, 0, 7)
1554
+ buf = tnCborWriteHead(buf, 0, uint64(h.exceeds))
1555
+ // 8: conversion_ratio = 1.0
1556
+ buf = tnCborWriteHead(buf, 0, 8)
1557
+ buf = tnCborWriteFloat64(buf, 1.0)
1558
+ // 9: counts_len
1559
+ buf = tnCborWriteHead(buf, 0, 9)
1560
+ buf = tnCborWriteHead(buf, 0, uint64(h.countsLen))
1561
+ // 10: delta-encoded [indexDelta, count, ...]
1562
+ var pairs []uint64
1563
+ prevIdx := 0
1564
+ for i, c := range h.counts {
1565
+ if c > 0 {
1566
+ pairs = append(pairs, uint64(i-prevIdx), uint64(c))
1567
+ prevIdx = i
1568
+ }
1569
+ }
1570
+ buf = tnCborWriteHead(buf, 0, 10)
1571
+ buf = tnCborWriteHead(buf, 4, uint64(len(pairs)))
1572
+ for _, v := range pairs {
1573
+ buf = tnCborWriteHead(buf, 0, v)
1574
+ }
1575
+ // 11: optional EWMA map
1576
+ if h.ewmaEnabled {
1577
+ buf = tnCborWriteHead(buf, 0, 11)
1578
+ ewmaLen := uint64(4)
1579
+ if h.ewmaHasThreshold {
1580
+ ewmaLen = 5
1581
+ }
1582
+ buf = tnCborWriteHead(buf, 5, ewmaLen)
1583
+ buf = tnCborWriteHead(buf, 0, 0)
1584
+ buf = tnCborWriteFloat64(buf, h.ewmaAlpha)
1585
+ buf = tnCborWriteHead(buf, 0, 1)
1586
+ buf = tnCborWriteFloat64(buf, h.ewmaMean)
1587
+ buf = tnCborWriteHead(buf, 0, 2)
1588
+ buf = tnCborWriteFloat64(buf, h.ewmaVar)
1589
+ buf = tnCborWriteHead(buf, 0, 3)
1590
+ buf = tnCborWriteFloat64(buf, h.ewmaErrorRate)
1591
+ if h.ewmaHasThreshold {
1592
+ buf = tnCborWriteHead(buf, 0, 4)
1593
+ if h.ewmaThreshold >= 0 && h.ewmaThreshold == math.Trunc(h.ewmaThreshold) {
1594
+ buf = tnCborWriteHead(buf, 0, uint64(h.ewmaThreshold))
1595
+ } else {
1596
+ buf = tnCborWriteFloat64(buf, h.ewmaThreshold)
1597
+ }
1598
+ }
1599
+ }
1600
+ return buf
1601
+ }
1602
+
1603
+ type tnCborReader struct {
1604
+ data []byte
1605
+ pos int
1606
+ }
1607
+
1608
+ func (r *tnCborReader) readHead() (byte, uint64, bool) {
1609
+ if r.pos >= len(r.data) {
1610
+ return 0, 0, false
1611
+ }
1612
+ b := r.data[r.pos]
1613
+ r.pos++
1614
+ major := b >> 5
1615
+ ai := b & 31
1616
+ switch {
1617
+ case ai < 24:
1618
+ return major, uint64(ai), true
1619
+ case ai == 24:
1620
+ if r.pos+1 > len(r.data) {
1621
+ return 0, 0, false
1622
+ }
1623
+ v := uint64(r.data[r.pos])
1624
+ r.pos++
1625
+ return major, v, true
1626
+ case ai == 25:
1627
+ if r.pos+2 > len(r.data) {
1628
+ return 0, 0, false
1629
+ }
1630
+ v := uint64(binary.BigEndian.Uint16(r.data[r.pos:]))
1631
+ r.pos += 2
1632
+ return major, v, true
1633
+ case ai == 26:
1634
+ if r.pos+4 > len(r.data) {
1635
+ return 0, 0, false
1636
+ }
1637
+ v := uint64(binary.BigEndian.Uint32(r.data[r.pos:]))
1638
+ r.pos += 4
1639
+ return major, v, true
1640
+ case ai == 27:
1641
+ if r.pos+8 > len(r.data) {
1642
+ return 0, 0, false
1643
+ }
1644
+ v := binary.BigEndian.Uint64(r.data[r.pos:])
1645
+ r.pos += 8
1646
+ return major, v, true
1647
+ default:
1648
+ return 0, 0, false
1649
+ }
1650
+ }
1651
+
1652
+ func (r *tnCborReader) readNum() (float64, bool) {
1653
+ major, val, ok := r.readHead()
1654
+ if !ok {
1655
+ return 0, false
1656
+ }
1657
+ switch major {
1658
+ case 0:
1659
+ return float64(val), true
1660
+ case 1:
1661
+ return -1.0 - float64(val), true
1662
+ case 7:
1663
+ return math.Float64frombits(val), true
1664
+ }
1665
+ return 0, false
1666
+ }
1667
+
1668
+ func TnImportHistogram(data []byte) *TnHistogram {
1669
+ if len(data) == 0 {
1670
+ panic(TnNewError("Invalid histogram data"))
1671
+ }
1672
+ r := &tnCborReader{data: data}
1673
+ major, mapLen, ok := r.readHead()
1674
+ if !ok || major != 5 {
1675
+ panic(TnNewError("Invalid histogram data"))
1676
+ }
1677
+ var version, lowest, highest, figures, exceeds uint64
1678
+ var pairs []uint64
1679
+ var hasEwma, hasThreshold bool
1680
+ var ewmaAlpha, ewmaMean, ewmaVar, ewmaErr, ewmaThresh float64
1681
+
1682
+ for i := uint64(0); i < mapLen; i++ {
1683
+ km, key, ok := r.readHead()
1684
+ if !ok || km != 0 {
1685
+ panic(TnNewError("Invalid histogram data"))
1686
+ }
1687
+ switch key {
1688
+ case 0:
1689
+ _, version, ok = r.readHead()
1690
+ case 1:
1691
+ _, lowest, ok = r.readHead()
1692
+ case 2:
1693
+ _, highest, ok = r.readHead()
1694
+ case 3:
1695
+ _, figures, ok = r.readHead()
1696
+ case 4, 5, 6, 9:
1697
+ _, _, ok = r.readHead()
1698
+ case 7:
1699
+ _, exceeds, ok = r.readHead()
1700
+ case 8:
1701
+ _, ok = r.readNum()
1702
+ case 10:
1703
+ am, arrLen, aok := r.readHead()
1704
+ if !aok || am != 4 || arrLen%2 != 0 {
1705
+ panic(TnNewError("Invalid histogram data"))
1706
+ }
1707
+ pairs = make([]uint64, arrLen)
1708
+ for j := uint64(0); j < arrLen; j++ {
1709
+ em, ev, eok := r.readHead()
1710
+ if !eok || em != 0 {
1711
+ panic(TnNewError("Invalid histogram data"))
1712
+ }
1713
+ pairs[j] = ev
1714
+ }
1715
+ ok = true
1716
+ case 11:
1717
+ em, eLen, eok := r.readHead()
1718
+ if !eok || em != 5 {
1719
+ panic(TnNewError("Invalid histogram data"))
1720
+ }
1721
+ hasEwma = true
1722
+ for j := uint64(0); j < eLen; j++ {
1723
+ ekm, ek, ekok := r.readHead()
1724
+ if !ekok || ekm != 0 {
1725
+ panic(TnNewError("Invalid histogram data"))
1726
+ }
1727
+ val, vok := r.readNum()
1728
+ if !vok {
1729
+ panic(TnNewError("Invalid histogram data"))
1730
+ }
1731
+ switch ek {
1732
+ case 0:
1733
+ ewmaAlpha = val
1734
+ case 1:
1735
+ ewmaMean = val
1736
+ case 2:
1737
+ ewmaVar = val
1738
+ case 3:
1739
+ ewmaErr = val
1740
+ case 4:
1741
+ ewmaThresh = val
1742
+ hasThreshold = true
1743
+ }
1744
+ }
1745
+ ok = true
1746
+ default:
1747
+ _, _, ok = r.readHead()
1748
+ }
1749
+ if !ok {
1750
+ panic(TnNewError("Invalid histogram data"))
1751
+ }
1752
+ }
1753
+ if version != 2 || lowest < 1 || figures < 1 || figures > 5 {
1754
+ panic(TnNewError("Invalid histogram data"))
1755
+ }
1756
+ h := tnHdrInit(int64(lowest), int64(highest), int(figures))
1757
+ h.exceeds = int64(exceeds)
1758
+ idx := 0
1759
+ for j := 0; j < len(pairs); j += 2 {
1760
+ idx += int(pairs[j])
1761
+ if idx < 0 || idx >= h.countsLen {
1762
+ panic(TnNewError("Invalid histogram data"))
1763
+ }
1764
+ h.counts[idx] += int64(pairs[j+1])
1765
+ }
1766
+ h.recomputeMinMax()
1767
+ if hasEwma {
1768
+ h.ewmaEnabled = true
1769
+ h.ewmaAlpha = ewmaAlpha
1770
+ h.ewmaMean = ewmaMean
1771
+ h.ewmaVar = ewmaVar
1772
+ h.ewmaErrorRate = ewmaErr
1773
+ h.ewmaHasThreshold = hasThreshold
1774
+ h.ewmaThreshold = ewmaThresh
1775
+ h.ewmaCount = h.totalCount
1776
+ }
1777
+ return h
1778
+ }
1779
+
1780
+ // --- SlidingWindowHistogram ---
1781
+
1782
+ type TnSlidingWindowHistogram struct {
1783
+ chunks []*TnHistogram
1784
+ chunkCount int
1785
+ curIdx int
1786
+ chunkDuration time.Duration
1787
+ recordsPerChunk int64
1788
+ curRecords int64
1789
+ lastRotate time.Time
1790
+ lowest int64
1791
+ highest int64
1792
+ figures int
1793
+ }
1794
+
1795
+ func TnCreateSlidingWindowHistogram(options interface{}) *TnSlidingWindowHistogram {
1796
+ if options == nil {
1797
+ panic(TnNodeError("TypeError", "ERR_INVALID_ARG_TYPE", "The \\"options\\" argument must be of type object."))
1798
+ }
1799
+ chunksVal, ok := TnGetValue(options, "chunks").(float64)
1800
+ if !ok {
1801
+ panic(TnNodeError("TypeError", "ERR_INVALID_ARG_TYPE", "The \\"options.chunks\\" property must be of type number."))
1802
+ }
1803
+ if chunksVal < 1 || chunksVal != math.Trunc(chunksVal) {
1804
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", "The value of \\"options.chunks\\" is out of range."))
1805
+ }
1806
+ chunkCount := int(chunksVal)
1807
+
1808
+ durVal, hasDur := TnGetValue(options, "chunkDuration").(float64)
1809
+ rpcVal, hasRPC := TnGetValue(options, "recordsPerChunk").(float64)
1810
+ if hasDur == hasRPC {
1811
+ panic(TnNodeError("TypeError", "ERR_INVALID_ARG_VALUE", "The argument 'options' must specify exactly one of \\"chunkDuration\\" or \\"recordsPerChunk\\"."))
1812
+ }
1813
+ var chunkDur time.Duration
1814
+ var rpc int64
1815
+ if hasDur {
1816
+ if !(durVal > 0) || math.IsInf(durVal, 0) {
1817
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", "The value of \\"options.chunkDuration\\" is out of range."))
1818
+ }
1819
+ chunkDur = time.Duration(durVal * float64(time.Millisecond))
1820
+ }
1821
+ if hasRPC {
1822
+ if rpcVal < 1 || rpcVal != math.Trunc(rpcVal) {
1823
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", "The value of \\"options.recordsPerChunk\\" is out of range."))
1824
+ }
1825
+ rpc = int64(rpcVal)
1826
+ }
1827
+
1828
+ lowest := int64(1)
1829
+ highest := int64(9007199254740991)
1830
+ figures := 3
1831
+ if v, ok := tnToInt64Val(TnGetValue(options, "lowest")); ok && v >= 1 {
1832
+ lowest = v
1833
+ }
1834
+ if v, ok := tnToInt64Val(TnGetValue(options, "highest")); ok && v >= lowest*2 {
1835
+ highest = v
1836
+ }
1837
+ if v, ok := TnGetValue(options, "figures").(float64); ok && v >= 1 && v <= 5 {
1838
+ figures = int(v)
1839
+ }
1840
+
1841
+ chunks := make([]*TnHistogram, chunkCount)
1842
+ for i := 0; i < chunkCount; i++ {
1843
+ chunks[i] = tnHdrInit(lowest, highest, figures)
1844
+ }
1845
+ return &TnSlidingWindowHistogram{
1846
+ chunks: chunks,
1847
+ chunkCount: chunkCount,
1848
+ curIdx: 0,
1849
+ chunkDuration: chunkDur,
1850
+ recordsPerChunk: rpc,
1851
+ curRecords: 0,
1852
+ lastRotate: time.Now(),
1853
+ lowest: lowest,
1854
+ highest: highest,
1855
+ figures: figures,
1856
+ }
1857
+ }
1858
+
1859
+ func (sw *TnSlidingWindowHistogram) rotateByTime() {
1860
+ if sw.chunkDuration <= 0 {
1861
+ return
1862
+ }
1863
+ now := time.Now()
1864
+ elapsed := int(now.Sub(sw.lastRotate) / sw.chunkDuration)
1865
+ if elapsed <= 0 {
1866
+ return
1867
+ }
1868
+ if elapsed >= sw.chunkCount {
1869
+ for _, c := range sw.chunks {
1870
+ c.reset()
1871
+ }
1872
+ sw.curIdx = 0
1873
+ sw.lastRotate = now
1874
+ return
1875
+ }
1876
+ for i := 0; i < elapsed; i++ {
1877
+ sw.curIdx = (sw.curIdx + 1) % sw.chunkCount
1878
+ sw.chunks[sw.curIdx].reset()
1879
+ }
1880
+ sw.lastRotate = sw.lastRotate.Add(time.Duration(elapsed) * sw.chunkDuration)
1881
+ }
1882
+
1883
+ func (sw *TnSlidingWindowHistogram) record(value interface{}) {
1884
+ if sw.chunkDuration > 0 {
1885
+ sw.rotateByTime()
1886
+ } else if sw.recordsPerChunk > 0 && sw.curRecords >= sw.recordsPerChunk {
1887
+ sw.curIdx = (sw.curIdx + 1) % sw.chunkCount
1888
+ sw.chunks[sw.curIdx].reset()
1889
+ sw.curRecords = 0
1890
+ }
1891
+ sw.chunks[sw.curIdx].record(value)
1892
+ sw.curRecords++
1893
+ }
1894
+
1895
+ func (sw *TnSlidingWindowHistogram) snapshot() *TnHistogram {
1896
+ if sw.chunkDuration > 0 {
1897
+ sw.rotateByTime()
1898
+ }
1899
+ out := tnHdrInit(sw.lowest, sw.highest, sw.figures)
1900
+ for _, c := range sw.chunks {
1901
+ out.add(c)
1902
+ }
1903
+ return out
1904
+ }
1905
+
1906
+ func (sw *TnSlidingWindowHistogram) reset() {
1907
+ for _, c := range sw.chunks {
1908
+ c.reset()
1909
+ }
1910
+ sw.curIdx = 0
1911
+ sw.curRecords = 0
1912
+ sw.lastRotate = time.Now()
1913
+ }
1914
+
1915
+ // monitorEventLoopDelay({ resolution }): samples how late a timer fires on the loop
1916
+ func TnMonitorEventLoopDelay(options ...interface{}) *TnHistogram {
1917
+ h := tnHdrInit(1, 9007199254740991, 3)
1918
+ h.interval = 10
1919
+ if len(options) > 0 && options[0] != nil {
1920
+ if n, ok := TnGetValue(options[0], "resolution").(float64); ok && n > 0 {
1921
+ h.interval = n
1922
+ }
1923
+ }
1924
+ return h
1925
+ }
1926
+
1927
+ func (h *TnHistogram) enable() bool {
1928
+ if h.enabled {
1929
+ return false
1930
+ }
1931
+ h.enabled = true
1932
+ h.stop = make(chan struct{})
1933
+ stop := h.stop
1934
+ period := time.Duration(h.interval * float64(time.Millisecond))
1935
+ go func() {
1936
+ ticker := time.NewTicker(period)
1937
+ defer ticker.Stop()
1938
+ for {
1939
+ select {
1940
+ case <-stop:
1941
+ return
1942
+ case expected := <-ticker.C:
1943
+ TnLoopPost(func() {
1944
+ if h.enabled {
1945
+ ns := time.Since(expected).Nanoseconds()
1946
+ if ns < 1 {
1947
+ ns = 1
1948
+ }
1949
+ h.recordInt64(ns, 1)
1950
+ }
1951
+ })
1952
+ }
1953
+ }
1954
+ }()
1955
+ return true
1956
+ }
1957
+
1958
+ func (h *TnHistogram) disable() bool {
1959
+ if !h.enabled {
1960
+ return false
1961
+ }
1962
+ h.enabled = false
1963
+ close(h.stop)
1964
+ return true
1965
+ }`;
1966
+ export function registerHistogram(r) {
1967
+ defineHelper(r, 'nodeHistogram', ['encoding/binary', 'math', 'math/big', 'math/bits', 'sort', 'time'], ['typedArrays', 'promise', 'error', 'dynamic', 'orderedMap', 'eventLoop'], histogramHelper);
1968
+ defineClass(r, 'perf_hooks.PercentileCI', '*TnPercentileCI', 'class', '', 'value=float64; lower=float64; upper=float64', '', '');
1969
+ defineClass(r, 'perf_hooks.MeanCI', '*TnMeanCI', 'class', '', 'mean=float64; lower=float64; upper=float64', '', '');
1970
+ defineClass(r, 'perf_hooks.ConfidenceInterval', '*TnConfidenceInterval', 'class', '', 'lower=float64; upper=float64', '', '');
1971
+ defineClass(r, 'perf_hooks.WelchResult', '*TnWelchResult', 'class', '', 'tStatistic=float64; degreesOfFreedom=float64; pValue=float64; confidenceInterval=*TnConfidenceInterval', '', '');
1972
+ defineClass(r, 'perf_hooks.MannWhitneyResult', '*TnMannWhitneyResult', 'class', '', 'uStatistic=float64; zScore=float64; pValue=float64', '', '');
1973
+ defineClass(r, 'perf_hooks.QrdeResult', '*TnQrdeResult', 'class', '', 'probabilities=*TnFloat64Array; quantiles=*TnFloat64Array; densities=*TnFloat64Array; count=*tnbig.Int; bucketCount=float64; corrections=float64; dequantize=string', '', '');
1974
+ defineClass(r, 'perf_hooks.Histogram', '*TnHistogram', 'class', 'record=; recordDelta=; recordCorrected=; percentile=float64; percentileBigInt=*tnbig.Int; percentilesAt=*TnMap[float64, float64]; percentileCI=*TnPercentileCI; meanCI=*TnMeanCI; burnRate=float64; welchTest=*TnWelchResult; ksTest=float64; mannWhitneyTest=*TnMannWhitneyResult; cohensD=float64; cliffsD=float64; linearBuckets=*TnMap[float64, float64]; logBuckets=*TnMap[float64, float64]; qrde=chan *TnQrdeResult; diff=*TnHistogram; export=[]byte; reset=; enable=bool; disable=bool; countAt=float64; cdf=float64; ccdf=float64; add=; subtract=; snapshot=*TnHistogram; toJSON=*TnMap[string, interface{}]', '', 'count=float64; countBigInt=*tnbig.Int; min=float64; minBigInt=*tnbig.Int; max=float64; maxBigInt=*tnbig.Int; mean=float64; stddev=float64; exceeds=float64; exceedsBigInt=*tnbig.Int; percentiles=*TnMap[float64, float64]; percentilesBigInt=*TnMap[float64, *tnbig.Int]; skewness=float64; kurtosis=float64; resetCount=float64; ewmaMean=float64; ewmaStddev=float64; ewmaErrorRate=float64', '');
1975
+ for (const name of ['RecordableHistogram', 'IntervalHistogram']) {
1976
+ r.classes[`perf_hooks.${name}`] = r.classes['perf_hooks.Histogram'];
1977
+ }
1978
+ defineClass(r, 'perf_hooks.SlidingWindowHistogram', '*TnSlidingWindowHistogram', 'class', 'record=; snapshot=*TnHistogram; reset=', '', '', '');
1979
+ const fn = (key, result, emit) => {
1980
+ r.functions[key] = (c) => {
1981
+ r.helper('nodeHistogram');
1982
+ return emit(c);
1983
+ };
1984
+ if (result)
1985
+ r.results[key] = result;
1986
+ };
1987
+ fn('perf_hooks.createHistogram', '*TnHistogram', (c) => `TnCreateHistogram(${c.args.join(', ')})`);
1988
+ fn('perf_hooks.createSlidingWindowHistogram', '*TnSlidingWindowHistogram', (c) => `TnCreateSlidingWindowHistogram(${c.args[0] ?? 'nil'})`);
1989
+ fn('perf_hooks.importHistogram', '*TnHistogram', (c) => `TnImportHistogram(${c.args[0] ?? 'nil'})`);
1990
+ fn('perf_hooks.monitorEventLoopDelay', '*TnHistogram', (c) => `TnMonitorEventLoopDelay(${c.args.join(', ')})`);
1991
+ }