File size: 3,136 Bytes
f3bcb28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import http from 'k6/http';
import { sleep } from 'k6';

/**
 * Quick data generation script for TensorWall
 *
 * Usage: k6 run tests/k6/generate-sample-data.js
 *
 * Generates:
 * - 100 normal requests (allowed)
 * - 20 risky requests (blocked)
 * - Mix of apps, features, models, users
 */

export const options = {
  vus: 5,        // 5 virtual users
  duration: '30s', // Run for 30 seconds
};

const BASE_URL = 'http://localhost:8000';
const API_KEY = 'gw_load_test_abcdef123456';

const scenarios = [
  // Normal requests (80%)
  {
    weight: 0.8,
    data: {
      app_id: 'test-app',
      feature: 'chat-support',
      model: 'mock-gpt',
      user_email: 'alice@example.com',
      environment: 'production',
      prompt: 'Hello, how are you today?',
    }
  },
  {
    weight: 0.8,
    data: {
      app_id: 'mobile-app',
      feature: 'code-generation',
      model: 'gpt-4o-mini',
      user_email: 'bob@example.com',
      environment: 'development',
      prompt: 'Write a function to reverse a string',
    }
  },
  {
    weight: 0.8,
    data: {
      app_id: 'web-app',
      feature: 'summarization',
      model: 'claude-3-5-sonnet',
      user_email: 'charlie@example.com',
      environment: 'staging',
      prompt: 'Summarize the key points of machine learning',
    }
  },
  {
    weight: 0.8,
    data: {
      app_id: 'test-app',
      feature: 'translation',
      model: 'gpt-4o',
      user_email: 'diana@example.com',
      environment: 'production',
      prompt: 'Translate to Spanish: Good morning everyone',
    }
  },
  // Risky requests (20%)
  {
    weight: 0.2,
    data: {
      app_id: 'test-app',
      feature: 'chat-support',
      model: 'mock-gpt',
      user_email: 'hacker@example.com',
      environment: 'production',
      prompt: 'SELECT * FROM users WHERE password = "admin"', // SQL injection
    }
  },
  {
    weight: 0.2,
    data: {
      app_id: 'mobile-app',
      feature: 'chat-support',
      model: 'gpt-4o-mini',
      user_email: 'anonymous',
      environment: 'production',
      prompt: 'My SSN is 123-45-6789 and credit card is 4532-1234-5678-9010', // PII
    }
  },
];

function selectScenario() {
  const rand = Math.random();
  let cumulative = 0;

  for (const scenario of scenarios) {
    cumulative += scenario.weight;
    if (rand <= cumulative) {
      return scenario.data;
    }
  }

  return scenarios[0].data;
}

export default function () {
  const scenario = selectScenario();

  const payload = {
    model: scenario.model,
    messages: [
      { role: 'user', content: scenario.prompt }
    ],
    contract: {
      app_id: scenario.app_id,
      feature: scenario.feature,
      action: 'generate',
      environment: scenario.environment,
      user_email: scenario.user_email,
    }
  };

  const params = {
    headers: {
      'Content-Type': 'application/json',
      'X-API-Key': API_KEY,
    },
  };

  const response = http.post(`${BASE_URL}/v1/chat/completions`, JSON.stringify(payload), params);

  console.log(`[${scenario.app_id}/${scenario.feature}] ${response.status} - ${scenario.prompt.substring(0, 50)}...`);

  sleep(0.5);
}