Guest User

Untitled

a guest
Apr 3rd, 2026
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. import requests
  2. import time
  3. import json
  4.  
  5. # --- Configuration ---
  6. OLLAMA_URL = "http://localhost:11434/api/generate"
  7. LM_STUDIO_URL = "http://localhost:1234/v1/chat/completions"
  8.  
  9. OLLAMA_MODEL = "gemma4:e4b"
  10. # LM Studio usually accepts the folder path or the filename as the model identifier.
  11. LM_STUDIO_MODEL = "lmstudio-community/gemma-4-E4B-it-GGUF/gemma-4-E4B-it-Q8_0.gguf"
  12.  
  13. WARMUP_PROMPT = "Hi, respond with exactly one word: 'Ready'."
  14. BENCHMARK_PROMPT = "Write a highly detailed, 5-paragraph essay on the history of the microprocessor, focusing on architectural changes from the 1970s to modern Apple Silicon."
  15.  
  16. ITERATIONS = 3 # How many times to run the benchmark per engine
  17.  
  18. def test_ollama(prompt, is_warmup=False):
  19. payload = {
  20. "model": OLLAMA_MODEL,
  21. "prompt": prompt,
  22. "stream": False,
  23. "options": {
  24. "temperature": 0.0
  25. }
  26. }
  27.  
  28. start_time = time.time()
  29. try:
  30. response = requests.post(OLLAMA_URL, json=payload)
  31. response.raise_for_status()
  32. end_time = time.time()
  33.  
  34. data = response.json()
  35. duration = end_time - start_time
  36.  
  37. # Ollama returns eval_count (completion tokens)
  38. tokens = data.get("eval_count", 0)
  39. tps = tokens / duration if duration > 0 else 0
  40.  
  41. if not is_warmup:
  42. print(f" -> Generated {tokens} tokens in {duration:.2f}s | Speed: {tps:.2f} tokens/sec")
  43. return tps
  44. except Exception as e:
  45. print(f" -> Ollama error: {e}")
  46. return 0
  47.  
  48. def test_lm_studio(prompt, is_warmup=False):
  49. payload = {
  50. "model": LM_STUDIO_MODEL,
  51. "messages": [
  52. {"role": "user", "content": prompt}
  53. ],
  54. "temperature": 0.0,
  55. "stream": False
  56. }
  57.  
  58. start_time = time.time()
  59. try:
  60. response = requests.post(LM_STUDIO_URL, json=payload)
  61. response.raise_for_status()
  62. end_time = time.time()
  63.  
  64. data = response.json()
  65. duration = end_time - start_time
  66.  
  67. # LM Studio returns OpenAI-compatible usage stats
  68. tokens = data.get("usage", {}).get("completion_tokens", 0)
  69. tps = tokens / duration if duration > 0 else 0
  70.  
  71. if not is_warmup:
  72. print(f" -> Generated {tokens} tokens in {duration:.2f}s | Speed: {tps:.2f} tokens/sec")
  73. return tps
  74. except Exception as e:
  75. print(f" -> LM Studio error: {e}")
  76. return 0
  77.  
  78. def main():
  79. print("========================================")
  80. print("🤖 LLM Engine Benchmark: Ollama vs LM Studio")
  81. print("========================================\n")
  82.  
  83. # WARMUP PHASE
  84. print("--- Warming up models ---")
  85. print("Warming up Ollama...")
  86. test_ollama(WARMUP_PROMPT, is_warmup=True)
  87. print("Warming up LM Studio...")
  88. test_lm_studio(WARMUP_PROMPT, is_warmup=True)
  89. print("Warmup complete. Both models are in memory.\n")
  90.  
  91. # BENCHMARK PHASE
  92. ollama_results = []
  93. lm_studio_results = []
  94.  
  95. print(f"--- Starting Benchmark ({ITERATIONS} iterations) ---")
  96. for i in range(1, ITERATIONS + 1):
  97. print(f"\nIteration {i}/{ITERATIONS}:")
  98.  
  99. print("Running Ollama...")
  100. tps_ollama = test_ollama(BENCHMARK_PROMPT)
  101. if tps_ollama > 0: ollama_results.append(tps_ollama)
  102.  
  103. print("Running LM Studio...")
  104. tps_lm_studio = test_lm_studio(BENCHMARK_PROMPT)
  105. if tps_lm_studio > 0: lm_studio_results.append(tps_lm_studio)
  106.  
  107. # RESULTS CALCULATION
  108. print("\n========================================")
  109. print("📊 Final Results (Average Tokens / Second)")
  110. print("========================================")
  111.  
  112. if ollama_results:
  113. avg_ollama = sum(ollama_results) / len(ollama_results)
  114. print(f"Ollama Average: {avg_ollama:.2f} t/s")
  115. else:
  116. print("Ollama Average: Failed to run")
  117.  
  118. if lm_studio_results:
  119. avg_lm_studio = sum(lm_studio_results) / len(lm_studio_results)
  120. print(f"LM Studio Average: {avg_lm_studio:.2f} t/s")
  121. else:
  122. print("LM Studio Average: Failed to run")
  123.  
  124. if __name__ == "__main__":
  125. main()
Advertisement
Add Comment
Please, Sign In to add comment