max_queue_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //go:build integration
  2. package integration
  3. import (
  4. "context"
  5. "errors"
  6. "log/slog"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "sync"
  11. "testing"
  12. "time"
  13. "github.com/stretchr/testify/require"
  14. "github.com/ollama/ollama/api"
  15. "github.com/ollama/ollama/envconfig"
  16. )
  17. func TestMaxQueue(t *testing.T) {
  18. if os.Getenv("OLLAMA_TEST_EXISTING") != "" {
  19. t.Skip("Max Queue test requires spawing a local server so we can adjust the queue size")
  20. return
  21. }
  22. // Note: This test can be quite slow when running in CPU mode, so keep the threadCount low unless your on GPU
  23. // Also note that by default Darwin can't sustain > ~128 connections without adjusting limits
  24. threadCount := 32
  25. if maxQueue := envconfig.MaxQueue(); maxQueue != 0 {
  26. threadCount = int(maxQueue)
  27. } else {
  28. t.Setenv("OLLAMA_MAX_QUEUE", strconv.Itoa(threadCount))
  29. }
  30. req := api.GenerateRequest{
  31. Model: "orca-mini",
  32. Prompt: "write a long historical fiction story about christopher columbus. use at least 10 facts from his actual journey",
  33. Options: map[string]interface{}{
  34. "seed": 42,
  35. "temperature": 0.0,
  36. },
  37. }
  38. resp := []string{"explore", "discover", "ocean"}
  39. // CPU mode takes much longer at the limit with a large queue setting
  40. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
  41. defer cancel()
  42. client, _, cleanup := InitServerConnection(ctx, t)
  43. defer cleanup()
  44. require.NoError(t, PullIfMissing(ctx, client, req.Model))
  45. // Context for the worker threads so we can shut them down
  46. // embedCtx, embedCancel := context.WithCancel(ctx)
  47. embedCtx := ctx
  48. var genwg sync.WaitGroup
  49. go func() {
  50. genwg.Add(1)
  51. defer genwg.Done()
  52. slog.Info("Starting generate request")
  53. DoGenerate(ctx, t, client, req, resp, 45*time.Second, 5*time.Second)
  54. slog.Info("generate completed")
  55. }()
  56. // Give the generate a chance to get started before we start hammering on embed requests
  57. time.Sleep(5 * time.Millisecond)
  58. threadCount += 10 // Add a few extra to ensure we push the queue past its limit
  59. busyCount := 0
  60. resetByPeerCount := 0
  61. canceledCount := 0
  62. succesCount := 0
  63. counterMu := sync.Mutex{}
  64. var embedwg sync.WaitGroup
  65. for i := 0; i < threadCount; i++ {
  66. go func(i int) {
  67. embedwg.Add(1)
  68. defer embedwg.Done()
  69. slog.Info("embed started", "id", i)
  70. embedReq := api.EmbeddingRequest{
  71. Model: req.Model,
  72. Prompt: req.Prompt,
  73. Options: req.Options,
  74. }
  75. // Fresh client for every request
  76. client, _ = GetTestEndpoint()
  77. resp, genErr := client.Embeddings(embedCtx, &embedReq)
  78. counterMu.Lock()
  79. defer counterMu.Unlock()
  80. switch {
  81. case genErr == nil:
  82. succesCount++
  83. require.Greater(t, len(resp.Embedding), 5) // somewhat arbitrary, but sufficient to be reasonable
  84. case errors.Is(genErr, context.Canceled):
  85. canceledCount++
  86. case strings.Contains(genErr.Error(), "busy"):
  87. busyCount++
  88. case strings.Contains(genErr.Error(), "connection reset by peer"):
  89. resetByPeerCount++
  90. default:
  91. require.NoError(t, genErr, "%d request failed", i)
  92. }
  93. slog.Info("embed finished", "id", i)
  94. }(i)
  95. }
  96. genwg.Wait()
  97. slog.Info("generate done, waiting for embeds")
  98. embedwg.Wait()
  99. slog.Info("embeds completed", "success", succesCount, "busy", busyCount, "reset", resetByPeerCount, "canceled", canceledCount)
  100. require.Equal(t, resetByPeerCount, 0, "Connections reset by peer, have you updated your fd and socket limits?")
  101. require.True(t, busyCount > 0, "no requests hit busy error but some should have")
  102. require.True(t, canceledCount == 0, "no requests should have been canceled due to timeout")
  103. }