-
Notifications
You must be signed in to change notification settings - Fork 210
Improve Activity event handling in the UI #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| package main | ||
|
|
||
| // created for issue: #252 https://github.com/mostlygeek/llama-swap/issues/252 | ||
| // this simple benchmark tool sends a lot of small chat completion requests to llama-swap | ||
| // to make sure all the requests are accounted for. | ||
| // | ||
| // requests can be sent in parallel, and the tool will report the results. | ||
| // usage: go run main.go -baseurl http://localhost:8080/v1 -model llama3 -requests 1000 -par 5 | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "flag" | ||
| "fmt" | ||
| "io" | ||
| "log" | ||
| "net/http" | ||
| "os" | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| func main() { | ||
| // ----- CLI arguments ---------------------------------------------------- | ||
| var ( | ||
| baseurl string | ||
| modelName string | ||
| totalRequests int | ||
| parallelization int | ||
| ) | ||
|
|
||
| flag.StringVar(&baseurl, "baseurl", "http://localhost:8080/v1", "Base URL of the API (e.g., https://api.example.com)") | ||
| flag.StringVar(&modelName, "model", "", "Model name to use") | ||
| flag.IntVar(&totalRequests, "requests", 1, "Total number of requests to send") | ||
| flag.IntVar(¶llelization, "par", 1, "Maximum number of concurrent requests") | ||
| flag.Parse() | ||
|
|
||
| if baseurl == "" || modelName == "" { | ||
| fmt.Println("Error: both -baseurl and -model are required.") | ||
| flag.Usage() | ||
| os.Exit(1) | ||
| } | ||
| if totalRequests <= 0 { | ||
| fmt.Println("Error: -requests must be greater than 0.") | ||
| os.Exit(1) | ||
| } | ||
| if parallelization <= 0 { | ||
| fmt.Println("Error: -parallelization must be greater than 0.") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| // ----- HTTP client ------------------------------------------------------- | ||
| client := &http.Client{ | ||
| Timeout: 30 * time.Second, | ||
| } | ||
|
|
||
| // ----- Tracking response codes ------------------------------------------- | ||
| statusCounts := make(map[int]int) // map[statusCode]count | ||
| var mu sync.Mutex // protects statusCounts | ||
|
|
||
| // ----- Request queue (buffered channel) ---------------------------------- | ||
| requests := make(chan int, 10) // Buffered channel with capacity 10 | ||
|
|
||
| // Goroutine to fill the request queue | ||
| go func() { | ||
| for i := 0; i < totalRequests; i++ { | ||
| requests <- i + 1 | ||
| } | ||
| close(requests) | ||
| }() | ||
|
|
||
| // ----- Worker pool ------------------------------------------------------- | ||
| var wg sync.WaitGroup | ||
| for i := 0; i < parallelization; i++ { | ||
| wg.Add(1) | ||
| go func(workerID int) { | ||
| defer wg.Done() | ||
|
|
||
| for reqID := range requests { | ||
| // Build request payload as a single line JSON string | ||
| payload := `{"model":"` + modelName + `","max_tokens":100,"stream":false,"messages":[{"role":"user","content":"write a snake game in python"}]}` | ||
|
|
||
| // Send POST request | ||
| req, err := http.NewRequest(http.MethodPost, | ||
| fmt.Sprintf("%s/chat/completions", baseurl), | ||
| bytes.NewReader([]byte(payload))) | ||
| if err != nil { | ||
| log.Printf("[worker %d][req %d] request creation error: %v", workerID, reqID, err) | ||
| mu.Lock() | ||
| statusCounts[-1]++ | ||
| mu.Unlock() | ||
| continue | ||
| } | ||
| req.Header.Set("Content-Type", "application/json") | ||
|
|
||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| log.Printf("[worker %d][req %d] HTTP request error: %v", workerID, reqID, err) | ||
| mu.Lock() | ||
| statusCounts[-1]++ | ||
| mu.Unlock() | ||
| continue | ||
| } | ||
| io.Copy(io.Discard, resp.Body) | ||
| resp.Body.Close() | ||
|
|
||
| // Record status code | ||
| mu.Lock() | ||
| statusCounts[resp.StatusCode]++ | ||
| mu.Unlock() | ||
| } | ||
| }(i + 1) | ||
| } | ||
|
|
||
| // ----- Status ticker (prints every second) ------------------------------- | ||
| done := make(chan struct{}) | ||
| tickerDone := make(chan struct{}) | ||
| go func() { | ||
| ticker := time.NewTicker(1 * time.Second) | ||
| startTime := time.Now() | ||
| for { | ||
| select { | ||
| case <-ticker.C: | ||
| mu.Lock() | ||
| // Compute how many requests have completed so far | ||
| completed := 0 | ||
| for _, cnt := range statusCounts { | ||
| completed += cnt | ||
| } | ||
| // Calculate duration and progress | ||
| duration := time.Since(startTime) | ||
| progress := completed * 100 / totalRequests | ||
| fmt.Printf("Duration: %v, Completed: %d%% requests\n", duration, progress) | ||
| mu.Unlock() | ||
| case <-done: | ||
| duration := time.Since(startTime) | ||
| fmt.Printf("Duration: %v, Completed: %d%% requests\n", duration, 100) | ||
| close(tickerDone) | ||
| return | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| // Wait for all workers to finish | ||
| wg.Wait() | ||
| close(done) // stops the status-update goroutine | ||
| <-tickerDone // give ticker time to finish / print | ||
|
|
||
| // ----- Summary ------------------------------------------------------------ | ||
| fmt.Println("\n\n=== HTTP response code summary ===") | ||
| mu.Lock() | ||
| for code, cnt := range statusCounts { | ||
| if code == -1 { | ||
| fmt.Printf("Client-side errors (no HTTP response): %d\n", cnt) | ||
| } else { | ||
| fmt.Printf("%d : %d\n", code, cnt) | ||
| } | ||
| } | ||
| mu.Unlock() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { useAPI } from "../contexts/APIProvider"; | ||
| import { useEffect, useState, useMemo } from "react"; | ||
|
|
||
| type ConnectionStatus = "disconnected" | "connecting" | "connected"; | ||
|
|
||
| const ConnectionStatus = () => { | ||
| const { getConnectionStatus } = useAPI(); | ||
| const [eventStreamStatus, setEventStreamStatus] = useState<ConnectionStatus>("disconnected"); | ||
|
|
||
| useEffect(() => { | ||
| const interval = setInterval(() => { | ||
| setEventStreamStatus(getConnectionStatus()); | ||
| }, 1000); | ||
| return () => clearInterval(interval); | ||
| }); | ||
|
|
||
| const eventStatusColor = useMemo(() => { | ||
| switch (eventStreamStatus) { | ||
| case "connected": | ||
| return "bg-green-500"; | ||
| case "connecting": | ||
| return "bg-yellow-500"; | ||
| case "disconnected": | ||
| default: | ||
| return "bg-red-500"; | ||
| } | ||
| }, [eventStreamStatus]); | ||
|
|
||
| return ( | ||
| <div className="flex items-center" title={`event stream: ${eventStreamStatus}`}> | ||
| <span className={`inline-block w-3 h-3 rounded-full ${eventStatusColor} mr-2`}></span> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default ConnectionStatus; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Batch handling for metrics should dedupe by id, cap length, and be backward-compatible with single-object payloads.
Current logic can:
Apply this diff to harden the handler:
Add a top-level constant near LOG_LENGTH_LIMIT:
🤖 Prompt for AI Agents