Skip to content

Commit d7214bb

Browse files
committed
Change data storage to files
1 parent 94f891c commit d7214bb

8 files changed

Lines changed: 314 additions & 4 deletions

File tree

benchmarking.ex

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
defmodule Benchemarking do
2+
3+
def run do
4+
test_country()
5+
end
6+
7+
defp test_country do
8+
Benchee.run(%{
9+
"country_test_from_list" => fn -> Faker.Address.En.country end,
10+
"country_test_from_file" => fn -> Faker.Address.En.country_test end
11+
},
12+
formatters: [
13+
Benchee.Formatters.HTML,
14+
Benchee.Formatters.Console
15+
])
16+
end
17+
end
18+
19+
Benchemarking.run

lib/faker.ex

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ defmodule Faker do
8888
Application.get_env(:faker, :country)
8989
end
9090

91+
@spec country_test() :: atom
92+
def country_test do
93+
Application.get_env(:faker, :country_test)
94+
end
95+
9196
@doc """
9297
Sets application locale.
9398
"""
@@ -160,6 +165,25 @@ defmodule Faker do
160165
end
161166
end
162167

168+
defmacro sampler_test(name, file) do
169+
data =
170+
File.read!(file)
171+
|> String.split("\n")
172+
|> IO.inspect()
173+
174+
count = Enum.count(data)
175+
176+
mapped_data =
177+
data |> Enum.with_index() |> Enum.into(%{}, fn {k, v} -> {v, k} end) |> Macro.escape()
178+
179+
quote do
180+
def unquote(name)() do
181+
unquote(mapped_data)
182+
|> Map.get(Faker.random_between(0, unquote(count - 1)))
183+
end
184+
end
185+
end
186+
163187
defmacro samplerp(name, data) do
164188
count = Enum.count(data)
165189

lib/faker/address.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ defmodule Faker.Address do
9494
@spec country() :: String.t()
9595
localize(:country)
9696

97+
@spec country_test() :: String.t()
98+
localize(:country_test)
99+
97100
@doc """
98101
Return country code.
99102

lib/faker/address/en.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule Faker.Address.En do
2-
import Faker, only: [sampler: 2]
2+
import Faker, only: [sampler: 2, sampler_test: 2]
33

44
alias Faker.Person
55

@@ -128,6 +128,10 @@ defmodule Faker.Address.En do
128128
iex> Faker.Address.En.country()
129129
"United Arab Emirates"
130130
"""
131+
132+
@spec country_test() :: String.t()
133+
sampler_test(:country_test, "priv/data/address/countries/en.txt")
134+
131135
@spec country() :: String.t()
132136
sampler(:country, [
133137
"Afghanistan",

lib/faker/util.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,12 @@ defmodule Faker.Util do
272272
_ -> raise "Rule #{rule_key} not found or not a function"
273273
end
274274
end
275+
276+
def transform_into_map(data) do
277+
count = Enum.count(data)
278+
279+
mapped_data = data |> Enum.with_index() |> Enum.into(%{}, fn {k, v} -> {v, k} end)
280+
281+
{mapped_data, count}
282+
end
275283
end

mix.exs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ defmodule Faker.Mixfile do
4444
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
4545
{:earmark, ">= 0.0.0", only: :dev, runtime: false},
4646
{:credo, ">= 0.0.0", only: [:dev, :test], runtime: false},
47-
{:dialyxir, "~> 1.0", only: [:dev], runtime: false}
47+
{:dialyxir, "~> 1.0", only: [:dev], runtime: false},
48+
{:benchee, "~> 1.0", only: :dev, runtime: false},
49+
{:benchee_html, "~> 1.0", only: :dev, runtime: false }
4850
]
4951
end
5052

@@ -61,9 +63,9 @@ defmodule Faker.Mixfile do
6163
defp package do
6264
%{
6365
maintainers: ["Anthony Smith", "Igor Kapkov", "Toby Hinloopen", "Vitor Oliveira"],
64-
files: ["lib", "mix.exs", "mix.lock", "README.md", "LICENSE", "CHANGELOG.md"],
66+
files: ["lib", "mix.exs", "mix.lock", "README.md", "LICENSE", "CHANGELOG.md", "priv/data"],
6567
licenses: ["MIT"],
66-
links: %{"GitHub" => @source_url}
68+
links: %{"GitHub" => @source_url},
6769
}
6870
end
6971
end

mix.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
%{
2+
"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"},
3+
"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"},
4+
"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"},
25
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
36
"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"},
7+
"deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"},
48
"dialyxir": {:hex, :dialyxir, "1.2.0", "58344b3e87c2e7095304c81a9ae65cb68b613e28340690dfe1a5597fd08dec37", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "61072136427a851674cab81762be4dbeae7679f85b1272b6d25c3a839aff8463"},
59
"earmark": {:hex, :earmark, "1.4.27", "b413b0379043df51475a9b22ce344e8a58a117516c735b8871e6cdd5ed0f0153", [:mix], [{:earmark_parser, "~> 1.4.26", [hex: :earmark_parser, repo: "hexpm", optional: false]}], "hexpm", "579ebe2eaf4c7e040815a73a268036bcd96e6aab8ad2b1fcd979aaeb1ea47e15"},
610
"earmark_parser": {:hex, :earmark_parser, "1.4.28", "0bf6546eb7cd6185ae086cbc5d20cd6dbb4b428aad14c02c49f7b554484b4586", [:mix], [], "hexpm", "501cef12286a3231dc80c81352a9453decf9586977f917a96e619293132743fb"},
@@ -13,4 +17,5 @@
1317
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
1418
"nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"},
1519
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
20+
"statistex": {:hex, :statistex, "1.0.0", "f3dc93f3c0c6c92e5f291704cf62b99b553253d7969e9a5fa713e5481cd858a5", [:mix], [], "hexpm", "ff9d8bee7035028ab4742ff52fc80a2aa35cece833cf5319009b52f1b5a86c27"},
1621
}

priv/data/address/countries/en.txt

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
Afghanistan
2+
Albania
3+
Algeria
4+
American Samoa
5+
Andorra
6+
Angola
7+
Anguilla
8+
Antarctica (the territory South of 60 deg S)
9+
Antigua and Barbuda
10+
Argentina
11+
Armenia
12+
Aruba
13+
Australia
14+
Austria
15+
Azerbaijan
16+
Bahamas
17+
Bahrain
18+
Bangladesh
19+
Barbados
20+
Belarus
21+
Belgium
22+
Belize
23+
Benin
24+
Bermuda
25+
Bhutan
26+
Bolivia
27+
Bosnia and Herzegovina
28+
Botswana
29+
Bouvet Island (Bouvetoya)
30+
Brazil
31+
British Indian Ocean Territory (Chagos Archipelago)
32+
Brunei Darussalam
33+
Bulgaria
34+
Burkina Faso
35+
Burundi
36+
Cambodia
37+
Cameroon
38+
Canada
39+
Cape Verde
40+
Cayman Islands
41+
Central African Republic
42+
Chad
43+
Chile
44+
China
45+
Christmas Island
46+
Cocos (Keeling) Islands
47+
Colombia
48+
Comoros
49+
Congo
50+
Congo
51+
Cook Islands
52+
Costa Rica
53+
Cote d'Ivoire
54+
Croatia
55+
Cuba
56+
Cyprus
57+
Czech Republic
58+
Denmark
59+
Djibouti
60+
Dominica
61+
Dominican Republic
62+
Ecuador
63+
Egypt
64+
El Salvador
65+
Equatorial Guinea
66+
Eritrea
67+
Estonia
68+
Ethiopia
69+
Faroe Islands
70+
Falkland Islands (Malvinas)
71+
Fiji
72+
Finland
73+
France
74+
French Guiana
75+
French Polynesia
76+
French Southern Territories
77+
Gabon
78+
Gambia
79+
Georgia
80+
Germany
81+
Ghana
82+
Gibraltar
83+
Greece
84+
Greenland
85+
Grenada
86+
Guadeloupe
87+
Guam
88+
Guatemala
89+
Guernsey
90+
Guinea
91+
Guinea-Bissau
92+
Guyana
93+
Haiti
94+
Heard Island and McDonald Islands
95+
Holy See (Vatican City State)
96+
Honduras
97+
Hong Kong
98+
Hungary
99+
Iceland
100+
India
101+
Indonesia
102+
Iran
103+
Iraq
104+
Ireland
105+
Isle of Man
106+
Israel
107+
Italy
108+
Jamaica
109+
Japan
110+
Jersey
111+
Jordan
112+
Kazakhstan
113+
Kenya
114+
Kiribati
115+
Democratic People's Republic of Korea
116+
Republic of Korea
117+
Kuwait
118+
Kyrgyz Republic
119+
Lao People's Democratic Republic
120+
Latvia
121+
Lebanon
122+
Lesotho
123+
Liberia
124+
Libyan Arab Jamahiriya
125+
Liechtenstein
126+
Lithuania
127+
Luxembourg
128+
Macao
129+
Macedonia
130+
Madagascar
131+
Malawi
132+
Malaysia
133+
Maldives
134+
Mali
135+
Malta
136+
Marshall Islands
137+
Martinique
138+
Mauritania
139+
Mauritius
140+
Mayotte
141+
Mexico
142+
Micronesia
143+
Moldova
144+
Monaco
145+
Mongolia
146+
Montenegro
147+
Montserrat
148+
Morocco
149+
Mozambique
150+
Myanmar
151+
Namibia
152+
Nauru
153+
Nepal
154+
Netherlands Antilles
155+
Netherlands
156+
New Caledonia
157+
New Zealand
158+
Nicaragua
159+
Niger
160+
Nigeria
161+
Niue
162+
Norfolk Island
163+
Northern Mariana Islands
164+
Norway
165+
Oman
166+
Pakistan
167+
Palau
168+
Palestinian Territory
169+
Panama
170+
Papua New Guinea
171+
Paraguay
172+
Peru
173+
Philippines
174+
Pitcairn Islands
175+
Poland
176+
Portugal
177+
Puerto Rico
178+
Qatar
179+
Reunion
180+
Romania
181+
Russian Federation
182+
Rwanda
183+
Saint Barthelemy
184+
Saint Helena
185+
Saint Kitts and Nevis
186+
Saint Lucia
187+
Saint Martin
188+
Saint Pierre and Miquelon
189+
Saint Vincent and the Grenadines
190+
Samoa
191+
San Marino
192+
Sao Tome and Principe
193+
Saudi Arabia
194+
Senegal
195+
Serbia
196+
Seychelles
197+
Sierra Leone
198+
Singapore
199+
Slovakia (Slovak Republic)
200+
Slovenia
201+
Solomon Islands
202+
Somalia
203+
South Africa
204+
South Georgia and the South Sandwich Islands
205+
Spain
206+
Sri Lanka
207+
Sudan
208+
Suriname
209+
Svalbard & Jan Mayen Islands
210+
Swaziland
211+
Sweden
212+
Switzerland
213+
Syrian Arab Republic
214+
Taiwan
215+
Tajikistan
216+
Tanzania
217+
Thailand
218+
Timor-Leste
219+
Togo
220+
Tokelau
221+
Tonga
222+
Trinidad and Tobago
223+
Tunisia
224+
Turkey
225+
Turkmenistan
226+
Turks and Caicos Islands
227+
Tuvalu
228+
Uganda
229+
Ukraine
230+
United Arab Emirates
231+
United Kingdom
232+
United States of America
233+
United States Minor Outlying Islands
234+
Uruguay
235+
Uzbekistan
236+
Vanuatu
237+
Venezuela
238+
Vietnam
239+
Virgin Islands, British
240+
Virgin Islands, U.S.
241+
Wallis and Futuna
242+
Western Sahara
243+
Yemen
244+
Zambia
245+
Zimbabw

0 commit comments

Comments
 (0)