Skip to content

Commit 723a877

Browse files
authored
[Security Scans]: Adding Performance tests (#4778)
* Adding Performance tests for Security scans.
1 parent c3c1cf1 commit 723a877

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/codescan/CodeWhispererCodeFileScanTest.kt

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import software.aws.toolkits.jetbrains.utils.rules.PythonCodeInsightTestFixtureR
4343
import software.aws.toolkits.telemetry.CodewhispererLanguage
4444
import java.io.File
4545
import java.io.FileInputStream
46+
import java.lang.management.ManagementFactory
4647
import java.util.Base64
4748
import java.util.UUID
4849
import java.util.zip.ZipFile
@@ -54,17 +55,27 @@ class CodeWhispererCodeFileScanTest : CodeWhispererCodeScanTestBase(PythonCodeIn
5455
private lateinit var psifile2: PsiFile
5556
private lateinit var psifile3: PsiFile
5657
private lateinit var psifile4: PsiFile
58+
private lateinit var psifilePerformanceTest: PsiFile
59+
private lateinit var psifilePerformanceTest2: PsiFile
5760
private lateinit var file: File
5861
private lateinit var file2: File
5962
private lateinit var file3: File
6063
private lateinit var file4: File
64+
private lateinit var performanceTestfileWithPayload200KB: File
65+
private lateinit var performanceTestfileWithPayload150KB: File
6166
private lateinit var virtualFile3: VirtualFile
6267
private lateinit var virtualFile4: VirtualFile
6368
private lateinit var sessionConfigSpy: CodeScanSessionConfig
6469
private lateinit var sessionConfigSpy2: CodeScanSessionConfig
70+
private lateinit var sessionConfigSpy3: CodeScanSessionConfig
71+
private lateinit var sessionConfigSpy4: CodeScanSessionConfig
6572
private val payloadContext = PayloadContext(CodewhispererLanguage.Python, 1, 1, 10, listOf(), 600, 200)
6673
private lateinit var codeScanSessionContext: CodeScanSessionContext
74+
private lateinit var codeScanSessionContext2: CodeScanSessionContext
75+
private lateinit var codeScanSessionContext3: CodeScanSessionContext
6776
private lateinit var codeScanSessionSpy: CodeWhispererCodeScanSession
77+
private lateinit var codeScanSessionSpy2: CodeWhispererCodeScanSession
78+
private lateinit var codeScanSessionSpy3: CodeWhispererCodeScanSession
6879
private val codeScanName = UUID.randomUUID().toString()
6980

7081
@Before
@@ -131,6 +142,33 @@ class CodeWhispererCodeFileScanTest : CodeWhispererCodeScanTestBase(PythonCodeIn
131142
virtualFile4 = psifile4.virtualFile
132143
file4 = virtualFile4.toNioPath().toFile()
133144

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()))
134172
sessionConfigSpy = spy(
135173
CodeScanSessionConfig.create(
136174
psifile.virtualFile,
@@ -158,6 +196,13 @@ class CodeWhispererCodeFileScanTest : CodeWhispererCodeScanTestBase(PythonCodeIn
158196
codeScanSessionSpy = spy(CodeWhispererCodeScanSession(codeScanSessionContext))
159197
doNothing().`when`(codeScanSessionSpy).uploadArtifactToS3(any(), any(), any(), any(), isNull(), any())
160198

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())
161206
mockClient.stub {
162207
onGeneric { createUploadUrl(any()) }.thenReturn(fakeCreateUploadUrlResponse)
163208
onGeneric { createCodeScan(any(), any()) }.thenReturn(fakeCreateCodeScanResponse)
@@ -166,6 +211,74 @@ class CodeWhispererCodeFileScanTest : CodeWhispererCodeScanTestBase(PythonCodeIn
166211
}
167212
}
168213

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+
169282
@Test
170283
fun `test createUploadUrlAndUpload()`() {
171284
val fileMd5: String = Base64.getEncoder().encodeToString(DigestUtils.md5(FileInputStream(file)))

0 commit comments

Comments
 (0)