#Performance Benchmark Tests

4 messages · Page 1 of 1 (latest)

leaden mantle
#

I'm trying to write some Benchmark tests for this csv file reader just using default java utils, I'm not sure how to go about it

package org.sample;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class Test {

Map<Integer, List<String>> props;

public Test(String filename) {

    props = new HashMap<>();
    String line = "";
    String splitBy = ",";
    boolean isFirstLine = true;

    try {
        BufferedReader br = new BufferedReader(new FileReader(filename));

        while ((line = br.readLine()) != null) {

            if (isFirstLine) {
                isFirstLine = false;
                continue;
            }

            String[] data = line.split(splitBy);
            List<String> details = new ArrayList<>();
            details.add(data[0]);
            details.add(data[1]);
            details.add(data[2]);
            details.add(data[3]);
            details.add(data[4]);

            props.put(Integer.parseInt(data[0]), details);
        }
    } catch (FileNotFoundException e) {
        System.out.println("File not found");
    } catch (IOException e) {
        System.out.println("Error Reading File");
    }
}

public static void main(String[] args) {

    Test cw = new Test(args[0]);
    System.out.println(cw.getProperty(Integer.parseInt(args[1])));
    System.out.println();
    System.out.println(cw.getTotalSalesForCity(args[2]));
    System.out.println();
    String[] properties = cw.getMostRecentPropertiesSold(Integer.parseInt(args[3]));
    for (int i = 0; i < Integer.parseInt(args[3]); i++) {
        System.out.println(properties[i] + ",");
    }
    System.out.println();
}

public String getProperty(int id) {
    String property = "" + id;

    if (props.containsKey(id)) {
        List<String> details = props.get(id);
        return "ID: " + details.get(0) + ", " + "City: " + details.get(1) + ", " + "Owner: " + details.get(2) + ", " + "Sale Price: " + details.get(3) + ", " + "Sale Date: " + details.get(4);
    }
    return property;
}

public String getTotalSalesForCity(String c) {
    int total = 0;
    for (List<String> details : props.values()) {
        if (details.get(1).trim().equalsIgnoreCase(c.trim())) {
            total += Integer.parseInt(details.get(3));
        }
    }
    return "Total Sales for " + c + " = " + total;
}

public String[] getMostRecentPropertiesSold(int n) {
    String[] properties = new String[n];

    List<List<String>> propertyList = new ArrayList<>(props.values());
    Collections.sort(propertyList, (p1, p2) -> p2.get(4).compareTo(p1.get(4)));

    for (int i = 0; i < n && i < propertyList.size(); i++) {
        List<String> details = propertyList.get(i);
        properties[i] = "ID: " + details.get(0) + " " + "City: " + details.get(1) + " " + "Owner: " + details.get(2) + " " + "Sale Price: " + details.get(3) + " " + "Sale Date: " + details.get(4);
    }

    return properties;
}```
prisma craterBOT
#

This post has been reserved for your question.

Hey @leaden mantle! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

leaden mantle
#

I've got this as a test but I want to make it read the csv file instead, and don't want to hard code the csv file

package org.sample;

import org.openjdk.jmh.annotations.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 5, time = 1)@Fork(1)
public class Benchmarks {

    @State(Scope.Thread)
    public static class PropertyState {
        Properties properties;

        @Setup(Level.Trial)
        public void setup() {
            properties = new Properties();
            // Add a large number of properties to the Properties object
            for (int i = 1; i <= 1000000; i++) {
                properties.setProperty(Integer.toString(i), "ID" + i + ",City" + i%10 + ",Owner" + i + "," + Integer.toString(i*1000) + ",2022-01-01");
            }
        }
    }

    @Benchmark
    public String testGetProperty(PropertyState state) {
        int id = ThreadLocalRandom.current().nextInt(1, 1000000);
        return state.properties.getProperty(Integer.toString(id));
    }


}```