From e60459079183975976edc68d97eb6b0b79ac2507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Ara=C3=BAjo?= Date: Tue, 17 May 2022 15:41:00 +0100 Subject: [PATCH] Change data storage to files --- benchmarking.ex | 19 +++ lib/faker.ex | 24 +++ lib/faker/address.ex | 3 + lib/faker/address/en.ex | 6 +- lib/faker/util.ex | 8 + mix.exs | 4 +- mix.lock | 5 + priv/data/address/countries/en.txt | 245 +++++++++++++++++++++++++++++ 8 files changed, 312 insertions(+), 2 deletions(-) create mode 100644 benchmarking.ex create mode 100644 priv/data/address/countries/en.txt diff --git a/benchmarking.ex b/benchmarking.ex new file mode 100644 index 000000000..c4c5068e6 --- /dev/null +++ b/benchmarking.ex @@ -0,0 +1,19 @@ +defmodule Benchemarking do + + def run do + test_country() + end + + defp test_country do + Benchee.run(%{ + "country_test_from_list" => fn -> Faker.Address.En.country end, + "country_test_from_file" => fn -> Faker.Address.En.country_test end + }, + formatters: [ + Benchee.Formatters.HTML, + Benchee.Formatters.Console + ]) + end +end + +Benchemarking.run diff --git a/lib/faker.ex b/lib/faker.ex index 337a4ec11..d83c3ed49 100644 --- a/lib/faker.ex +++ b/lib/faker.ex @@ -88,6 +88,11 @@ defmodule Faker do Application.get_env(:faker, :country) end + @spec country_test() :: atom + def country_test do + Application.get_env(:faker, :country_test) + end + @doc """ Sets application locale. """ @@ -160,6 +165,25 @@ defmodule Faker do end end + defmacro sampler_test(name, file) do + data = + File.read!(file) + |> String.split("\n") + |> IO.inspect() + + count = Enum.count(data) + + mapped_data = + data |> Enum.with_index() |> Enum.into(%{}, fn {k, v} -> {v, k} end) |> Macro.escape() + + quote do + def unquote(name)() do + unquote(mapped_data) + |> Map.get(Faker.random_between(0, unquote(count - 1))) + end + end + end + defmacro samplerp(name, data) do count = Enum.count(data) diff --git a/lib/faker/address.ex b/lib/faker/address.ex index 30f276635..669a66440 100644 --- a/lib/faker/address.ex +++ b/lib/faker/address.ex @@ -94,6 +94,9 @@ defmodule Faker.Address do @spec country() :: String.t() localize(:country) + @spec country_test() :: String.t() + localize(:country_test) + @doc """ Return country code. diff --git a/lib/faker/address/en.ex b/lib/faker/address/en.ex index 277e4bf11..e0fa45810 100644 --- a/lib/faker/address/en.ex +++ b/lib/faker/address/en.ex @@ -1,5 +1,5 @@ defmodule Faker.Address.En do - import Faker, only: [sampler: 2] + import Faker, only: [sampler: 2, sampler_test: 2] alias Faker.Person @@ -128,6 +128,10 @@ defmodule Faker.Address.En do iex> Faker.Address.En.country() "United Arab Emirates" """ + + @spec country_test() :: String.t() + sampler_test(:country_test, "priv/data/address/countries/en.txt") + @spec country() :: String.t() sampler(:country, [ "Afghanistan", diff --git a/lib/faker/util.ex b/lib/faker/util.ex index 34bae38d1..be6684a57 100644 --- a/lib/faker/util.ex +++ b/lib/faker/util.ex @@ -272,4 +272,12 @@ defmodule Faker.Util do _ -> raise "Rule #{rule_key} not found or not a function" end end + + def transform_into_map(data) do + count = Enum.count(data) + + mapped_data = data |> Enum.with_index() |> Enum.into(%{}, fn {k, v} -> {v, k} end) + + {mapped_data, count} + end end diff --git a/mix.exs b/mix.exs index 9392ddadd..823ae0ea0 100644 --- a/mix.exs +++ b/mix.exs @@ -44,7 +44,9 @@ defmodule Faker.Mixfile do {:ex_doc, ">= 0.0.0", only: :dev, runtime: false}, {:earmark, ">= 0.0.0", only: :dev, runtime: false}, {:credo, ">= 0.0.0", only: [:dev, :test], runtime: false}, - {:dialyxir, "~> 1.0", only: [:dev], runtime: false} + {:dialyxir, "~> 1.0", only: [:dev], runtime: false}, + {:benchee, "~> 1.0", only: :dev, runtime: false}, + {:benchee_html, "~> 1.0", only: :dev, runtime: false } ] end diff --git a/mix.lock b/mix.lock index 460daff90..6c87ae841 100644 --- a/mix.lock +++ b/mix.lock @@ -1,6 +1,10 @@ %{ + "benchee": {:hex, :benchee, "1.1.0", "f3a43817209a92a1fade36ef36b86e1052627fd8934a8b937ac9ab3a76c43062", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.0", [hex: :statistex, repo: "hexpm", optional: false]}], "hexpm", "7da57d545003165a012b587077f6ba90b89210fd88074ce3c60ce239eb5e6d93"}, + "benchee_html": {:hex, :benchee_html, "1.0.0", "5b4d24effebd060f466fb460ec06576e7b34a00fc26b234fe4f12c4f05c95947", [:mix], [{:benchee, ">= 0.99.0 and < 2.0.0", [hex: :benchee, repo: "hexpm", optional: false]}, {:benchee_json, "~> 1.0", [hex: :benchee_json, repo: "hexpm", optional: false]}], "hexpm", "5280af9aac432ff5ca4216d03e8a93f32209510e925b60e7f27c33796f69e699"}, + "benchee_json": {:hex, :benchee_json, "1.0.0", "cc661f4454d5995c08fe10dd1f2f72f229c8f0fb1c96f6b327a8c8fc96a91fe5", [:mix], [{:benchee, ">= 0.99.0 and < 2.0.0", [hex: :benchee, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "da05d813f9123505f870344d68fb7c86a4f0f9074df7d7b7e2bb011a63ec231c"}, "bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"}, "credo": {:hex, :credo, "1.6.6", "f51f8d45db1af3b2e2f7bee3e6d3c871737bda4a91bff00c5eec276517d1a19c", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "625520ce0984ee0f9f1f198165cd46fa73c1e59a17ebc520038b8fce056a5bdc"}, + "deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"}, "dialyxir": {:hex, :dialyxir, "1.2.0", "58344b3e87c2e7095304c81a9ae65cb68b613e28340690dfe1a5597fd08dec37", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "61072136427a851674cab81762be4dbeae7679f85b1272b6d25c3a839aff8463"}, "earmark": {:hex, :earmark, "1.4.27", "b413b0379043df51475a9b22ce344e8a58a117516c735b8871e6cdd5ed0f0153", [:mix], [{:earmark_parser, "~> 1.4.26", [hex: :earmark_parser, repo: "hexpm", optional: false]}], "hexpm", "579ebe2eaf4c7e040815a73a268036bcd96e6aab8ad2b1fcd979aaeb1ea47e15"}, "earmark_parser": {:hex, :earmark_parser, "1.4.28", "0bf6546eb7cd6185ae086cbc5d20cd6dbb4b428aad14c02c49f7b554484b4586", [:mix], [], "hexpm", "501cef12286a3231dc80c81352a9453decf9586977f917a96e619293132743fb"}, @@ -13,4 +17,5 @@ "makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"}, "nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"}, "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"}, + "statistex": {:hex, :statistex, "1.0.0", "f3dc93f3c0c6c92e5f291704cf62b99b553253d7969e9a5fa713e5481cd858a5", [:mix], [], "hexpm", "ff9d8bee7035028ab4742ff52fc80a2aa35cece833cf5319009b52f1b5a86c27"}, } diff --git a/priv/data/address/countries/en.txt b/priv/data/address/countries/en.txt new file mode 100644 index 000000000..c0529d01d --- /dev/null +++ b/priv/data/address/countries/en.txt @@ -0,0 +1,245 @@ +Afghanistan +Albania +Algeria +American Samoa +Andorra +Angola +Anguilla +Antarctica (the territory South of 60 deg S) +Antigua and Barbuda +Argentina +Armenia +Aruba +Australia +Austria +Azerbaijan +Bahamas +Bahrain +Bangladesh +Barbados +Belarus +Belgium +Belize +Benin +Bermuda +Bhutan +Bolivia +Bosnia and Herzegovina +Botswana +Bouvet Island (Bouvetoya) +Brazil +British Indian Ocean Territory (Chagos Archipelago) +Brunei Darussalam +Bulgaria +Burkina Faso +Burundi +Cambodia +Cameroon +Canada +Cape Verde +Cayman Islands +Central African Republic +Chad +Chile +China +Christmas Island +Cocos (Keeling) Islands +Colombia +Comoros +Congo +Congo +Cook Islands +Costa Rica +Cote d'Ivoire +Croatia +Cuba +Cyprus +Czech Republic +Denmark +Djibouti +Dominica +Dominican Republic +Ecuador +Egypt +El Salvador +Equatorial Guinea +Eritrea +Estonia +Ethiopia +Faroe Islands +Falkland Islands (Malvinas) +Fiji +Finland +France +French Guiana +French Polynesia +French Southern Territories +Gabon +Gambia +Georgia +Germany +Ghana +Gibraltar +Greece +Greenland +Grenada +Guadeloupe +Guam +Guatemala +Guernsey +Guinea +Guinea-Bissau +Guyana +Haiti +Heard Island and McDonald Islands +Holy See (Vatican City State) +Honduras +Hong Kong +Hungary +Iceland +India +Indonesia +Iran +Iraq +Ireland +Isle of Man +Israel +Italy +Jamaica +Japan +Jersey +Jordan +Kazakhstan +Kenya +Kiribati +Democratic People's Republic of Korea +Republic of Korea +Kuwait +Kyrgyz Republic +Lao People's Democratic Republic +Latvia +Lebanon +Lesotho +Liberia +Libyan Arab Jamahiriya +Liechtenstein +Lithuania +Luxembourg +Macao +Macedonia +Madagascar +Malawi +Malaysia +Maldives +Mali +Malta +Marshall Islands +Martinique +Mauritania +Mauritius +Mayotte +Mexico +Micronesia +Moldova +Monaco +Mongolia +Montenegro +Montserrat +Morocco +Mozambique +Myanmar +Namibia +Nauru +Nepal +Netherlands Antilles +Netherlands +New Caledonia +New Zealand +Nicaragua +Niger +Nigeria +Niue +Norfolk Island +Northern Mariana Islands +Norway +Oman +Pakistan +Palau +Palestinian Territory +Panama +Papua New Guinea +Paraguay +Peru +Philippines +Pitcairn Islands +Poland +Portugal +Puerto Rico +Qatar +Reunion +Romania +Russian Federation +Rwanda +Saint Barthelemy +Saint Helena +Saint Kitts and Nevis +Saint Lucia +Saint Martin +Saint Pierre and Miquelon +Saint Vincent and the Grenadines +Samoa +San Marino +Sao Tome and Principe +Saudi Arabia +Senegal +Serbia +Seychelles +Sierra Leone +Singapore +Slovakia (Slovak Republic) +Slovenia +Solomon Islands +Somalia +South Africa +South Georgia and the South Sandwich Islands +Spain +Sri Lanka +Sudan +Suriname +Svalbard & Jan Mayen Islands +Swaziland +Sweden +Switzerland +Syrian Arab Republic +Taiwan +Tajikistan +Tanzania +Thailand +Timor-Leste +Togo +Tokelau +Tonga +Trinidad and Tobago +Tunisia +Turkey +Turkmenistan +Turks and Caicos Islands +Tuvalu +Uganda +Ukraine +United Arab Emirates +United Kingdom +United States of America +United States Minor Outlying Islands +Uruguay +Uzbekistan +Vanuatu +Venezuela +Vietnam +Virgin Islands, British +Virgin Islands, U.S. +Wallis and Futuna +Western Sahara +Yemen +Zambia +Zimbabw \ No newline at end of file