Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions tests/profiling/kv_cache_index/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
Copyright 2025 The llm-d Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"context"
"flag"
"fmt"
"math/rand/v2"
"time"

"github.com/llm-d/llm-d-kv-cache-manager/pkg/kvcache/kvblock"
"k8s.io/apimachinery/pkg/util/sets"
)

const modelName = "bert-base-uncased"

type IndexProfileResult struct {
AddTime time.Duration
LookupTime time.Duration
}

// TODO @kartikx: Use a more realistic workload if possible.
func generateWorkloadKeys(numKeys int) []kvblock.Key {
// Uses time as seed to ensure that different profiling runs get different keys.
randGen := rand.New(rand.NewPCG(42, uint64(time.Now().UnixNano())))
Copy link

Copilot AI Nov 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The random number generator is seeded with a fixed seed (42) combined with the current time, which contradicts the comment on line 39. The fixed seed of 42 means that if the same time value is obtained (or if profiling is run very quickly in succession), the same random keys could be generated. Consider removing the fixed seed and using only the time-based seed: rand.New(rand.NewPCG(uint64(time.Now().UnixNano()), uint64(time.Now().UnixNano()))) or simply use two different time-based values.

Suggested change
randGen := rand.New(rand.NewPCG(42, uint64(time.Now().UnixNano())))
randGen := rand.New(rand.NewPCG(uint64(time.Now().UnixNano()), uint64(time.Now().UnixNano())))

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you generate the same workload keys for all profiling sessions? That way, the only difference will be the indexer implementation.


keys := make([]kvblock.Key, numKeys)

for i := range numKeys {
keys[i] = kvblock.Key{
ModelName: modelName,
ChunkHash: randGen.Uint64(),
}
}

return keys
}

func averageIndexProfileResults(durations []IndexProfileResult) IndexProfileResult {
if len(durations) == 0 {
return IndexProfileResult{}
}

var total IndexProfileResult
for _, d := range durations {
total.AddTime += d.AddTime
total.LookupTime += d.LookupTime
}

count := time.Duration(len(durations))
return IndexProfileResult{
AddTime: total.AddTime / count,
LookupTime: total.LookupTime / count,
}
}

func profileInMemoryIndex(numTrials, numKeys int) (IndexProfileResult, error) {
return runProfileTrials(func() *kvblock.IndexConfig {
return kvblock.DefaultIndexConfig()
}, numTrials, numKeys)
}

func profileRedisIndex(numTrials, numKeys int) (IndexProfileResult, error) {
return runProfileTrials(func() *kvblock.IndexConfig {
return &kvblock.IndexConfig{
RedisConfig: kvblock.DefaultRedisIndexConfig(),
EnableMetrics: false,
}
}, numTrials, numKeys)
}

func profileCostIndex(numTrials, numKeys int) (IndexProfileResult, error) {
return runProfileTrials(func() *kvblock.IndexConfig {
return &kvblock.IndexConfig{
CostAwareMemoryConfig: kvblock.DefaultCostAwareMemoryIndexConfig(),
EnableMetrics: false,
}
}, numTrials, numKeys)
}

// runProfileTrials returns averaged results over multiple profiling runs.
func runProfileTrials(createConfig func() *kvblock.IndexConfig, numTrials, numKeys int) (IndexProfileResult, error) {
profileResults := make([]IndexProfileResult, numTrials)

for i := range numTrials {
ctx := context.Background()

indexConfig := createConfig()
index, err := kvblock.NewIndex(ctx, indexConfig)
if err != nil {
return IndexProfileResult{}, fmt.Errorf("failed to create index: %w", err)
}

result, err := measureIndexRun(ctx, index, "pod1", numKeys)
if err != nil {
return IndexProfileResult{}, fmt.Errorf("failed to profile index: %w", err)
}

Comment on lines +100 to +113
Copy link

Copilot AI Nov 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Creating a new index instance for each trial may skew profiling results due to initialization overhead. Consider clarifying whether this is intentional (to measure cold-start performance) or if the index should be created once outside the loop for warmed-up performance measurement. The current approach profiles both initialization and operation costs together.

Suggested change
for i := range numTrials {
ctx := context.Background()
indexConfig := createConfig()
index, err := kvblock.NewIndex(ctx, indexConfig)
if err != nil {
return IndexProfileResult{}, fmt.Errorf("failed to create index: %w", err)
}
result, err := measureIndexRun(ctx, index, "pod1", numKeys)
if err != nil {
return IndexProfileResult{}, fmt.Errorf("failed to profile index: %w", err)
}
ctx := context.Background()
indexConfig := createConfig()
index, err := kvblock.NewIndex(ctx, indexConfig)
if err != nil {
return IndexProfileResult{}, fmt.Errorf("failed to create index: %w", err)
}
for i := range numTrials {
result, err := measureIndexRun(ctx, index, "pod1", numKeys)
if err != nil {
return IndexProfileResult{}, fmt.Errorf("failed to profile index: %w", err)
}

Copilot uses AI. Check for mistakes.
profileResults[i] = result
}

return averageIndexProfileResults(profileResults), nil
}

// measureIndexRun performs a single profiling measurement of Add and Lookup operations on an index.
func measureIndexRun(ctx context.Context, index kvblock.Index, podName string, numKeys int) (IndexProfileResult, error) {
keys := generateWorkloadKeys(numKeys)

podEntries := []kvblock.PodEntry{{PodIdentifier: podName, DeviceTier: "gpu"}}
Copy link

Copilot AI Nov 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The empty podIdentifierSet means all pods will be returned during lookup (as documented in the Index interface). Consider adding a comment to clarify this is intentional, as it may be confusing why an empty set is used instead of including "pod1" to measure filtered lookup performance.

Suggested change
podEntries := []kvblock.PodEntry{{PodIdentifier: podName, DeviceTier: "gpu"}}
podEntries := []kvblock.PodEntry{{PodIdentifier: podName, DeviceTier: "gpu"}}
// Intentionally use an empty podIdentifierSet to return all pods during lookup,
// as documented in the Index interface. This measures unfiltered lookup performance.

Copilot uses AI. Check for mistakes.
podIdentifierSet := sets.Set[string]{}

addStartTime := time.Now()

err := index.Add(ctx, keys, podEntries)
if err != nil {
return IndexProfileResult{}, fmt.Errorf("failed to add entries: %w", err)
}

addTime := time.Since(addStartTime)

lookupStartTime := time.Now()

_, err = index.Lookup(ctx, keys, podIdentifierSet)
if err != nil {
return IndexProfileResult{}, fmt.Errorf("failed to lookup entries: %w", err)
}

lookupTime := time.Since(lookupStartTime)

return IndexProfileResult{
AddTime: addTime,
LookupTime: lookupTime,
}, nil
}

func main() {
var (
numTrials = flag.Int("trials", 5, "Number of profiling trials to run")
numKeys = flag.Int("keys", 100, "Number of keys to use in each profiling run")
)
flag.Parse()

result, err := profileCostIndex(*numTrials, *numKeys)
if err != nil {
fmt.Printf("Failed to profile cost index: %v\n", err)
} else {
fmt.Printf("[Cost Aware] Add: %v Lookup %v \n", result.AddTime, result.LookupTime)
Copy link

Copilot AI Nov 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing colon after "Lookup" in the output format string. The output format should be consistent with lines 169 and 176 which have a colon after "Lookup".

Suggested change
fmt.Printf("[Cost Aware] Add: %v Lookup %v \n", result.AddTime, result.LookupTime)
fmt.Printf("[Cost Aware] Add: %v Lookup: %v \n", result.AddTime, result.LookupTime)

Copilot uses AI. Check for mistakes.
}

result, err = profileRedisIndex(*numTrials, *numKeys)
if err != nil {
fmt.Printf("Failed to profile redis index: %v\n", err)
} else {
fmt.Printf("[Redis] Add: %v Lookup %v \n", result.AddTime, result.LookupTime)
Copy link

Copilot AI Nov 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing colon after "Lookup" in the output format string. The output format should be consistent with line 176 which has a colon after "Lookup".

Suggested change
fmt.Printf("[Redis] Add: %v Lookup %v \n", result.AddTime, result.LookupTime)
fmt.Printf("[Redis] Add: %v Lookup: %v \n", result.AddTime, result.LookupTime)

Copilot uses AI. Check for mistakes.
}

result, err = profileInMemoryIndex(*numTrials, *numKeys)
if err != nil {
fmt.Printf("Failed to profile in memory index: %v\n", err)
} else {
fmt.Printf("[InMemory] Add: %v Lookup %v \n", result.AddTime, result.LookupTime)
Comment on lines +162 to +176
Copy link

Copilot AI Nov 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space before the newline. This should be consistent with the format: \n not \n.

Suggested change
fmt.Printf("[Cost Aware] Add: %v Lookup %v \n", result.AddTime, result.LookupTime)
}
result, err = profileRedisIndex(*numTrials, *numKeys)
if err != nil {
fmt.Printf("Failed to profile redis index: %v\n", err)
} else {
fmt.Printf("[Redis] Add: %v Lookup %v \n", result.AddTime, result.LookupTime)
}
result, err = profileInMemoryIndex(*numTrials, *numKeys)
if err != nil {
fmt.Printf("Failed to profile in memory index: %v\n", err)
} else {
fmt.Printf("[InMemory] Add: %v Lookup %v \n", result.AddTime, result.LookupTime)
fmt.Printf("[Cost Aware] Add: %v Lookup %v\n", result.AddTime, result.LookupTime)
}
result, err = profileRedisIndex(*numTrials, *numKeys)
if err != nil {
fmt.Printf("Failed to profile redis index: %v\n", err)
} else {
fmt.Printf("[Redis] Add: %v Lookup %v\n", result.AddTime, result.LookupTime)
}
result, err = profileInMemoryIndex(*numTrials, *numKeys)
if err != nil {
fmt.Printf("Failed to profile in memory index: %v\n", err)
} else {
fmt.Printf("[InMemory] Add: %v Lookup %v\n", result.AddTime, result.LookupTime)

Copilot uses AI. Check for mistakes.
}
}
Loading