Skip to content

Commit f35a480

Browse files
committed
feat: compile all native sources, add JMH benchmark and run-benchmark.bat
1 parent 0b46f5e commit f35a480

26 files changed

Lines changed: 1302 additions & 89 deletions

compile.bat

Lines changed: 57 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,57 @@
1-
@echo off
2-
:: FastJava Native DLL Compiler Script
3-
:: Auto-detects Visual Studio and JAVA_HOME
4-
5-
echo ========================================
6-
echo FastXXX Native Library Builder
7-
echo ========================================
8-
9-
:: Configuration
10-
set LIB_NAME=fastXXX
11-
12-
:: Try to find VS using vswhere.exe (most reliable)
13-
set "VSWHERE=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"
14-
if exist "%VSWHERE%" (
15-
for /f "usebackq tokens=*" %%i in (`"%VSWHERE%" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath`) do (
16-
set "VS_PATH=%%i"
17-
)
18-
)
19-
20-
:: Fallback: Check standard paths if vswhere didn't work
21-
if not defined VS_PATH (
22-
if exist "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" (
23-
set "VS_PATH=C:\Program Files\Microsoft Visual Studio\2022\Community"
24-
) else if exist "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat" (
25-
set "VS_PATH=C:\Program Files\Microsoft Visual Studio\2022\Enterprise"
26-
) else if exist "C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build\vcvars64.bat" (
27-
set "VS_PATH=C:\Program Files\Microsoft Visual Studio\2022\Professional"
28-
) else if exist "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat" (
29-
set "VS_PATH=C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools"
30-
) else if exist "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat" (
31-
set "VS_PATH=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community"
32-
)
33-
)
34-
35-
if not defined VS_PATH (
36-
echo ERROR: Visual Studio not found!
37-
echo Please install Visual Studio 2019 or 2022 with "Desktop development with C++"
38-
exit /b 1
39-
)
40-
41-
echo Found Visual Studio at: %VS_PATH%
42-
43-
:: Try to detect JAVA_HOME if not set
44-
if not defined JAVA_HOME (
45-
if exist "C:\Program Files\Java\jdk-25" (
46-
set "JAVA_HOME=C:\Program Files\Java\jdk-25"
47-
) else if exist "C:\Program Files\Eclipse Adoptium\jdk-17-hotspot" (
48-
set "JAVA_HOME=C:\Program Files\Eclipse Adoptium\jdk-17-hotspot"
49-
) else if exist "C:\Program Files\Java\jdk-17" (
50-
set "JAVA_HOME=C:\Program Files\Java\jdk-17"
51-
)
52-
)
53-
54-
if not defined JAVA_HOME (
55-
echo ERROR: JAVA_HOME not set!
56-
exit /b 1
57-
)
58-
59-
echo Using JAVA_HOME: %JAVA_HOME%
60-
61-
:: Setup environment
62-
call "%VS_PATH%\VC\Auxiliary\Build\vcvars64.bat"
63-
64-
:: Create build directory
65-
if not exist build mkdir build
66-
67-
:: Compile C++ source
68-
cl.exe /O2 /W3 /MD /EHsc /LD ^
69-
/I "%JAVA_HOME%\include" ^
70-
/I "%JAVA_HOME%\include\win32" ^
71-
/Fo:build\ ^
72-
/Fe:build\%LIB_NAME%.dll ^
73-
native\*.cpp ^
74-
user32.lib gdi32.lib shcore.lib advapi32.lib dwmapi.lib ^
75-
/link /DLL /MACHINE:X64 /DEF:native\%LIB_NAME%.def
76-
77-
if %ERRORLEVEL% == 0 (
78-
echo.
79-
echo [SUCCESS] DLL built at: build\%LIB_NAME%.dll
80-
:: Optional: copy to resources if needed
81-
:: copy build\%LIB_NAME%.dll src\main\resources\native\
82-
) else (
83-
echo.
84-
echo [FAILED] Compilation failed.
85-
exit /b 1
86-
)
87-
88-
echo.
89-
pause
1+
@echo off
2+
chcp 65001 > nul
3+
cd /d "%~dp0"
4+
5+
echo ========================================
6+
echo Compiling FastHardware Native Library
7+
echo ========================================
8+
9+
set "VSWHERE=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"
10+
if exist "%VSWHERE%" (
11+
for /f "usebackq tokens=*" %%i in (`"%VSWHERE%" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath`) do (
12+
set "VS_PATH=%%i"
13+
)
14+
)
15+
16+
if not defined VS_PATH (
17+
echo ERROR: Visual Studio not found!
18+
pause
19+
exit /b 1
20+
)
21+
22+
if not defined JAVA_HOME (
23+
if exist "C:\Program Files\Java\jdk-26.0.2.1" (
24+
set "JAVA_HOME=C:\Program Files\Java\jdk-26.0.2.1"
25+
) else if exist "C:\Program Files\Java\jdk-25" (
26+
set "JAVA_HOME=C:\Program Files\Java\jdk-25"
27+
) else if exist "C:\Program Files\Java\jdk-17" (
28+
set "JAVA_HOME=C:\Program Files\Java\jdk-17"
29+
)
30+
)
31+
32+
echo Visual Studio: %VS_PATH%
33+
echo JAVA_HOME: %JAVA_HOME%
34+
35+
call "%VS_PATH%\VC\Auxiliary\Build\vcvars64.bat"
36+
37+
if not exist build mkdir build
38+
if not exist "src\main\resources\native" mkdir "src\main\resources\native"
39+
40+
cl.exe /O2 /W3 /MD /EHsc /LD ^
41+
/I "%JAVA_HOME%\include" ^
42+
/I "%JAVA_HOME%\include\win32" ^
43+
/I "native\include" ^
44+
/Fo:build\ ^
45+
/Fe:build\fasthardware.dll ^
46+
native\src\*.cpp ^
47+
pdh.lib user32.lib gdi32.lib advapi32.lib wbemuuid.lib ole32.lib oleaut32.lib ^
48+
/link /DLL /MACHINE:X64
49+
50+
if %ERRORLEVEL% equ 0 (
51+
copy /Y build\fasthardware.dll src\main\resources\native\fasthardware.dll
52+
echo [SUCCESS] fasthardware.dll compiled and copied to src\main\resources\native\
53+
) else (
54+
echo [ERROR] Compilation failed!
55+
pause
56+
exit /b 1
57+
)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>fasthardware.benchmark</groupId>
5+
<artifactId>fasthardware-benchmark</artifactId>
6+
<version>0.1.0</version>
7+
<build>
8+
<plugins>
9+
<plugin>
10+
<artifactId>maven-compiler-plugin</artifactId>
11+
<version>3.11.0</version>
12+
<configuration>
13+
<source>17</source>
14+
<target>17</target>
15+
<annotationProcessorPaths>
16+
<path>
17+
<groupId>org.openjdk.jmh</groupId>
18+
<artifactId>jmh-generator-annprocess</artifactId>
19+
<version>${jmh.version}</version>
20+
</path>
21+
</annotationProcessorPaths>
22+
</configuration>
23+
</plugin>
24+
<plugin>
25+
<artifactId>maven-shade-plugin</artifactId>
26+
<version>3.5.0</version>
27+
<executions>
28+
<execution>
29+
<phase>package</phase>
30+
<goals>
31+
<goal>shade</goal>
32+
</goals>
33+
<configuration>
34+
<finalName>${uberjar.name}</finalName>
35+
<transformers>
36+
<transformer>
37+
<mainClass>org.openjdk.jmh.Main</mainClass>
38+
</transformer>
39+
</transformers>
40+
<filters>
41+
<filter>
42+
<artifact>*:*</artifact>
43+
<excludes>
44+
<exclude>META-INF/*.SF</exclude>
45+
<exclude>META-INF/*.DSA</exclude>
46+
<exclude>META-INF/*.RSA</exclude>
47+
</excludes>
48+
</filter>
49+
</filters>
50+
</configuration>
51+
</execution>
52+
</executions>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
<repositories>
57+
<repository>
58+
<id>jitpack.io</id>
59+
<url>https://jitpack.io</url>
60+
</repository>
61+
</repositories>
62+
<dependencies>
63+
<dependency>
64+
<groupId>org.openjdk.jmh</groupId>
65+
<artifactId>jmh-generator-annprocess</artifactId>
66+
<version>1.37</version>
67+
<scope>provided</scope>
68+
</dependency>
69+
</dependencies>
70+
<properties>
71+
<jmh.version>1.37</jmh.version>
72+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
73+
<maven.compiler.target>17</maven.compiler.target>
74+
<uberjar.name>benchmarks</uberjar.name>
75+
<maven.compiler.source>17</maven.compiler.source>
76+
</properties>
77+
</project>

examples/Benchmark/pom.xml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>fasthardware.benchmark</groupId>
8+
<artifactId>fasthardware-benchmark</artifactId>
9+
<version>0.1.0</version>
10+
11+
<properties>
12+
<maven.compiler.source>17</maven.compiler.source>
13+
<maven.compiler.target>17</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<jmh.version>1.37</jmh.version>
16+
<uberjar.name>benchmarks</uberjar.name>
17+
</properties>
18+
19+
<repositories>
20+
<repository>
21+
<id>jitpack.io</id>
22+
<url>https://jitpack.io</url>
23+
</repository>
24+
</repositories>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>com.github.andrestubbe</groupId>
29+
<artifactId>FastHardware</artifactId>
30+
<version>0.1.0</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>com.github.andrestubbe</groupId>
34+
<artifactId>FastCore</artifactId>
35+
<version>main-SNAPSHOT</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.openjdk.jmh</groupId>
39+
<artifactId>jmh-core</artifactId>
40+
<version>${jmh.version}</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.openjdk.jmh</groupId>
44+
<artifactId>jmh-generator-annprocess</artifactId>
45+
<version>${jmh.version}</version>
46+
<scope>provided</scope>
47+
</dependency>
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-compiler-plugin</artifactId>
55+
<version>3.11.0</version>
56+
<configuration>
57+
<source>17</source>
58+
<target>17</target>
59+
<annotationProcessorPaths>
60+
<path>
61+
<groupId>org.openjdk.jmh</groupId>
62+
<artifactId>jmh-generator-annprocess</artifactId>
63+
<version>${jmh.version}</version>
64+
</path>
65+
</annotationProcessorPaths>
66+
</configuration>
67+
</plugin>
68+
<plugin>
69+
<groupId>org.apache.maven.plugins</groupId>
70+
<artifactId>maven-shade-plugin</artifactId>
71+
<version>3.5.0</version>
72+
<executions>
73+
<execution>
74+
<phase>package</phase>
75+
<goals>
76+
<goal>shade</goal>
77+
</goals>
78+
<configuration>
79+
<finalName>${uberjar.name}</finalName>
80+
<transformers>
81+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
82+
<mainClass>org.openjdk.jmh.Main</mainClass>
83+
</transformer>
84+
</transformers>
85+
<filters>
86+
<filter>
87+
<artifact>*:*</artifact>
88+
<excludes>
89+
<exclude>META-INF/*.SF</exclude>
90+
<exclude>META-INF/*.DSA</exclude>
91+
<exclude>META-INF/*.RSA</exclude>
92+
</excludes>
93+
</filter>
94+
</filters>
95+
</configuration>
96+
</execution>
97+
</executions>
98+
</plugin>
99+
</plugins>
100+
</build>
101+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package fasthardware.benchmark;
2+
3+
import fasthardware.FastHardware;
4+
import org.openjdk.jmh.annotations.*;
5+
6+
import java.util.concurrent.TimeUnit;
7+
8+
@BenchmarkMode(Mode.Throughput)
9+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
10+
@State(Scope.Benchmark)
11+
@Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS)
12+
@Measurement(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS)
13+
@Fork(1)
14+
public class Benchmark {
15+
16+
private FastHardware hardware;
17+
18+
@Setup
19+
public void setup() {
20+
try {
21+
hardware = FastHardware.create();
22+
} catch (Exception e) {
23+
hardware = null;
24+
}
25+
}
26+
27+
@org.openjdk.jmh.annotations.Benchmark
28+
public long benchmarkGetFreeMemory() {
29+
if (hardware != null) {
30+
return hardware.getFreeMemoryBytes();
31+
}
32+
return 0L;
33+
}
34+
35+
@org.openjdk.jmh.annotations.Benchmark
36+
public double benchmarkGetGlobalCpuUsage() {
37+
if (hardware != null) {
38+
return hardware.getGlobalCpuUsage();
39+
}
40+
return 0.0;
41+
}
42+
}
2.77 MB
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
JMH S 32 fasthardware.benchmark.Benchmark S 77 fasthardware.benchmark.jmh_generated.Benchmark_benchmarkGetFreeMemory_jmhTest S 22 benchmarkGetFreeMemory S 10 Throughput E A 1 1 1 E I 1 2 T 3 1 s E I 1 3 T 3 1 s E I 1 1 E E E E E E U 12 MILLISECONDS E E
2+
JMH S 32 fasthardware.benchmark.Benchmark S 81 fasthardware.benchmark.jmh_generated.Benchmark_benchmarkGetGlobalCpuUsage_jmhTest S 26 benchmarkGetGlobalCpuUsage S 10 Throughput E A 1 1 1 E I 1 2 T 3 1 s E I 1 3 T 3 1 s E I 1 1 E E E E E E U 12 MILLISECONDS E E
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
dontinline,*.*_all_jmhStub
2+
dontinline,*.*_avgt_jmhStub
3+
dontinline,*.*_sample_jmhStub
4+
dontinline,*.*_ss_jmhStub
5+
dontinline,*.*_thrpt_jmhStub
6+
inline,fasthardware/benchmark/Benchmark.benchmarkGetFreeMemory
7+
inline,fasthardware/benchmark/Benchmark.benchmarkGetGlobalCpuUsage
8+
inline,fasthardware/benchmark/Benchmark.setup
Binary file not shown.

0 commit comments

Comments
 (0)