File size: 1,470 Bytes
5ecd2f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cd8047
5ecd2f9
 
 
 
 
8cd8047
 
 
 
 
 
 
5ecd2f9
 
 
 
 
8cd8047
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import random

from locust import HttpUser, between, task


class TuringApiUser(HttpUser):
    # Wait time between requests
    wait_time = between(1, 5)

    # List of supported languages
    languages = ["python", "java", "pharo"]

    # Code examples
    code_snippets = {
        "python": ["def init(self): pass", "print('Hello World')", "import os"],
        "java": ["public static void main(String[] args)", "System.out.println(e);", "private int x = 0;"],
        "pharo": ["Transcript show: 'Hello'.", "^ self size", "Object subclass: #Name"]
    }

    @task(1)
    def health_check(self):
        """
        Checks if the API is alive.
        """
        self.client.get("/")

    @task(3)
    def predict_code_classification(self):
        """
        Sends a prediction request with language as a QUERY PARAMETER.
        """
        # Randomly selects one of the three languages
        selected_lang = random.choice(self.languages)
        texts = self.code_snippets.get(selected_lang, ["generic code"])
        
    
        json_payload = {
            "texts": texts
        }
        
        query_params = {
            "language": selected_lang
        }
        
        headers = {'Content-Type': 'application/json'}
        
        # Perform the request
        self.client.post(
            "/predict", 
            params=query_params,  
            json=json_payload,   
            headers=headers, 
            name="/predict"
        )