1 /*
  2  * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 package org.openjdk.bench.java.lang.foreign;
 25 
 26 import org.openjdk.jmh.annotations.Benchmark;
 27 import org.openjdk.jmh.annotations.BenchmarkMode;
 28 import org.openjdk.jmh.annotations.Fork;
 29 import org.openjdk.jmh.annotations.Measurement;
 30 import org.openjdk.jmh.annotations.Mode;
 31 import org.openjdk.jmh.annotations.OutputTimeUnit;
 32 import org.openjdk.jmh.annotations.Param;
 33 import org.openjdk.jmh.annotations.Setup;
 34 import org.openjdk.jmh.annotations.State;
 35 import org.openjdk.jmh.annotations.TearDown;
 36 import org.openjdk.jmh.annotations.Warmup;
 37 
 38 import java.lang.foreign.Arena;
 39 import java.lang.foreign.FunctionDescriptor;
 40 import java.lang.foreign.Linker;
 41 import java.lang.foreign.MemorySegment;
 42 import java.lang.foreign.MemorySegment.Scope;
 43 import java.lang.foreign.SegmentAllocator;
 44 import java.lang.invoke.MethodHandle;
 45 import java.util.concurrent.TimeUnit;
 46 
 47 import static java.lang.foreign.ValueLayout.JAVA_BYTE;
 48 
 49 @BenchmarkMode(Mode.AverageTime)
 50 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 51 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 52 @State(org.openjdk.jmh.annotations.Scope.Thread)
 53 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 54 @Fork(value = 3, jvmArgsAppend = { "--enable-native-access=ALL-UNNAMED" })
 55 public class ToCStringTest extends CLayouts {
 56 
 57     @Param({"5", "20", "100", "200"})
 58     public int size;
 59     public String str;
 60 
 61     static {
 62         System.loadLibrary("ToCString");
 63     }
 64 
 65     static final MethodHandle STRLEN;
 66 
 67     static {
 68         Linker abi = Linker.nativeLinker();
 69         STRLEN = abi.downcallHandle(abi.defaultLookup().find("strlen").get(),
 70                 FunctionDescriptor.of(C_INT, C_POINTER));
 71     }
 72 
 73     @Setup
 74     public void setup() {
 75         str = makeString(size);
 76     }
 77 
 78     @Benchmark
 79     public long jni_writeString() throws Throwable {
 80         return writeString(str);
 81     }
 82 
 83     @Benchmark
 84     public MemorySegment panama_writeString() throws Throwable {
 85         Arena arena = Arena.ofConfined();
 86         MemorySegment segment = arena.allocateFrom(str);
 87         arena.close();
 88         return segment;
 89     }
 90 
 91     static native long writeString(String str);
 92 
 93     static String makeString(int size) {
 94         String lorem = """
 95                 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
 96                  dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
 97                  ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
 98                  fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
 99                  mollit anim id est laborum.
100                 """;
101         return lorem.substring(0, size);
102     }
103 }