Аккаунт Войти / Регистрация
Тема
Размер текста
Analysis of Unit testing, Race detector, Benchmarking, Profiling (pprof) Go vs Java | Testing, Debugging, and Profiling

Analysis of Unit testing, Race detector, Benchmarking, Profiling (pprof) Go vs Java | Testing, Debugging, and Profiling

1. Unit testing

In Go, the built-in package testing allows writing unit tests. For Java developers, this is analogous to JUnit/TestNG.


// Go: simple unit test
func Add(a, b int) int {
    return a + b
}

func TestAdd(t *testing.T) {
    got := Add(2, 3)
    if got != 5 {
        t.Errorf("Expected 5, got %d", got)
    }
}

// Java: JUnit 5
public class MathTest {
    @Test
    void testAdd() {
        int result = Math.addExact(2, 3);
        Assertions.assertEquals(5, result);
    }
}
Unit tests in Go are simple and built into the standard library. For Java, it is more common to use JUnit or TestNG.

2. Race detector

Go has a built-in -race flag for detecting data races. Java developers will have to use static analysis or Thread-safe constructs.


// Running a test with race detection
go test -race ./...

// Java: Thread-safety manually
List<Integer> list = Collections.synchronizedList(new ArrayList<>());
// or use ConcurrentHashMap / AtomicInteger
Race detector in Go allows for quick identification of data races in all goroutines during testing.

3. Benchmarking

Go supports built-in benchmarks via testing.B. In Java, JMH or System.nanoTime() is used.


// Go benchmark
func BenchmarkAdd(b *testing.B) {
    for i := 0; i < b.N; i++ {
        _ = Add(2, 3)
    }
}

// Java benchmark (JMH)
@Benchmark
public void benchmarkAdd(Blackhole bh) {
    bh.consume(Math.addExact(2, 3));
}
Benchmarks help measure the performance of functions. Go has built this into the testing package, Java uses JMH for accurate measurements.

4. Profiling (pprof)

Go provides pprof for CPU, heap, blocking, and tracing analysis. In Java, profilers (VisualVM, YourKit, JFR) are used.


// Go: CPU profiling
f, _ := os.Create("cpu.prof")
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()

// Code for profiling
for i := 0; i < 1000000; i++ {
    _ = i * i
}

// Java: JFR / VisualVM
// CPU profiling via JFR or profiler in IDE
pprof allows obtaining complete CPU and memory profiles, as well as analyzing blocking and competition between goroutines.

5. Summary Table Go vs Java

Concept Go Java Comment
Unit testing testing package, t.Errorf, go test JUnit/TestNG, Assertions Go has built-in tests, Java requires external libraries
Race detector -race flag when testing Thread-safety, static analysis Go allows for automatic detection of data races
Benchmarking testing.B JMH or System.nanoTime() Go is built-in, Java requires JMH for accurate measurements
Profiling pprof (CPU, heap, locks) JFR, VisualVM, YourKit Go has a built-in tool, Java uses external profilers

Result

Go provides **built-in tools** for testing, race detection, benchmarking, and profiling, which allows writing concurrent code faster and more safely. For Java developers, analogs exist, but often require external libraries and additional configuration. Practical recommendation: use unit tests with a race detector to identify concurrency issues, benchmarks for performance evaluation, and pprof for analyzing bottlenecks in Go.

Оставить комментарий

Мой канал в социальных сетях
Отправляя email, вы принимаете условия политики конфиденциальности

Полезные статьи:

Pointers, functions, and execution control in Go vs Java | Types - Language
Series: Go for Java Developers — analyzing pointer, closures, defer, panic/recover In this article, we will analyze how Go manages the state and lifecycle of functions. A feature of Go is the ease of…
Multithreading in Go and Java: types of tasks and solution patterns
Multithreading is not just about "starting a million threads and letting them calculate". It is the art of efficiently using CPU and memory resources, safely processing data, and properly distributing…
Java under the Microscope: Stack, Heap, and GC using Code Examples
Diagram - Java Memory Model - Heap / Non-Heap / Stack Heap (memory for objects) Creates objects via new. Young Generation: Eden + Survivor. Old Generation: objects that have survived several GC c…

Новые статьи:

Concurrency is not about “starting many threads”. It’s about agreements between them. Imagine a restaurant kitchen: — cooks (threads / goroutines) — orders (tasks) — and the main question: how do th…
When HashMap starts killing production: the engineering story of ConcurrentHashMap
Imagine a typical production service. 32 CPU hundreds of threads configuration / session / rate limits cache tens of thousands of operations per second And somewhere inside — a regular Map. At first…
Zero Allocation in Java: what it is and why it matters
Zero Allocation — is an approach to writing code in which no unnecessary objects are created in heap memory during runtime. The main idea: fewer objects → less GC → higher stability and performance. …