summaryrefslogtreecommitdiff
path: root/src/main/java/org/linaro/benchmarks/micro/SystemArrayCopy.java
blob: 230bd4bf99224a31db4756fb8b67a78478d95730 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright (C) 2016 Linaro Limited. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

/*
 * Description:     Tracks performance of System.arraycopy intrinsics.
 * Main Focus:      Looped load store for varying copy lengths.
 * Secondary Focus:
 *
 */

package org.linaro.benchmarks.micro;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.lang.StringBuilder;
import java.lang.System;
import java.util.Random;
import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)

public class SystemArrayCopy {

  private static Random rnd = new Random();
  private static int ARRAY_COPY_SMALL = 16;
  private static int ARRAY_COPY_MEDIUM = 128;
  private static int ARRAY_COPY_LARGE = 1024;
  private static int MAX_BUFFER_BYTES = 8192;
  private static int ARRAY_LENGTH = 1024;
  private static String RANDOM_STRING = generateRandomString(MAX_BUFFER_BYTES);
  private static char[] cbuf = new char[MAX_BUFFER_BYTES];
  private static char arrayCopyCharBufferedReadSmallResult;
  private static char arrayCopyCharBufferedReadMediumResult;
  private static char arrayCopyCharBufferedReadLargeResult;

  private static String[] stringArray = new String[ARRAY_LENGTH];
  private static String[] stringArraySmall = new String[ARRAY_LENGTH];
  private static String[] stringArrayMedium = new String[ARRAY_LENGTH];
  private static String[] stringArrayLarge = new String[ARRAY_LENGTH];

  private static String generateRandomString(int sz) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < sz; i++) {
      sb.append(Character.valueOf((char)rnd.nextInt()));
    }
    return sb.toString();
  }

  static {
    for (int i = 0; i < ARRAY_LENGTH; i++) {
      stringArray[i] = String.valueOf(i);
    }
  }

  private void bufferedReadLoop(char[] cbuf, int copyLength) throws IOException {
    BufferedReader reader = new BufferedReader(new StringReader(RANDOM_STRING));
    int offset = 0;
    String s;
    /* Read 16Kb RANDOM_STRING in chunks of copyLength chars until EOF */
    while (offset < MAX_BUFFER_BYTES && (reader.read(cbuf, offset, copyLength)) != -1) {
      offset += copyLength;
    }
  }

  @Benchmark
  public void jmhTimeArrayCopyCharBufferedReadSmall() throws IOException {
    bufferedReadLoop(cbuf, ARRAY_COPY_SMALL);
    arrayCopyCharBufferedReadSmallResult = cbuf[MAX_BUFFER_BYTES - 1];
  }

  @Benchmark
  public void jmhTimeArrayCopyCharBufferedReadMedium() throws IOException {
    bufferedReadLoop(cbuf, ARRAY_COPY_MEDIUM);
    arrayCopyCharBufferedReadMediumResult = cbuf[MAX_BUFFER_BYTES - 1];
  }

  @Benchmark
  public void jmhTimeArrayCopyCharBufferedReadLarge() throws IOException {
    bufferedReadLoop(cbuf, ARRAY_COPY_LARGE);
    arrayCopyCharBufferedReadLargeResult = cbuf[MAX_BUFFER_BYTES - 1];
  }

  @Benchmark
  public void jmhTimeArrayCopySmall() {
    System.arraycopy(stringArray, 0, stringArraySmall, 0, ARRAY_COPY_SMALL);
  }

  @Benchmark
  public void jmhTimeArrayCopyMedium() {
    System.arraycopy(stringArray, 0, stringArrayMedium, 0, ARRAY_COPY_MEDIUM);
  }

  @Benchmark
  public void jmhTimeArrayCopyLarge() {
    System.arraycopy(stringArray, 0, stringArrayLarge, 0, ARRAY_COPY_LARGE);
  }
}