<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Init context on Grafana Labs</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/init-context/</link><description>Recent content in Init context on Grafana Labs</description><generator>Hugo -- gohugo.io</generator><language>en</language><atom:link href="/docs/k6/v2.3.x/javascript-api/init-context/index.xml" rel="self" type="application/rss+xml"/><item><title>open( filePath, [mode] )</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/init-context/open/</link><pubDate>Mon, 21 Sep 2026 15:16:28 +0000</pubDate><guid>https://grafana.com/docs/k6/v2.3.x/javascript-api/init-context/open/</guid><content><![CDATA[&lt;h1 id=&#34;open-filepath-mode-&#34;&gt;open( filePath, [mode] )&lt;/h1&gt;
&lt;p&gt;Opens a file, reading all its contents into memory for use in the script.&lt;/p&gt;


&lt;div class=&#34;admonition admonition-caution&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Caution&lt;/p&gt;&lt;p&gt;&lt;code&gt;open()&lt;/code&gt; can only be called from the 
    &lt;a href=&#34;/docs/k6/v2.3.x/using-k6/test-lifecycle/#the-init-stage&#34;&gt;init context&lt;/a&gt;. This restriction is necessary to determine the local files needed to bundle when distributing the test across multiple nodes.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;



&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;&lt;code&gt;open()&lt;/code&gt; often consumes a large amount of memory because every VU keeps a separate copy of the file in memory.&lt;/p&gt;
&lt;p&gt;To reduce the memory consumption, you can:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;open()&lt;/code&gt; within a 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-data/sharedarray/&#34;&gt;SharedArray&lt;/a&gt;, which shares the allocated file memory between VUs.&lt;/li&gt;
&lt;li&gt;Use 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-experimental/fs/&#34;&gt;open() from the &lt;code&gt;k6/experimental/fs&lt;/code&gt;&lt;/a&gt; module. It provides a memory-efficient way to handle file interactions in your test script and can read files in small chunks.&lt;/li&gt;
&lt;/ul&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Parameter&lt;/th&gt;
              &lt;th&gt;Type&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;filePath&lt;/td&gt;
              &lt;td&gt;string&lt;/td&gt;
              &lt;td&gt;The path to the file, absolute or relative, that will be read into memory. The file will only be loaded once, even when running with several VUs.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;mode&lt;/td&gt;
              &lt;td&gt;string&lt;/td&gt;
              &lt;td&gt;By default, the contents of the file are read as text, but if you specify &lt;code&gt;b&lt;/code&gt;, the file will be read as binary data instead.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h3 id=&#34;returns&#34;&gt;Returns&lt;/h3&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Type&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;string / ArrayBuffer&lt;/td&gt;
              &lt;td&gt;The contents of the file, returned as string or ArrayBuffer (if &lt;code&gt;b&lt;/code&gt; was specified as the mode).&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h3 id=&#34;examples&#34;&gt;Examples&lt;/h3&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JSON&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-json&#34;&gt;[
  {
    &amp;#34;username&amp;#34;: &amp;#34;user1&amp;#34;,
    &amp;#34;password&amp;#34;: &amp;#34;password1&amp;#34;
  },
  {
    &amp;#34;username&amp;#34;: &amp;#34;user2&amp;#34;,
    &amp;#34;password&amp;#34;: &amp;#34;password2&amp;#34;
  },
  {
    &amp;#34;username&amp;#34;: &amp;#34;user3&amp;#34;,
    &amp;#34;password&amp;#34;: &amp;#34;password3&amp;#34;
  }
]&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;!-- md-k6:skip --&gt;
&lt;!-- Requires the local data files shown or referenced in the example. --&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import { SharedArray } from &amp;#39;k6/data&amp;#39;;
import { sleep } from &amp;#39;k6&amp;#39;;

const data = new SharedArray(&amp;#39;users&amp;#39;, function () {
  // here you can open files, and then do additional processing or generate the array with data dynamically
  const f = JSON.parse(open(&amp;#39;./users.json&amp;#39;));
  return f; // f must be an array[]
});

export default () =&amp;gt; {
  const randomUser = data[Math.floor(Math.random() * data.length)];
  console.log(`${randomUser.username}, ${randomUser.password}`);
  sleep(3);
};&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;!-- md-k6:skip --&gt;
&lt;!-- Requires the local data files shown or referenced in the example. --&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import { sleep } from &amp;#39;k6&amp;#39;;

const users = JSON.parse(open(&amp;#39;./users.json&amp;#39;)); // consider using SharedArray for large files

export default function () {
  const user = users[__VU - 1];
  console.log(`${user.username}, ${user.password}`);
  sleep(3);
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;!-- md-k6:skip --&gt;
&lt;!-- Requires the local data files shown or referenced in the example. --&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import http from &amp;#39;k6/http&amp;#39;;
import { sleep } from &amp;#39;k6&amp;#39;;

const binFile = open(&amp;#39;/path/to/file.bin&amp;#39;, &amp;#39;b&amp;#39;);

export default function () {
  const data = {
    field: &amp;#39;this is a standard form field&amp;#39;,
    file: http.file(binFile, &amp;#39;test.bin&amp;#39;),
  };
  const res = http.post(&amp;#39;https://example.com/upload&amp;#39;, data);
  sleep(3);
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
]]></content><description>&lt;h1 id="open-filepath-mode-">open( filePath, [mode] )&lt;/h1>
&lt;p>Opens a file, reading all its contents into memory for use in the script.&lt;/p>
&lt;div class="admonition admonition-caution">&lt;blockquote>&lt;p class="title text-uppercase">Caution&lt;/p>&lt;p>&lt;code>open()&lt;/code> can only be called from the
&lt;a href="/docs/k6/v2.3.x/using-k6/test-lifecycle/#the-init-stage">init context&lt;/a>. This restriction is necessary to determine the local files needed to bundle when distributing the test across multiple nodes.&lt;/p></description></item></channel></rss>