sim.go ×20

Frontier kind: Code frontier

unlabeled · c_b6f6b58a68c3

6 tests · 138 LOC · 3 files · introduces 0 tests · 97 LOC · 1 file

Introduces — evidence that enters the hierarchy at this concept

Code
20 ranges97 lines · 1 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
33 ranges138 lines · 3 files · Browse complete extent
All tests (intent)
6 testsBrowse complete intent

Neighbourhood graph

The orange circle is the focus. Violet and green circles are every ancestor and descendant, broader and narrower, at any distance; blue squares and pink diamonds are the introduced files and exact introduced tests of every visible concept, not only the focus's. Arrows point from broader to narrower concepts and bridge only concepts omitted from this view. Undirected links show source or test introduction. Concept and file size follows LOC; exact test nodes use test-count units.

Introduced files, introduced tests, and structurally relevant concept specialization

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the native relationship evidence on this page.

Introduced files, introduced tests, and structurally relevant concept specializationTestFairness_UniformDistribution_500 · 0 introduced LOCTestFairness_UniformDist…TestFairness_CMSCounter · 0 introduced LOCTestFairness_CMSCountermap.go ×1 · 5 introduced LOCmap.go ×1TestFairness_MapCounter, TestFairness_UniformDistribution_50 · 0 introduced LOCTestFairness_MapCounter,…sim.go ×4 · 21 introduced LOCsim.go ×4sim.go ×2 · 7 introduced LOCsim.go ×2TestHybridCounter_StartsWithMap · 0 introduced LOCTestHybridCounter_Starts…hybrid.go ×4 · 12 introduced LOChybrid.go ×4map.go ×1 · 6 introduced LOCmap.go ×1map.go ×1 · 1 introduced LOCmap.go ×1map.go ×7 · 22 introduced LOCmap.go ×7TestOperatorServiceMetadata, TestWorkflowServiceMetadata · 0 introduced LOCTestOperatorServiceMetad…go.temporal.io/server/service/matching/counter/hybrid.go · 85 LOCcounter/hybrid.gogo.temporal.io/server/service/matching/counter/map.go · 102 LOCcounter/map.gogo.temporal.io/server/tools/fairsim/sim.go · 611 LOCfairsim/sim.goTestOperatorServiceMetadata · introduced test · go.temporal.io/server/common/api/TestOperatorServiceMetadataTestOperatorServiceMetad…TestWorkflowServiceMetadata · introduced test · go.temporal.io/server/common/api/TestWorkflowServiceMetadataTestWorkflowServiceMetad…TestHybridCounter_StartsWithMap · introduced test · go.temporal.io/server/service/matching/counter/TestHybridCounter_StartsWithMapTestHybridCounter_Starts…TestFairness_CMSCounter · introduced test · go.temporal.io/server/tools/fairsim/TestFairness_CMSCounterTestFairness_CMSCounterTestFairness_ExtremeSkew · introduced test · go.temporal.io/server/tools/fairsim/TestFairness_ExtremeSkewTestFairness_ExtremeSkewTestFairness_MapCounter · introduced test · go.temporal.io/server/tools/fairsim/TestFairness_MapCounterTestFairness_MapCounterTestFairness_UniformDistribution_50 · introduced test · go.temporal.io/server/tools/fairsim/TestFairness_UniformDistribution_50TestFairness_UniformDist…TestFairness_UniformDistribution_500 · introduced test · go.temporal.io/server/tools/fairsim/TestFairness_UniformDistribution_500TestFairness_UniformDist…TestFairness_Weights · introduced test · go.temporal.io/server/tools/fairsim/TestFairness_WeightsTestFairness_WeightsFocused concept · sim.go ×20 · 97 introduced LOCsim.go ×20

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the native relationship evidence on this page while the interactive map is unavailable.

Native relationship evidence

Every exact file and test below is linked only from the concept that introduces it.

Introduced tests

Every collected test enters the hierarchy at exactly one concept.

No tests are introduced at this concept. Its intent tests are introduced by other concepts.

Introduced code

Every collected source range enters the hierarchy at exactly one concept.

1 file ranked by introduced lines: 97 introduced LOC across 20 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

go.temporal.io/server/tools/fairsim/sim.go 97 introduced LOC · 20 ranges

Open complete file

150 }
151
152 > func newLatencyStats() *latencyStats { sim.go
153 > return &latencyStats{
154 > byKey: make(map[string][]int64),
155 > byKeyNormalized: make(map[string][]float64),
156 > }
157 > }
158
159 > func newState(rnd *rand.Rand, counterFactory func() counter.Counter, partitions, strideFactor int) *state { sim.go
160 > return &state{
161 > rnd: rnd,
162 > counterFactory: counterFactory,
163 > partitions: make([]partitionState, partitions),
164 > strideFactor: float32(strideFactor),
165 > }
166 > }
167
168 > func newSimulator(state *state, stats *latencyStats, defaultPriority int, w io.Writer, verbose bool) *simulator { sim.go
169 > return &simulator{
170 > state: state,
171 > stats: stats,
172 > w: w,
173 > verbose: verbose,
174 > defaultPriority: defaultPriority,
175 > }
176 > }
177
178 // addTask adds a task to the simulator, assigning defaults and an index.
179 > func (sim *simulator) addTask(t *task) { sim.go
180 > t.pri = cmp.Or(t.pri, sim.defaultPriority)
181 > t.fweight = cmp.Or(t.fweight, 1.0)
182 > t.index = sim.nextIndex
183 > sim.nextIndex++
184 > sim.state.addTask(t)
185 > }
186
187 // processTask records stats for a dispatched task and returns the latency.
188 > func (sim *simulator) processTask(t *task) int64 { sim.go
189 > latency := sim.dispatchIndex - t.index
190 > sim.stats.byKey[t.fkey] = append(sim.stats.byKey[t.fkey], latency)
191 > sim.stats.overall = append(sim.stats.overall, latency)
192 > sim.dispatchIndex++
193 > return latency
194 > }
195
196 // printTask writes a single task's dispatch info to the writer.
197 > func (sim *simulator) printTask(t *task, partition int, latency int64) { sim.go
198 > if !sim.verbose {
199 > return
200 > }
201 fmt.Fprintf(sim.w, "task idx:%6d dsp:%6d lat:%6d pri:%2d fkey:%10q fweight:%3g part:%2d payload:%q\n",
202 t.index, sim.dispatchIndex-1, latency, t.pri, t.fkey, t.fweight, partition, t.payload)
204
205 // drainTasks pops and processes all remaining tasks, printing each one.
206 > func (sim *simulator) drainTasks() { sim.go
207 > for t, partition := sim.state.popTask(); t != nil; t, partition = sim.state.popTask() {
208 > latency := sim.processTask(t)
209 > sim.printTask(t, partition, latency)
210 > }
211 }
212
357
358 // addTask adds a task to a random partition, computing its pass via the counter.
359 > func (s *state) addTask(t *task) { sim.go
360 > partition := &s.partitions[s.rnd.IntN(len(s.partitions))]
361 >
362 > if partition.perPri == nil {
363 > partition.perPri = make(map[int]perPriState)
364 > }
365
366 > priState, exists := partition.perPri[t.pri] sim.go
367 > if !exists {
368 > priState = perPriState{c: s.counterFactory()}
369 > partition.perPri[t.pri] = priState
370 > }
371
372 > t.pass = priState.c.GetPass(t.fkey, 0, max(1, int64(s.strideFactor/t.fweight))) sim.go
373 > heap.Push(&partition.heap, t)
374 }
375
376 // popTask returns the task with minimum (pri, pass, index) from a random partition.
377 > func (s *state) popTask() (*task, int) { sim.go
378 > for _, idx := range s.rnd.Perm(len(s.partitions)) {
379 > partition := &s.partitions[idx]
380 > if partition.heap.Len() > 0 {
381 > t := heap.Pop(&partition.heap).(*task) //nolint:revive
382 > return t, idx
383 > }
384 }
385 > return nil, -1 sim.go
386 }
387
394 // --- taskHeap (heap.Interface) ---
395
396 > func (h taskHeap) Len() int { return len(h) } sim.go
397
398 > func (h taskHeap) Less(i, j int) bool { sim.go
399 > if h[i].pri != h[j].pri {
400 return h[i].pri < h[j].pri
401 }
402 > if h[i].pass != h[j].pass { sim.go
403 > return h[i].pass < h[j].pass
404 > }
405 > return h[i].index < h[j].index
406 }
407
408 > func (h taskHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } sim.go
409
410 > func (h *taskHeap) Push(x any) { *h = append(*h, x.(*task)) } sim.go
411
412 > func (h *taskHeap) Pop() any { sim.go
413 > old := *h
414 > n := len(old)
415 > item := old[n-1]
416 > *h = old[:n-1]
417 > return item
418 > }
419
420 // --- latencyStats ---
421
422 > func (stats *latencyStats) calculateNormalized() { sim.go
423 > totalTasks := len(stats.overall)
424 > totalKeys := len(stats.byKey)
425 > if totalTasks == 0 || totalKeys == 0 {
426 return
427 }
431 // of total traffic, making values comparable across different workload sizes.
432 // Values center on 0: negative = expedited, positive = delayed.
433 > fairShareFactor := float64(totalKeys) / float64(totalTasks) sim.go
434 > stats.overallNormalized = stats.overallNormalized[:0]
435 > for key, latencies := range stats.byKey {
436 > taskCount := float64(len(latencies))
437 > normalizedLatencies := make([]float64, len(latencies))
438 > for i, rawLatency := range latencies {
439 > normalizedLatencies[i] = float64(rawLatency) / taskCount * fairShareFactor
440 > stats.overallNormalized = append(stats.overallNormalized, normalizedLatencies[i])
441 > }
442 > stats.byKeyNormalized[key] = normalizedLatencies
443 }
444 }