@@ -43,6 +43,7 @@ import software.aws.toolkits.jetbrains.utils.rules.PythonCodeInsightTestFixtureR
43
43
import software.aws.toolkits.telemetry.CodewhispererLanguage
44
44
import java.io.File
45
45
import java.io.FileInputStream
46
+ import java.lang.management.ManagementFactory
46
47
import java.util.Base64
47
48
import java.util.UUID
48
49
import java.util.zip.ZipFile
@@ -54,17 +55,27 @@ class CodeWhispererCodeFileScanTest : CodeWhispererCodeScanTestBase(PythonCodeIn
54
55
private lateinit var psifile2: PsiFile
55
56
private lateinit var psifile3: PsiFile
56
57
private lateinit var psifile4: PsiFile
58
+ private lateinit var psifilePerformanceTest: PsiFile
59
+ private lateinit var psifilePerformanceTest2: PsiFile
57
60
private lateinit var file: File
58
61
private lateinit var file2: File
59
62
private lateinit var file3: File
60
63
private lateinit var file4: File
64
+ private lateinit var performanceTestfileWithPayload200KB: File
65
+ private lateinit var performanceTestfileWithPayload150KB: File
61
66
private lateinit var virtualFile3: VirtualFile
62
67
private lateinit var virtualFile4: VirtualFile
63
68
private lateinit var sessionConfigSpy: CodeScanSessionConfig
64
69
private lateinit var sessionConfigSpy2: CodeScanSessionConfig
70
+ private lateinit var sessionConfigSpy3: CodeScanSessionConfig
71
+ private lateinit var sessionConfigSpy4: CodeScanSessionConfig
65
72
private val payloadContext = PayloadContext (CodewhispererLanguage .Python , 1 , 1 , 10 , listOf (), 600 , 200 )
66
73
private lateinit var codeScanSessionContext: CodeScanSessionContext
74
+ private lateinit var codeScanSessionContext2: CodeScanSessionContext
75
+ private lateinit var codeScanSessionContext3: CodeScanSessionContext
67
76
private lateinit var codeScanSessionSpy: CodeWhispererCodeScanSession
77
+ private lateinit var codeScanSessionSpy2: CodeWhispererCodeScanSession
78
+ private lateinit var codeScanSessionSpy3: CodeWhispererCodeScanSession
68
79
private val codeScanName = UUID .randomUUID().toString()
69
80
70
81
@Before
@@ -131,6 +142,33 @@ class CodeWhispererCodeFileScanTest : CodeWhispererCodeScanTestBase(PythonCodeIn
131
142
virtualFile4 = psifile4.virtualFile
132
143
file4 = virtualFile4.toNioPath().toFile()
133
144
145
+ // Create a 200KB file
146
+ val content = " a" .repeat(200 * 1024 )
147
+ psifilePerformanceTest = projectRule.fixture.addFileToProject(" test.txt" , content)
148
+ performanceTestfileWithPayload200KB = psifilePerformanceTest.virtualFile.toNioPath().toFile()
149
+
150
+ sessionConfigSpy3 = spy(
151
+ CodeScanSessionConfig .create(
152
+ psifilePerformanceTest.virtualFile,
153
+ project,
154
+ CodeWhispererConstants .CodeAnalysisScope .FILE
155
+ )
156
+ )
157
+ setupResponse(psifilePerformanceTest.virtualFile.toNioPath().relativeTo(sessionConfigSpy3.projectRoot.toNioPath()))
158
+
159
+ // Create a 150KB file
160
+ val codeContentForPayload = " a" .repeat(150 * 1024 )
161
+ psifilePerformanceTest2 = projectRule.fixture.addFileToProject(" test.txt" , codeContentForPayload)
162
+ performanceTestfileWithPayload150KB = psifilePerformanceTest2.virtualFile.toNioPath().toFile()
163
+
164
+ sessionConfigSpy4 = spy(
165
+ CodeScanSessionConfig .create(
166
+ psifilePerformanceTest2.virtualFile,
167
+ project,
168
+ CodeWhispererConstants .CodeAnalysisScope .FILE
169
+ )
170
+ )
171
+ setupResponse(psifilePerformanceTest2.virtualFile.toNioPath().relativeTo(sessionConfigSpy4.projectRoot.toNioPath()))
134
172
sessionConfigSpy = spy(
135
173
CodeScanSessionConfig .create(
136
174
psifile.virtualFile,
@@ -158,6 +196,13 @@ class CodeWhispererCodeFileScanTest : CodeWhispererCodeScanTestBase(PythonCodeIn
158
196
codeScanSessionSpy = spy(CodeWhispererCodeScanSession (codeScanSessionContext))
159
197
doNothing().`when `(codeScanSessionSpy).uploadArtifactToS3(any(), any(), any(), any(), isNull(), any())
160
198
199
+ codeScanSessionContext2 = CodeScanSessionContext (project, sessionConfigSpy3, CodeWhispererConstants .CodeAnalysisScope .FILE )
200
+ codeScanSessionSpy2 = spy(CodeWhispererCodeScanSession (codeScanSessionContext2))
201
+ doNothing().`when `(codeScanSessionSpy2).uploadArtifactToS3(any(), any(), any(), any(), isNull(), any())
202
+
203
+ codeScanSessionContext3 = CodeScanSessionContext (project, sessionConfigSpy4, CodeWhispererConstants .CodeAnalysisScope .FILE )
204
+ codeScanSessionSpy3 = spy(CodeWhispererCodeScanSession (codeScanSessionContext3))
205
+ doNothing().`when `(codeScanSessionSpy3).uploadArtifactToS3(any(), any(), any(), any(), isNull(), any())
161
206
mockClient.stub {
162
207
onGeneric { createUploadUrl(any()) }.thenReturn(fakeCreateUploadUrlResponse)
163
208
onGeneric { createCodeScan(any(), any()) }.thenReturn(fakeCreateCodeScanResponse)
@@ -166,6 +211,74 @@ class CodeWhispererCodeFileScanTest : CodeWhispererCodeScanTestBase(PythonCodeIn
166
211
}
167
212
}
168
213
214
+ @Test
215
+ fun `test run() - measure CPU and memory usage` () {
216
+ // Set up CPU and Memory monitoring
217
+ val runtime = Runtime .getRuntime()
218
+ val bean = ManagementFactory .getThreadMXBean()
219
+ val startCpuTime = bean.getCurrentThreadCpuTime()
220
+ val startMemoryUsage = runtime.totalMemory() - runtime.freeMemory()
221
+ val startSystemTime = System .nanoTime()
222
+
223
+ // Run the code scan
224
+ runBlocking {
225
+ codeScanSessionSpy2.run ()
226
+ }
227
+
228
+ // Calculate CPU and memory usage
229
+ val endCpuTime = bean.getCurrentThreadCpuTime()
230
+ val endMemoryUsage = runtime.totalMemory() - runtime.freeMemory()
231
+ val endSystemTime = System .nanoTime()
232
+
233
+ val cpuTimeUsedNanos = endCpuTime - startCpuTime
234
+ val cpuTimeUsedSeconds = cpuTimeUsedNanos / 1_000_000_000.0
235
+ val elapsedTimeSeconds = (endSystemTime - startSystemTime) / 1_000_000_000.0
236
+
237
+ val memoryUsed = endMemoryUsage - startMemoryUsage
238
+ val memoryUsedInMB = memoryUsed / (1024.0 * 1024.0 ) // Converting into MB
239
+
240
+ // Calculate CPU usage in percentage
241
+ val cpuUsagePercentage = (cpuTimeUsedSeconds / elapsedTimeSeconds) * 100
242
+
243
+ assertThat(cpuTimeUsedSeconds).isLessThan(5.0 )
244
+ assertThat(cpuUsagePercentage).isLessThan(30.0 )
245
+ assertThat(memoryUsedInMB).isLessThan(200.0 ) // Memory used should be less than 200MB
246
+ }
247
+
248
+ @Test
249
+ fun `test run() - measure CPU and memory usage with payload of 150KB` () {
250
+ // Set up CPU and Memory monitoring
251
+ val runtime = Runtime .getRuntime()
252
+ val bean = ManagementFactory .getThreadMXBean()
253
+ val startCpuTime = bean.getCurrentThreadCpuTime()
254
+ val startMemoryUsage = runtime.totalMemory() - runtime.freeMemory()
255
+ val startSystemTime = System .nanoTime()
256
+
257
+ // Run the code scan
258
+ runBlocking {
259
+ codeScanSessionSpy3.run ()
260
+ }
261
+
262
+ // Calculate CPU and memory usage
263
+ val endCpuTime = bean.getCurrentThreadCpuTime()
264
+ val endMemoryUsage = runtime.totalMemory() - runtime.freeMemory()
265
+ val endSystemTime = System .nanoTime()
266
+
267
+ val cpuTimeUsedNanos = endCpuTime - startCpuTime
268
+ val cpuTimeUsedSeconds = cpuTimeUsedNanos / 1_000_000_000.0
269
+ val elapsedTimeSeconds = (endSystemTime - startSystemTime) / 1_000_000_000.0
270
+
271
+ val memoryUsed = endMemoryUsage - startMemoryUsage
272
+ val memoryUsedInMB = memoryUsed / (1024.0 * 1024.0 ) // Converting into MB
273
+
274
+ // Calculate CPU usage in percentage
275
+ val cpuUsagePercentage = (cpuTimeUsedSeconds / elapsedTimeSeconds) * 100
276
+
277
+ assertThat(cpuTimeUsedSeconds).isLessThan(5.0 )
278
+ assertThat(cpuUsagePercentage).isLessThan(30.0 )
279
+ assertThat(memoryUsedInMB).isLessThan(200.0 ) // Memory used should be less than 200MB
280
+ }
281
+
169
282
@Test
170
283
fun `test createUploadUrlAndUpload()` () {
171
284
val fileMd5: String = Base64 .getEncoder().encodeToString(DigestUtils .md5(FileInputStream (file)))
0 commit comments